AOP in spring based on AOP namespaces one (a bit of preparation and an example)

Source: Internet
Author: User
Tags aop config xmlns

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;
}
}

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.