您的位置:首页 > 编程语言 > Java开发

Spring AOP基本配置

2013-03-15 15:03 363 查看
前几天在公司的项目里面加入了SpringAOP的特性,主要用来自动抽取一些信息,填充展示的实体类。为了不破坏原有的结构,只能使用AOP切面,切在实体保存的时候,通过设置AfterReturn切入点,就可以很好的完成抽取工作了。

在Spring3.0中除了引入基本的jar包,另外要加入

aspectj-1.6.10.jar

aspectjrt.jar

com.springsource.net.sf.cglib-2.2.0.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

之后在applicationContext.xml中加入如下的基本配置:

<?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"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="cn.com.yong.aop">

<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>

</context:component-scan>

<aop:aspectj-autoproxy />

</beans>

完成这两步,相当于应用已经有了Spring的AOP特性了。

接着,编写自己的业务逻辑:

1、首先定义一个接口

public interface MyInterface {

public void add1(String str);

public void add2(Object str);

}

实现类:

public class Address implements MyInterface {

public void add(String address) {

System.out.println("=====1======"+address+"===========");

}

public void add2(Object address) {

System.out.println("=====2======"+address+"===========");

}

}

业务类:

public class Person {

public void add(Object obj) {

System.out.println("=========add method=========");

}

}

然后我们编写Aspect类:

@Aspect

public class AuthorityAspect {

@Before("execution(* *.add(..)) && within(cn.com.yong.test.Person)")

public void authority() {

// before add method, we should authority

System.out.println("=======check authority1=======");

}

@Before("target(cn.com.yong.test.MyInterface) && args(java.lang.String)")

public void authority2() {

// before add method, we should authority

System.out.println("=======check authority2=======");

}

}

显然,这个切面类,定义了两个before切入点,分别切在add()方法和MyInterface接口的实现类上。

最后是我的测试方法:

public class TestAop {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// 测试类1

Person p = context.getBean("person", Person.class);

System.out.println("========person start============");

p.add(new Object());

System.out.println("==========person end=============");

// 测试类2

MyInterface a = context.getBean("address", MyInterface.class);

System.out.println("========address start===========");

a.add("yong");

a.add2(new Object());

System.out.println("========address end=============");

}

}

测试结果:

========person start============

=========add method=========

==========person end=============

========address start===========

=====1======yong===========

=====2====java.lang.Object@3e7799=========

========address end=============

Perfect!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: