美文网首页
aop全自动编程

aop全自动编程

作者: 神豪VS勇士赢 | 来源:发表于2018-08-03 20:24 被阅读13次

目标:第三大步实现不需要手动配置。
只需要把通知和切入点关联形成切面就可以了。
从spring容器获得目标类,配置aop将自动生成代理。


<aop:config>
<aop:pointcut id="myPoint" expression="execution( * com.zyh.service...(..))"/>
<aop:advisor advice-ref="circleLog" pointcut-ref="myPoint"/>
</aop:config>

切入点表达式部分讲解:


image.png

开发流程
第一步:创建目标类

目标类为 AutoServiceImpl 实现 AutoService
@Service("autoService")
public class AutoServiceImpl implements AutoService {

@Qualifier("autoDao")
@Autowired
private AutoDaoImpl autoDao;
@Override
public void addAuto() {
    autoDao.addAuto();
}

}

@Repository("AnnotationDaoImpl")
public class AnnotationDaoImpl implements AnnotationDao {
@Override
public void addAnnotation() {
Log.info("我是AnnotationDaoImpl 里面的方法");
}

@Override
public String returnAnnotation() {
    return  "我是 我是AnnotationDaoImpl 里面返回数据的一个方法";
}

}

第二步:创建通知类

@Component("circleLog")
public class LogCircleAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Log.info("环绕前");
Object proceed = invocation.proceed();
Log.info("环绕后" );
return proceed;
}
}

第三步:AOP全自动编程

<aop:config>
    <aop:pointcut id="myPoint" expression="execution( * com.zyh.service..*.*(..))"/>
    <aop:advisor advice-ref="circleLog" pointcut-ref="myPoint"/>
</aop:config>

第四步:测试

@Test
public void testAutoService(){
    ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    AutoService autoService = (AutoService) classPathXmlApplicationContext.getBean("autoService");
    autoService.addAuto();
}

输出结果如下所示:


image.png

可以根据修改如下引用进行修改 通知类 : 就不一一列举了


image.png

相关文章

网友评论

      本文标题:aop全自动编程

      本文链接:https://www.haomeiwen.com/subject/pibuvftx.html