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

Spring 面向切面(AOP)编程,注解

2017-01-20 16:23 676 查看
引入AOP命名空间

两种方式:

1. 基于XML配置方式进行AOP开发。

2. 基于注解方式进行AOP开发。

/**
* Created by nanji on 2017/1/19.
*/
public class JDKProxyFactory implements InvocationHandler{
private Object targetObject;
public Object createProxyIntance(Object targetObject){
this.targetObject=targetObject;
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),this.targetObject.getClass().getInterfaces(),this);
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//环绕通知
Object result=null;
PersonServiceBean bean=(PersonServiceBean)this.targetObject;
if(bean.getUser()!=null){
//....advice() -->前置通知
try {
result=method.invoke(targetObject,args);
// afteradvice () -->后置通知
} catch (RuntimeException e){
//exceptionAdvice() -->例外通知
}finally {
//finallyadvice() --嘴终通知
}
}
return result;
}
}


public class PersonServiceBean implements PersonService{
@Override
public void save(String name) {
System.out.println("xxxxxxxx");
throw new RuntimeException("wo is exception");
}

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

@Override
public String getPersonName(Integer id) {
return "xxxxsss ";
}
}


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

@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println("my is before "+name);
}
@AfterReturning(pointcut = "anyMethod()",returning = "result")
public void doAfterReturning(String result){
System.out.println("houzhitongzhi"+result);
}

@After("anyMethod()")
public void doAfterCheck(){
System.out.println("my is after");
}

@AfterThrowing(pointcut = "anyMethod()",throwing = "e")
public void doAfterThrowing(Exception e ){
// throw new RuntimeException("wo is exception ");
System.out.println("exception informing "+e);
}

@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("into method");
Object result=pjp.proceed();
System.out.println("out method");
return result;
}
}


public class SpringAOPTest {

@Before
public void before() throws Exception {
}

@After
public void after() throws Exception {
}

@Test
public void interceptorTest() {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService=(PersonService)cxt.getBean("personService");
personService.save("xxxxxx");
//personService.getPersonName(2);
}


再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"
xsi:schemaLocation="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.xsd" >
<aop:aspectj-autoproxy/>
<bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
<aop:config>
<aop:aspect id="asp" ref="aspetbean">
<aop:pointcut id="mycut"  expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))" />
<aop:before method="doAccessCheck" pointcut-ref="mycut"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut"/>
<aop:after method="doAfterCheck" pointcut-ref="mycut"/>
<aop:around method="anyMethod" pointcut-ref="mycut"/>
</aop:aspect>
</aop:config>
</beans>


import cn.itcast.service.PersonService;

/**
* Created by nanji on 2017/1/20.
*/
public class PersonServiceBean implements PersonService{
@Override
public void save(String name) {
System.out.println("xxxxxxxx");
}

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

@Override
public String getPersonName(Integer id) {
return "xxxxsss ";
}
}


/**
* Created by nanji on 2017/1/20.
*/
//@Aspect
public class MyInterceptor {

//   @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
private void anyMethod(){
System.out.println("anyMethod.....");
}

//  @Before("anyMethod() && args(name)")
public void doAccessCheck(){
System.out.println("my is before ");
}
// @AfterReturning(pointcut = "anyMethod()",returning = "result")
public void doAfterReturning(){
System.out.println("houzhitongzhi");
}

//   @After("anyMethod()")
public void doAfterCheck(){
System.out.println("my is after");
}

//  @AfterThrowing(pointcut = "anyMethod()",throwing = "e")
public void doAfterThrowing(){
// throw new RuntimeException("wo is exception ");
System.out.println("exception informing ");
}

//  @Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("into method");
Object result=pjp.proceed();
System.out.println("out method");
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring 编程