In some cases, the JDK used in our engineering is not necessarily more than 1.5, which means that annotation annotations may not be supported by nature, and it is not possible to use @aspectj annotation-driven AOP, so if we still want to use AspectJ flexible pointcut expressions, So what should we do? Spring provides us with an AOP namespace based on XML schematic, which is used in the same way as @aspectj annotations, unlike configuration information that is moved from annotations to the spring configuration file. Here, we will detail how to configure Spring AOP with the <aop:config/> tags provided by spring.
1. A little preparatory work and an example
Using the <aop:config/> tag, you need to introduce the spring AOP namespace based on the XML schema to the spring configuration file. The following spring configuration file is completed (in this section, the spring AOP namespace is added to all routines ' configuration files, except in exceptional cases, in order to conserve space, this part will be omitted in the given code), and the bold content is what we need to add:
Code
<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd >
………… Spring配置信息
</beans>
With regard to the label of the AOP namespace, we have previously used <aop:aspectj-autoproxy/>, in this section we will focus on <aop:config/> tags. In fact, all of the tags we covered in this section are child labels for that label.
Here's an example of how to use the <aop:config/> tag to configure Spring AOP (see routine 4.15 for complete code). In the example, we use <aop:config/> to configure a slice and intercept the SayHello () method of the target object peoples to output the prompt before it executes.
First create the project aop_test4.15, add the spring IOC and the Spring AOP Library, create the Aop.test package, new target class people, and the following code:
Code
package aop.test;
/**
* 该类将作为目标对象对应的类。
* @author zhangyong
* */
public class People{
public String SayHello(String str){
System.out.println(this.getClass().getName()+ "说:"+str);
return str;
}
}