spring-基于注解的aop开发(快速入门)
生活随笔
收集整理的這篇文章主要介紹了
spring-基于注解的aop开发(快速入门)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
步驟:
1.導(dǎo)入坐標(biāo)
2.創(chuàng)建目標(biāo)類和接口(內(nèi)部有切點)
public interface TargetInterface {public void save(); } @Component public class Target implements TargetInterface{public void save() {System.out.println("save running....");} }3.創(chuàng)建切面類(內(nèi)部有增強(qiáng)方法)
@Component @Aspect //標(biāo)注當(dāng)前aspect是切面類 public class MyAspect {//配置織入關(guān)系 value=切點表達(dá)式@Before(value = "execution(public void com.hao.anno.Target.save())")public void before(){System.out.println("前置增強(qiáng)...");}public void afterReturning(){System.out.println("后置增強(qiáng)...");}public Object around(ProceedingJoinPoint point) throws Throwable { //切入點System.out.println("環(huán)繞前增強(qiáng)...");//切點方法Object proceed = point.proceed();System.out.println("環(huán)繞后增強(qiáng)...");return proceed;} }4.將目標(biāo)類和切面類的對象創(chuàng)建權(quán)交給spring
5.在切面類中使用注解配置織入關(guān)系
第三步和第二步已經(jīng)實現(xiàn)
6.在配置文件中開啟組件掃描和aop自動代理
7.測試
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContextanno.xml") public class AnnoTest {@Autowiredprivate TargetInterface target;@Testpublic void test2(){target.save();}}結(jié)果:
前置增強(qiáng)…
save running…
總結(jié)
以上是生活随笔為你收集整理的spring-基于注解的aop开发(快速入门)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring-xml实现aop-通知的种
- 下一篇: 编程式事务控制相关对象