实现步骤:
1、通过MethodBeforeAdivice接口实现前置增强处理
1 public class ServiceBeforeAdvisor implements MethodBeforeAdvice { 2 private Logger logger = Logger.getLogger(ServiceBeforeAdvisor.class); 3 @Override 4 public void before(Method method, Object[] args, Object target) 5 throws Throwable { 6 logger.info("启动事务"); 7 logger.info("连接点对象:"+target.getClass().getSimpleName()); 8 logger.info("连接点方法:"+method.getName()); 9 logger.info("连接点方法参数:"+args[0]); 10 11 } 12 13 }
2、使用<aop:advisor>标签织入增强处理
1 //注意:advisor要放在aspect前面 2 <bean id="userService" class="com.pb.service.UserService"></bean> 3 <bean id="serviceBeforeAdvisor" class="com.pb.aop.ServiceBeforeAdvisor"></bean> 4 <aop:config> 5 <aop:pointcut expression="execution(public * com.pb.service.*.*(..))" id="servicePointcut"/> 6 <aop:advisor advice-ref="serviceBeforeAdvisor" pointcut-ref="servicePointcut"/> 7 </aop:config>
测试类
1 public class Test { 2 public static void main(String[] args) { 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); 4 UserService service = (UserService)context.getBean("userService"); 5 service.addUser(new User()); 6 } 7 }
时间: 2024-11-04 11:52:36