Spring Learning 2_AOP Simple implementation via XML configuration

Source: Internet
Author: User

Spring can be implemented using annotations or XML configuration to implement AOP functionality, which is based on the demo to simulate the process of implementing AOP in XML configuration.

The code structure is as follows

1, spring configuration is as follows, the corresponding tangent pointcut is configured in <aop:config>, and the corresponding Pointcut is referenced in the tangent aspect.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd        Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">           <BeanID= "DAO"class= "Com.dao.UserDaoImpl"/>       <BeanID= "UserService"class= "Com.service.UserService">       < Propertyname= "DAO"ref= "DAO"></ Property>   </Bean>       <BeanID= "Loginter"class= "Com.handler.LogInterceptor"/>    <Aop:config>          <Aop:pointcutexpression= "Execution (* com.service.userservice.* (..))"ID= "Servicepointcut" />       <Aop:aspectID= "LOGAOP"ref= "Loginter">           <Aop:beforeMethod= "Beforeexcute"Pointcut-ref= "Servicepointcut"/>       </Aop:aspect>   </Aop:config>  </Beans>

2. Interception class Loginterceptor

 Package Com.handler;  Public class Loginterceptor {    publicvoid  Beforeexcute () {        System.out.println (" Aspect execute. " );    }}

3, the interface call is completed by injecting the DAO layer through spring in UserService.

The test classes are as follows:

 Packagecom.test;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.service.UserService; Public classtestaopannotation {@Test Public voidtestaopannotation () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring.xml"); UserService Service= (UserService) context.getbean ("UserService");    Service.save (); }}

Test results:

Spring Learning 2_AOP Simple implementation via XML configuration

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.