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

查看网上资料配置使用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的包的版本问题,

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