SpringAOP Based on XML configuration files

Source: Internet
Author: User

Including: poincut, advisor, and aspect elements, which are configured in XML.

SpringAOP loading steps:
1. When the spring container is started, the spring configuration file is loaded.
2. Create an object for all beans in the configuration file
3. The spring container parses the configuration of aop: config.
1. parse the entry-point expression and use the entry-point expression and bean incorporated into the spring container for matching.
If the matching succeeds, a proxy object will be created for the bean. The proxy object method = target method + notification
If the match fails, no proxy object is created.
4. When the client uses context. getBean to obtain an object, if the object has a proxy object, the proxy object is returned. If the proxy object is returned, the target object is returned.


Note: If the target class does not implement interfaces, the spring container uses cglib to generate proxy objects. If the interface is implemented, jdk is used.


MyAspect. java

Package net. csdn. www. aop; import org. aspectj. lang. joinPoint; import org. aspectj. lang. proceedingJoinPoint; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. afterReturning; import org. aspectj. lang. annotation. around; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. springframework. core. annotation. order; public class MyAspect {public vo Id authority (JoinPoint jp) {System. out. println ("Name of the proxy method:" + jp. getSignature (). getName (); System. out. println ("parameter of the proxy method:" + jp. getArgs (); System. out. println ("proxy object:" + jp. getTarget (); System. out. println ("permission verification is called now");} public void log (JoinPoint jp, Object rvt) {System. out. println ("Name of the proxy method:" + jp. getSignature (). getName (); System. out. println ("parameter of the proxy method:" + jp. getArgs (); System. out. println ("proxy object:" + jp. getTarget (); Sy Stem. out. println ("returned value of the proxy object" + rvt); System. out. println ("currently called Log Management");} public Object processTx (ProceedingJoinPoint pjp) throws Throwable {System. out. println ("the transaction is enabled now"); // obtain the business method parameter Object [] args = pjp. getArgs (); System. out. println ("business method parameters:" + args); // Business Method of the proxy Object result = pjp. proceed (args); System. out. println ("currently calling transaction commit or rollback"); return result;} public void release () {System. out. println ("The resource has been released! ");}}

UserDao. java

Package net. csdn. www. dao; import java. util. date; import org. springframework. stereotype. component; @ Componentpublic class UserDao {public String save (String name) {System. out. println (name + "saved"); return "success ";}}

ApplicationContext. xml

 
 
  
  
  
  
 

Test. java

package net.csdn.www.text;import java.util.Date;import net.csdn.www.dao.UserDao;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {   public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");UserDao  userDao=(UserDao) context.getBean("userDao");     userDao.save("csdnccccccc");   }}



Declare a method with parameters in the UserDao class

Public String eat (Date date, String food) {System. out. println (date + "Preparations:" + food); return "delicious ";}

MyAspect1.java

Package net. csdn. www. aop; import java. util. date; import org. aspectj. lang. annotation. afterReturning; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. springframework. core. annotation. order; public class MyAspect1 {// Add Parameter Match. The parameter location type must be consistent with that of the proxy method. public void access (Date date, String food, Object rvt) {System. out. println (date + "eat" + food + "-----"); System. out. println ("Return Value:" + rvt );}}

Configure the aspect in the XML file

 
  

Call userDao. eat (new Date (), "xiaochi ");



Set the entry point


Id Entry Point id
Expression pointcut expression


Change the XML file

 
 
  
  
  
  
                  
 



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.