Spring-AOP

来源:互联网 发布:陈思思捏脸数据 编辑:程序博客网 时间:2024/05/22 12:26

aop层User

public class User implements Serializable{    private Integer id;    private String username;    private String password;    private String email;


aop层UserDao

public class UserDao implements IDAO{    public void save(User user) {        System.out.println("save success!");    }}

aop层LoggerBefore


public class LoggerBefore implements MethodBeforeAdvice {    public void before(Method method, Object[] objects, Object o) throws Throwable {        System.out.println("=========================");    }}

aop层LoggerAfter
public class LoggerAfter implements AfterReturningAdvice{    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {        System.out.println("----之后------------------");    }}



aop层IDAO接口

public interface IDAO {    public void save(User user);}

applicationContext.xml

<!--增强配置--><!--前置--><bean id="beforeAdvice" class="cn.happy.printer.aop.LoggerBefore"></bean><!--后置--><bean id="afterAdvice" class="cn.happy.printer.aop.LoggerAfter"></bean><!--AOP配置--><aop:config>    <aop:pointcut id="pointcut" expression="execution(public void *(..))"></aop:pointcut>    <!--顾问-->     <aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointcut"/>     <!-- <aop:advisor advice-ref="afterAdvice" pointcut-ref="pointcut"/>--></aop:config>
测试
 @Test    public void test03() {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        IUserBiz biz = (IUserBiz) context.getBean("userService");        System.out.println(biz);        User user=new User();        user.setUsername("asdf");        biz.save(user);    }}