XML file configuration in spring implements AOP

Source: Internet
Author: User

The configuration file differs greatly from the way annotations are, with many more configuration items.

Beans2.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: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 ">
<aop:aspectj-autoproxy/>
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" ></bean>
<bean id= "Myinterceptor" class= "Test.spring.aop.MyInterceptor2" ></bean>
<aop:config>
<aop:aspect id= "Myaspect" ref= "Myinterceptor" >
<aop:pointcut id= "mypointcut" expression= "Execution (* test.spring.service.impl.personservicebean.* (..))"/>
<aop:before pointcut-ref= "Mypointcut" method= "Doaccesscheck"/>
<aop:after-returning pointcut-ref= "Mypointcut" method= "doafterreturning"/>
<aop:after-throwing pointcut-ref= "Mypointcut" method= "doafterthrowing"/>
<aop:around pointcut-ref= "Mypointcut" method= "Doaround"/>
<aop:after pointcut-ref= "Mypointcut" method= "Doafter"/>
</aop:aspect>
</aop:config>
</beans>

Pointcut syntax definition for facets

    • Intercept all the methods under Test.spring.service.impl.PersonServiceBean
      expression= "Execution (* test.spring.service.impl.personservicebean.* (..))"
    • All methods of intercepting all classes under the Test.spring.service.impl sub-package
      expression= "Execution (* Test.spring.service.impl). *.*(..))"
    • Block all methods that return a string type under Test.spring.service.impl.PersonServiceBean
      expression= "Execution (java.lang.String test.spring.service.impl.personservicebean.* (..))"
    • Block the first method in all methods under Test.spring.service.impl.PersonServiceBean that has a parameter type of string
      expression= "Execution (* test.spring.service.impl.personservicebean.* (java.lang.String,..))"

[Java]View Plain Copy
  1. Package Test.spring.service.impl;
  2. Import Test.spring.service.PersonService;
  3. The proxy object implements all interfaces of the target object
  4. Public class Personservicebean implements Personservice {
  5. Public Personservicebean () {
  6. }
  7. @Override
  8. public void Save (String name) {
  9. System.out.println ("Save ()->>" + name);
  10. throw New RuntimeException (">>----Custom Exception----<<");
  11. }
  12. @Override
  13. Public String GetResult () {
  14. return "GetResult () ==>> returns the result";
  15. }
  16. }

[Java]View Plain Copy
  1. Package TEST.SPRING.AOP;
  2. Import Org.aspectj.lang.ProceedingJoinPoint;
  3. Public class MyInterceptor2 {
  4. public void Doaccesscheck () {
  5. System.out.println ("Pre-notification-->>");
  6. }
  7. public void Doafterreturning () {
  8. System.out.println ("post-notification-->>");
  9. }
  10. public void Doafter () {
  11. System.out.println ("Final Notice");
  12. }
  13. public void Doafterthrowing () {
  14. System.out.println ("Exception notification--");
  15. }
  16. Public Object Doaround (proceedingjoinpoint pjoinpoint) throws Throwable {
  17. System.out.println ("surround Notice");
  18. //here if Pjoinpoint.proceed () does not execute, the methods that are intercepted later are not executed, and are well suited for rights management
  19. Object result = Pjoinpoint.proceed ();
  20. System.out.println ("exit");
  21. return result;
  22. }
  23. }

[Java]View Plain Copy
  1. Package test.spring.junit;
  2. Import Org.junit.Test;
  3. Import Org.springframework.context.support.AbstractApplicationContext;
  4. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  5. Import Test.spring.service.PersonService;
  6. Public class AOPTest3 {
  7. @Test
  8. public Void Test () {
  9. Abstractapplicationcontext Acontext = //
  10. New Classpathxmlapplicationcontext ("Beans2.xml");
  11. Personservice Pservice = (personservice) acontext
  12. . Getbean ("Personservice");
  13. Pservice.save ("Lindl");
  14. Pservice.getresult ();
  15. Acontext.close ();
  16. }
  17. }

XML file configuration in spring implements AOP

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.