Spring entry Blog [9. Implementation of Spring Aop in xml format]

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.