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//此實現(xiàn)方法對象必須由Spring容器來初始化才能實現(xiàn)代理

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..*)")//將此包及其子包內(nèi)所有方法進行代理
//其它注解請查Spring文檔
@Before("myMethod()")

public void before()
{
System.out.println("Log start
");
}

@Pointcut("within(com.bebig.service..*)")
//代理沒有實現(xiàn)接口的對象時,是由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" />
<!-- 動態(tài)代理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對象來創(chuàng)建代理對象
UserService service = (UserService) cxt.getBean("userService");
service.add(new User());
service.deleteById(1);
}

}
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//此實現(xiàn)方法對象必須由Spring容器來初始化才能實現(xiàn)代理
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..*)")//將此包及其子包內(nèi)所有方法進行代理
//其它注解請查Spring文檔
@Before("myMethod()")
public void before()
{
System.out.println("Log start
");
}
@Pointcut("within(com.bebig.service..*)")
//代理沒有實現(xiàn)接口的對象時,是由CGLib直接生成二進制文件的,因此需要cglib包文件與objectweb.asm包文件支持
public void myMethod()
{
};
}
package com.bebig.dao;
import com.bebig.model.User;

public interface UserDAO
{
public void save(User u);
public void deleteById(int id);
}
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!");
}
}
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;
}
}
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.");
}
}
<?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" />
<!-- 動態(tài)代理Annotation編寫方法 -->
<aop:aspectj-autoproxy/>
</beans>
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對象來創(chuàng)建代理對象
UserService service = (UserService) cxt.getBean("userService");
service.add(new User());
service.deleteById(1);
}
}

