AOP:spring 的Annotation配置

1.文件目录:

2.实体类

package com.wangcf.po;

public class User {
    private int id;
    private String name;
    private String password;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

3.接口:

package com.wangcf.manager;

import com.wangcf.po.User;


public interface IUserManager {
    
    public void addUser();
    
    public void addUser(User user);
    public void delUser(int uid);
    
    public User modifyUser(User user);
}

4.实现类:

package com.wangcf.manager;

import com.wangcf.po.User;


public class UserManagerImp implements IUserManager{

    @Override
    public void addUser() {
        // TODO Auto-generated method stub
        System.out.println("UserManagerImp addUser..");
    }

    @Override
    public void delUser(int uid) {
        // TODO Auto-generated method stub
        System.out.println("UserManagerImp delUser..");
    }

    @Override
    public User modifyUser(User user) {
        // TODO Auto-generated method stub
        user.setName(user.getName()+",修改后的");
        System.out.println("UserManagerImp modifyUser..");
        return null;
    }

    @Override
    public void addUser(User user) {
        // TODO Auto-generated method stub
        System.out.println("UserManagerImp addUser(User user)...");
    }

}

5.切面:

package com.wangcf.manager.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;


@Aspect
/**
 * 切面类
 * @author fan
 *
 */
public class LogManager {
    @Before("execution(public void com.wangcf.manager.UserManagerImp.addUser())")
    /*addUser(..)表示带参数或不带参数
     * add*() 可以用通配符进行修饰
     * execution为切入点的指示符,还有其他的
     * advice(通知)
     */
    public static void addLogBefore(){
        System.out.println("添加日志 Before...");
    }
    
    @After("execution(* com.wangcf.manager.*.*(..))")
    public void addLogAfter(){
        System.out.println("添加日志记录 After...");
    }
    
    @AfterReturning("execution(* com.wangcf.manager.*.*(..))")
    public void addLogAfterReturning(){
        System.out.println("添加日志记录 AfterReturning...");
    }
    
    @AfterThrowing("execution(* com.wangcf.manager.*.*(..))")
    public void addLogAfterThrowing(){
        System.out.println("添加日志记录 AfterThrowing...");
    }
    
    @Around("execution(* com.wangcf.manager.*.*(..))")
    public void addLogAround(JoinPoint joinPoint){
        System.out.println("添加日志记录 AfterAround start...");
        System.out.println(joinPoint.getTarget());
        System.out.println("添加日志记录 AfterAround end...");
    }
}

6.测试类:

package com.wangcf.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wangcf.manager.IUserManager;
import com.wangcf.po.User;


public class TestAction {
    public static void main(String[] args) {
        BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserManager userManager=(IUserManager) factory.getBean("userManager");
        userManager.addUser();
        System.out.println("=================");
        User user=new User();
        user.setName("张三")    ;
        userManager.addUser(user);
        System.out.println("========");
        userManager.modifyUser(null);
    }
}

7.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-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <bean name="userManager" class="com.wangcf.manager.UserManagerImp"/>
    <bean name="logManager" class="com.wangcf.manager.aspect.LogManager"/>
    <!--启用AspectJ的支持  -->
    <aop:aspectj-autoproxy/>
</beans>

8.输出

9.总结:

在xml配置中会生成代理类

 

在使用SpringAOP的时候,业务类需要实现接口,不然就需要使用CGLIB生成目标对象的代理类

Spring会自动选择用何种方式处理代理:

1.JDK动态代理:当目标对象实现了接口的时候

2.CGLIB的代理:当目标对象没有实现接口的时候

可以强制使用CGLIB来生成代理:

<aop:aspectj-autoproxy proxy-target-class="true"/>

 

posted @ 2017-01-21 12:04  言凡  阅读(369)  评论(0编辑  收藏  举报