Spring 注解AOP 入门
http://www.flypeng.com win10系统 发布时间:2016-01-05 09:40 来源:未知 浏览:加载中

 XML

<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        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">
    <aop:aspectj-autoproxy />
    <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor" />
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" />
</beans>

 

业务Bean,接口就不贴了

复制代码
package cn.itcast.service.impl;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {

    public String getPersonName(Integer id) {
        System.out.println("我是getPersonName()方法");
        return "XXX";
    }

    public void save(String name) {
        System.out.println("我是save()方法");
    }

    public void update(String name, Integer id) {
        System.out.println("我是update()方法");
    }

}
复制代码

实现AOP的类 

复制代码
package cn.itcast.service;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyInterceptor {
    @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
    private void anyMethod() {}

    @Before("anyMethod()")
    public void doAccessCheck(String userName) {
        System.out.println("前置通知");
    }
}
复制代码

测试方法

复制代码
package junit.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {
    @Test
    public void interceptorTest(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService = (PersonService)ctx.getBean("personService");
        personService.save("xxx");
    }
}
复制代码

如果你有好的win10资讯或者win10教程,以及win10相关的问题想要获得win10系统下载的关注与报道。
欢迎加入发送邮件到657025171#qq.com(#替换为@)。期待你的好消息!