Understanding and simple implementation of Spring Aop, springaop understanding

Source: Internet
Author: User

Understanding and simple implementation of Spring Aop, springaop understanding
1. The Aspect-Oriented Programming mentioned in the AOP concept is actually a set of actions when processing a series of business logic. For example, to connect to the database: load the driver ----- get class -------- get the connection object ------- access the database ------ query --------- operation results for the above series of actions we regard the dotted line as a line by line. Then we add some logic to the position of the dotted line. Even if it is a log, this makes it possible to add logical processing to the corresponding location without knowing it. And formed the so-called Aspect-Oriented Programming! The following uses @ Before to demonstrate how to execute some logic [html] view plain copy Before Aop is woven into the method.

  1. Application of AOP:
  2. Xml configuration the xml configuration introduces the xsd syntax constraints of aop. In addition, the added automatic proxy label for aop. Please refer to the notes
  3. <? Xml version = "1.0" encoding = "UTF-8"?>
  4. <Beans xmlns = "http://www.springframework.org/schema/beans ";
  5. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance ";
  6. Xmlns: context = "http://www.springframework.org/schema/context ";
  7. Xmlns: aop = "http://www.springframework.org/schema/aop ";
  8. Xsi: schemaLocation = "http://www.springframework.org/schema/beans
  9. Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. Http://www.springframework.org/schema/context
  11. Http://www.springframework.org/schema/context/spring-context-2.5.xsd
  12. Http://www.springframework.org/schema/aop
  13. Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ";>
  14. <Context: annotation-config/>
  15. <! -- Configure the container resource scan package -->
  16. <Context: component-scan base-package = "com. spring"/>
  17. <! -- Automatically generate a proxy. The internal implementation is implemented using AspectJ. You can use the aspectj annotation to generate a proxy -->
  18. <Aop: aspectj-autoproxy/>
  19. </Beans>
Add the Jar package: 1. Compile the injection method. It must be the spring container management object [java] view plain copy
  1. Import org. springframework. stereotype. Component;
  2. Import com. spring. dao. UserDao;
  3. @ Component ("userService ")
  4. Public class UserServiceImpl implements UserService {
  5. Private UserDao userDao;
  6. Public void setUserDao (UserDao userDao ){
  7. This. userDao = userDao;
  8. }
  9. // Add logic before the following method
  10. Public void HelloWorld (){
  11. System. out. println ("helloworld ");
  12. }
  13. }
2. configuration of the weaving method: [java] view plain copy
  1. Package com. spring. aop;
  2. Import org. aspectj. lang. annotation. Aspect;
  3. Import org. aspectj. lang. annotation. Before;
  4. Import org. springframework. stereotype. Component;
  5. // Declarative aspect
  6. @ Aspect
  7. // Add the Spring management container
  8. @ Component ("LogInterceptor ")
  9. Public class LogInterceptor {
  10. // Specify the weaving method.
  11. @ Before ("execution (public void com. spring. service. UserServiceImpl. HelloWorld ())")
  12. Public void BeforeMethod (){
  13. System. out. println ("method start! ");
  14. }
  15. }
  16. To test the configuration of aop:
  17. Public class SpringTest {
  18. @ Test
  19. Public void test01 (){
  20. BeanFactory applicationContext = new ClassPathXmlApplicationContext (
  21. "Beans. xml ");
  22. UserService user = (UserService) applicationContext. getBean ("userService ");
  23. User. HelloWorld ();
  24. }
  25. }
Note: In the weaving point syntax During writing, specify the method before which before is woven. In brackets, it is best to implement an interface for the specified weaving point. If the interface is not implemented, the following error occurs: Cannot proxy target class because CGLIB2 is not available. add CGLIB to the class path or specify proxy interfaces. the solution to this exception is: Add cglib. jar. This is because the method unit of the woven object requires the support of cglib. jar if the interface is not implemented. The basic concept of AOP: JoinPoint: the starting point, which can be understood as the HelloWorld method in the above case, is a joinpointcut: the set of starting points. You can define N starting points to be added to the logic by means of the weaving point syntax. Aspect: a plane, which refers to a plane class. That is, the logic set Advise of Aspect declared above refers to the logic on the Aspect, such as @ Before and @ AfterTarget: the object to be proxies. The above case refers to UserServiceImplWeave: woven

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.