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.
Introducing AspectJ related class library:
<dependency> <groupId></groupId> <artifactId>aspectjrt</artifactId> <version>${}</version> </dependency> <dependency> <groupId></groupId> <artifactId>aspectjweaver</artifactId> <version>${}</version> </dependency> <dependency> <groupId></groupId> <artifactId>aspectjtools</artifactId> <version>${}</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${}</version> </dependency>
2 Programming methods
@Aspect//Identify facets
public class PreRentAspect { /** * Enhanced logic */ @Before("execution(* rent(..))")//Define the tangent and enhancement types public void beforeRent() { ("Start the rental action"); } }
This section is just an ordinary POJO, but the @Aspect annotation is added.
@Before("execution(* rent(..))")
In-house@Before
It means that the enhancement 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 target class's rent() method. rent() can contain any incoming parameters and any return value.
bring@Aspect
The class, through annotation and code, integrates the tangent point, enhancement type and enhanced cross-cutting logic together. Isn't it very convenient? O(∩_∩)O Haha~
Unit Tests:
AspectJProxyFactory factory = new AspectJProxyFactory(); //Set target class(new User()); //Add a facet class(); User proxy = (); String userId = "001"; (userId); (userId);
Output result:
--Start the rental action-
User: Rental [Power Bank]
User: Return [Power Bank]
3 Configuration method
<!-- Target class--> <bean class="."/> <!-- Face-cut--> <bean class="."/> <!-- Automatically create a proxy--> <bean class=""/>
Unit Tests:
ApplicationContext context = new ClassPathXmlApplicationContext("); User user = (User) ("user"); String userId = "001"; (userId); (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="/schema/beans" xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:aop="/schema/aop" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4. /schema/aop /schema/aop/"> <!--aspectj Drive --> <aop:aspectj-autoproxy/> <!-- Target class--> <bean class="."/> <!-- Face-cut--> <bean class="."/> </beans>
This configuration is simpler. Actually <aop:aspectj-atuoproxy/>
The automatic proxy mode has been adopted inside. O(∩_∩)O Haha~
<aop:aspectj-atuoproxy/>
ofproxy-target-class
The property, defaults to false, means using JDK dynamic proxy technology to weave enhancement; if this value is true, means using CGLib dynamic proxy technology to weave enhancement. If the target class does not declare the interface, then even proxy-target-class
Set to false, and the enhanced yo(∩_∩)O will be automatically weave the enhanced yo(∩_∩)O haha~
For projects based on Java5.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 study and I hope everyone will support me more.