查看网上资料配置使用spring-aop的笔记
2018-03-02 10:22
435 查看
1.配置好ssm框架后,spring-aop的所需要的额外jar包
2.增添一个spring-aop.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
</beans>
3.配置的方式
<!-- 所需要配置的切面的(即是执行业务逻辑之前需要做的权限查询等) --><bean id="timeHandler" class="com.ssm.test.PrintTimeHandler" />
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<!-- 切点的配置(从何处切入)表示com.ssm.dao.sayHelloServiceIpm下的所有的类均执行切面(可精确到具体的方法) -->
<aop:pointcut id="addAllMethod" expression="execution(* com.ssm.servicesImp.sayHelloServiceIpm.*(..))" />
<aop:before method="sayWords" pointcut-ref="addAllMethod"/>
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
4.使用半自动注解(注解切面)
<!-- 所需要配置的切面的 --><!-- <bean id="timeHandler" class="com.ssm.test.PrintTimeHandler" /> -->
<!-- 使用xml配置aop -->
<!-- 强制使用cglib代理,如果不设置,将默认使用jdk的代理,但是jdk的代理是基于接口的 -->
<aop:config proxy-target-class="true" />
<aop:config>
<aop:aspect id="time" ref="PrintTimeHandler">
<!-- 切点的配置(从何处切入)表示com.ssm.dao.sayHelloDaobao下的所有的类均执行切面(可精确到具体的方法) -->
<aop:pointcut id="addAllMethod" expression="execution(* com.ssm.servicesImp.sayHelloServiceIpm.*(..))" />
<aop:before method="sayWords" pointcut-ref="addAllMethod"/>
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
该配置需要在拦截的类里添加注解@component("别名对应PrintTimeHandler")
5.使用完全注解方式(仅在aop.xml配置文件中配置开启开启aop注解方式)
<!-- 开启注解扫描 --><!-- 开启aop注解方式即可,此步骤不能少,这样java类中的aop注解才会生效 -->
<aop:aspectj-autoproxy/>
@Aspect标注切面类(需要的拦截等功能。。)
@component(表明将此类交给spring,)
(1)方式一(表明切点以及切入的时机)
@Aspect
@Component
public class Sings {
@Before(value="execution(* com.ssm.servicesImp.sayHelloServiceIpm.*(..))")
public void sing1(){
System.out.println("I Love Five Moth Day");
}
}
@Before(value="execution(* com.ssm.servicesImp.sayHelloServiceIpm.*(..))")(表明切点以及切入的时机)
(2)方式二
@Aspect
@Component
public class PrintTimeHandler {
public void printTime(){
System.out.println("CurrentTime="+new Date(System.currentTimeMillis()));
}
@Pointcut(value="execution(* com.ssm.servicesImp.sayHelloServiceIpm.*(..))")
public void ass(){};
@After("ass()")
public void sayWords(){
System.out.println("唱一首歌:最美的太阳");
}
如果运行过程中出现Spring aop : error at ::0 can't find referenced pointcut sleepPonit的错误,那么很可能是spring的包的版本问题,
相关文章推荐
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)
- 使用Spring AOP 自动代理配置声明式事务
- Spring 2.0 AOP 与事务配置[资料]
- spring AOP拦截器的配置使用
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- 使用 来配置Spring AOP
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- Spring学习笔记 在XML配置文件中使用properties文件的键值
- spring 学习笔记 使用pojo+xml的方式开发aop
- Spring中AOP的配置与使用(csdn站内转载)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- spring aop的使用(注解方式以及基于xml配置方式)
- 使用spring配置文件实现AOP
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- 学习51笔记与一份从网上找的资料:单片机C语言中_nop_函数的使用及C语言的延时计算2010-
- Spring学习笔记 使用annotation配置实现Bean的auto-wiring (自动绑定)
- spring aop的使用(注解方式以及基于xml配置方式)
- [原]spring学习笔记9.3-Spring对Aop的支持-配置文件的方式
- Spring学习笔记(14)----使用Spring的注解方式实现AOP