(This case is a maven project under the idea, supplement the previous article)
1. Write a comment in the class enhancements:
@Aspect
// Class - cut
public class Anno {
@Before (. "Execution (com.xbf.service.UserServiceImpl * * (..))")
// comment statement: entry points, and a method for weaving into
void before public () {
System.out.println ( "before the method executes: ~~~~~~~~~~~~~~");
}
@After ( "execution (* com.xbf.service.UserServiceImpl. * (..)) ")
// comment statement: entry points, and a method for weaving into
public void after () {
System.out.println (" the method performed ~~~~~~~~~~~ ~ ");
}
}
2.spring profile dnno.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.User bean的注册--> <bean id="user" class="com.xbf.service.UserServiceImpl"/> <! - 2 registered enhanced class -> <the bean ID = "Anno" class = "com.xbf.anno.Anno" /> <! - automatic agent identification annotation -> <AOP: AspectJ-the autoproxy /> </ Beans>
3. Test preparation classes:
public class Anno { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("anno.xml"); UserService user = (UserService) context.getBean("user"); user.add(); } }
Summary: Enhanced class to add annotations; add annotations in the class name section above, add the name of the above methods in the class notes woven into the timing (and declare the starting point, the starting point is to achieve the objectives woven into the interface method of the class) .