AspectJ describes tangent and enhancement through annotations.
1 Development environment requirements
Because you want to use annotations, make sure you are using Java 5.0 and above.
Introduce AspectJ related class library:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version></dependency><dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${aopalliance.version}</version></dependency> 2 Programming methods
@Aspect//Identify facets
public class PreRentAspect { /** * Enhanced logic*/ @Before("execution(* rent(..))")//Define tangent and enhancement types public void beforeRent() { System.out.println("Start execution of rental actions"); }}This section is just an ordinary POJO, but the @Aspect annotation is added.
@Before("execution(* rent(..))") means that the @Before type is a pre-enhanced enhancement, and its content is a @AspectJ point-cut expression. Here it means that the enhancement is weaved on the rent() method of the target class. rent() can contain any incoming parameters and any return value.
Isn't it very convenient for a class with @Aspect to integrate the tangent point, enhancement type and enhanced cross-cutting logic through annotation and code? O(∩_∩)O Haha~
Unit Tests:
AspectJProxyFactory factory = new AspectJProxyFactory();//Set the target class factory.setTarget(new User());//Add the facet class factory.addAspect(PreRentAspect.class);User proxy = factory.getProxy();String userId = "001";proxy.rent(userId);proxy.back(userId);
Output result:
--Start the rental action-
User: Rental [Power Bank]
User: Return [Power Bank]
3 Configuration method
<!-- Target class--><bean id="user"/><!-- Face class--><bean/><!-- Automatically create agent--><bean />
Unit Tests:
ApplicationContext context = new ClassPathXmlApplicationContext(spring.xml");User user = (User) context.getBean("user");String userId = "001";user.rent(userId);user.back(userId);The output is exactly the same as the program.
It can also be configured based on Schema's aop namespace:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--aspectj drive--> <aop:aspectj-autoproxy/> <!-- Target class--> <bean id="user"/> <!-- Face class--> <bean/></beans>
This configuration is simpler. In fact, the automatic proxy mode has been adopted within <aop:aspectj-atuoproxy/> O(∩_∩)O Haha~
proxy-target-class attribute of <aop:aspectj-atuoproxy/> is false by default, indicating that the enhancement is weaved using JDK dynamic proxy technology; if this value is true, it indicates that the enhancement is weaved using CGLib dynamic proxy technology. If the target class does not declare the interface, even if proxy-target-class is set to false, the enhanced yo (∩_∩)O Haha~
For projects based on Java 5.0+, it is recommended to use AspectJ to configure point-cutting and enhancement, because this is simpler and more direct.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.