LogInterception.java
package com.bebig.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
//需要加入Aspectj包文件才能使用,而Aspectj包又需要aopalliance包支持
@Component//此实现方法对象必须由Spring容器来初始化才能实现代理
public class LogInterception {
    
// @Before("execution(public void com.bebig.dao.impl.UserDAOImpl.save(com.bebig.model.User))")
    
// @Before("execution(public * com.bebig.dao.impl.UserDAOImpl.*(..))")
    
// @Before("execution(* save(..))")
    
// @Before("within(com.bebig.dao.impl..*)")//将此包及其子包内所有方法进行代理
    
//其它注解请查Spring文档
    @Before("myMethod()")
    
public void before() {
        System.out.println(
"Log start");
    }


    @Pointcut(
"within(com.bebig.service..*)")
    
//代理没有实现接口的对象时,是由CGLib直接生成二进制文件的,因此需要cglib包文件与objectweb.asm包文件支持
    public void myMethod() {
    }
;

}

UserDAO.java
package com.bebig.dao;

import com.bebig.model.User;

public interface UserDAO {
    
public void save(User u);
    
public void deleteById(int id);
}

UserDAOImpl.java
package com.bebig.dao.impl;

import org.springframework.stereotype.Repository;
import com.bebig.dao.UserDAO;
import com.bebig.model.User;

@Repository
public class UserDAOImpl implements UserDAO {

    @Override
    
public void save(User u) {
        System.out.println(
"a user saved!");
    }


    @Override
    
public void deleteById(int id) {
        System.out.println(
"delete a user by id!");
        
    }


}

User.java
package com.bebig.model;

public class User {
    
private String username;
    
private String password;

    
public String getPassword() {
        
return password;
    }


    
public String getUsername() {
        
return username;
    }


    
public void setPassword(String password) {
        
this.password = password;
    }


    
public void setUsername(String username) {
        
this.username = username;
    }

}

UserService.java
package com.bebig.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.bebig.dao.UserDAO;
import com.bebig.model.User;

@Service(
"userService")
public class UserService {
    
private UserDAO userDAO;

    
public void add(User u) {
        userDAO.save(u);

    }


    
public void deleteById(int id) {
        userDAO.deleteById(id);

    }


    @Resource
    
public void setUserDAO(UserDAO userDAO) {
        
this.userDAO = userDAO;
    }


    
public UserDAO getUserDAO() {
        
return userDAO;
    }


    
public void init() {
        System.out.println(
"init.");
    }


    
public void destory() {
        System.out.println(
"destory.");
    }

}

beans.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:context
="http://www.springframework.org/schema/context"
    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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>

    
<!-- a service object; we will be profiling its methods -->
    
<context:annotation-config />
    
<context:component-scan base-package="com.bebig" />
    
<!-- 动态代理Annotation编写方法 -->
    
<aop:aspectj-autoproxy/>

</beans>
UserServiceTest.java
package com.bebig.service;

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

import com.bebig.model.User;

public class UserServiceTest {

    @Test
    
public void testAdd() throws Exception {
        ClassPathXmlApplicationContext cxt 
= new ClassPathXmlApplicationContext(
                
"beans.xml");
        
// 不再需要使用Proxy对象来创建代理对象
        UserService service = (UserService) cxt.getBean("userService");
        service.add(
new User());
        service.deleteById(
1);
    }


}