Spring AOP基本配置

前几天在公司的项目里面加入了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!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值