After learning Spring AOPAnnotation, I think it is easy to understand the xml method. This case will not repeat some detailed questions. Please refer to the case Code as follows:
1. Compile the proxy object:
[Java]
Import org. springframework. stereotype. Component;
Import com. spring. dao. UserDao;
@ Component ("userService ")
Public class UserServiceImpl implements UserService {
Private UserDao userDao;
Public void setUserDao (UserDao userDao ){
This. userDao = userDao;
}
// Add logic before the following method
Public void HelloWorld (){
System. out. println ("helloworld ");
}
}
2. Compile the configuration file:
[Html]
<? 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: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
<Context: annotation-config/>
<! -- Configure the container resource scan package -->
<Context: component-scan base-package = "com. spring"/>
<! -- Write the front class to the container -->
<Bean id = "logInterceptor" class = "com. spring. aop. LogInterceptor"/>
<! -- Configure AOP -->
<Aop: config>
<! -- Declare pointcut -->
<Aop: pointcut expression = "execution (public * com. spring. service... *. * (..)" id = "servicepoint"/>
<! -- Declare the partition class -->
<Aop: aspect id = "aspectLog" ref = "logInterceptor">
<Aop: before method = "BeforeMethod" pointcut-ref = "servicepoint"/>
<Aop: after-returning method = "AfterMethod" pointcut-ref = "servicepoint"/>
<Aop: around method = "aroundProcced" pointcut-ref = "servicepoint"/>
</Aop: aspect>
</Aop: config>
</Beans>
3. Compile the test file:
[Java]
Package com. spring. test;
Import org. junit. Test;
Import org. springframework. beans. factory. BeanFactory;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import com. spring. service. UserService;
Public class SpringTest {
@ Test
Public void test01 (){
BeanFactory applicationContext = new ClassPathXmlApplicationContext (
"Beans. xml ");
UserService user = (UserService) applicationContext. getBean ("userService ");
User. HelloWorld ();
}
}