微信網(wǎng)站設計價格網(wǎng)站流量分析的指標有哪些
目錄
Spring框架的AOP技術(shù)(注解方式)
通知類型
Spring框架的AOP技術(shù)(注解方式)
1. 步驟一:創(chuàng)建JavaWEB項目,引入具體的開發(fā)的jar包* 先引入Spring框架開發(fā)的基本開發(fā)包com.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org.apache.log4j-1.2.15.jarspring-beans-5.0.2.RELEASE.jarspring-context-5.0.2.RELEASE.jarspring-core-5.0.2.RELEASE.jarspring-expression-5.0.2.RELEASE.jar* 再引入Spring框架的AOP的開發(fā)包* spring的傳統(tǒng)AOP的開發(fā)的包* spring-aop-5.0.2.RELEASE.jar* com.springsource.org.aopalliance-1.0.0.jar* aspectJ的開發(fā)包* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar* spring-aspects-5.0.2.RELEASE.jar*如果是Maven項目,就引入下面的jar包坐標即可<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency>
2. 步驟二:創(chuàng)建Spring的配置文件,引入具體的AOP的schema約束<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"></beans>
? ? 3. 步驟三:創(chuàng)建包結(jié)構(gòu),編寫具體的接口和實現(xiàn)類* org.westos.demo1* CustomerDao -- 接口* CustomerDaoImpl -- 實現(xiàn)類 ? 4. 步驟四:將目標類配置到Spring中<bean id="customerDao" class="org.westos.demo1.CustomerDaoImpl"/> ? 5. 步驟五:定義切面類* 添加切面和通知的注解* @Aspect -- 定義切面類的注解* 通知類型(注解的參數(shù)是切入點的表達式)* @Before -- 前置通知* @AfterReturing -- 后置通知* @Around -- 環(huán)繞通知* @After -- 最終通知* @AfterThrowing -- 異常拋出通知* 具體的代碼如下@Aspectpublic class MyAspectAnno {@Before(value="execution(public void org.westos.demo1.CustomerDaoImpl.save())")public void log(){System.out.println("記錄日志...");}} ? 6. 步驟六:在配置文件中定義切面類<bean id="myAspectAnno" class="org.westos.demo1.MyAspectAnno"/> ? 7. 步驟七:在配置文件中開啟自動代理<aop:aspectj-autoproxy/> 放在最前面 ? 8. 完成測試@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo1 {@Resource(name="customerDao")private CustomerDao customerDao;@Testpublic void run1(){customerDao.save();customerDao.update();}}
通知類型
1. 通知類型* @Before -- 前置通知* @AfterReturing -- 后置通知* @Around -- 環(huán)繞通知(目標對象方法默認不執(zhí)行的,需要手動執(zhí)行)* @After -- 最終通知* @AfterThrowing -- 異常拋出通知 ? 2. 配置通用的切入點* 使用@Pointcut定義通用的切入點@Aspectpublic class MyAspectAnno {//隨便定義一個方法,上面用注解定義一個切入點@Pointcut(value="execution(public void org.westos.demo1.CustomerDaoImpl.save())")public void fn(){}//引用這個定義的切入點 格式:切面類名.方法名@Before(value="MyAspectAnno.fn()")public void log(){System.out.println("記錄日志...");}
/*** 環(huán)繞通知*/@Around(value="MyAspectAnno.fn()")public void around(ProceedingJoinPoint joinPoint){System.out.println("環(huán)繞通知1...");try {// 讓目標對象的方法執(zhí)行joinPoint.proceed();} catch (Throwable e) {e.printStackTrace();}System.out.println("環(huán)繞通知2...");}}