自动创建代理-DefaultAdvisorAutoProxyCreator

释放双眼,带上耳机,听听看~!

在上一篇中我们通过BeanNameAutoProxyCreator类中的beanNames属性来自动的匹配需要增强的对象,那么在这一篇中我们将使用一种更智能的类它匹配我们需要的增强的对象,也就是DefaultAdvisorAutoProxyCreator类,该类甚至可以不设置需要匹配的增强对象,即可对对象进行增强。下面我们看一下该类的具体使用。

public class UserService{

  public void login(String username, String password) {
    System.out.println(String.format("用户登陆: username: %s password: %s", username, password));
  }

  public void register(String username, String nickName, String password) {
    System.out.println(String.format("用户注册: username: %s nickName: %s password: %s", username, nickName, password));
  }

}
public class UserServiceBeforeAdvice implements MethodBeforeAdvice{

  @Override
  public void before(Method method, Object[] objects, Object o) throws Throwable {
    System.out.println(String.format("spring增强: %s", method.getName()));
    TransactionManager.beginTransaction();
  }
}
<bean id="userService" class="com.jilinwula.spring.aop.UserService"/>
<bean id="userServiceBeforeAdvice" class="com.jilinwula.spring.aop.UserServiceBeforeAdvice"/>
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
    p:patterns=".*reg.*"
    p:advice-ref="userServiceBeforeAdvice"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
@Test
public void test() {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
  UserService userService = applicationContext.getBean("userService", UserService.class);
  userService.register("admin","吉林乌拉","jilinwula");
}
spring增强: register
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新消息 消息中心
有新私信 私信列表
搜索