日期:2014-05-17 浏览次数:20902 次
?
下面就是一个AspectJ产生代理的一个例子
?
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.lbx"/> <aop:aspectj-autoproxy /> </beans>
?
注意:一定要加@Componnent
package com.lbx.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LogInterceptor { @Before("execution(public void com.lbx.dao.impl.UserDaoImpl.add(com.lbx.po.User))") public void before(){ System.out.println("LogInterceptor.before()");; } }
?
?
?
package com.lbx.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.lbx.dao.UserDao; import com.lbx.po.User; import com.lbx.service.UserService; @Component("userService") public class UserServiceImpl implements UserService { private UserDao userDao; public void add(User user) { userDao.add(user); } public UserDao getUserDao() { return userDao; } @Resource(name="userDao") //最好用@Resource 不要用@Autowired public void setUserDao( UserDao userDao) { this.userDao = userDao; } }
?
package com.lbx.dao.impl; import org.springframework.stereotype.Component; import com.lbx.dao.UserDao; import com.lbx.po.User; @Component("userDao") //UserDaoImpl public class UserDaoImpl implements UserDao { public void add(User user) { // TODO Auto-generated method stub System.out.println("一个user添加进来"); } }
??
?