在傳統的基於代理類的AOP實現中,每一個代理都是經過ProxyFactoryBean織入切面代理,在實際開發中,很是多的Bean每一個都配置ProxyFactoryBean開發維護量巨大。
解決方案:自動建立代理spring
建立StuDao接口和接口實現類StuDaoImpl
StuDao接口,代碼示例以下:app
public interface StuDao { public void save(); public void modify(); public void delete(); }
StuDaoImpl實現類,代碼示例以下:ide
public class StuDaoImpl implements StuDao { @Override public void save() { System.out.println("保存"); } @Override public void modify() { System.out.println("修改"); } @Override public void delete() { System.out.println("刪除"); } }
建立CustomDao類,演示不使用接口的實例方式測試
public class CustomDao { public void add(){ System.out.println("添加客戶"); } public void modify(){ System.out.println("修改客戶"); } }
建立切面類,實現加強方法spa
public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置加強========"); } }
在applicationContext.xml中配置代理
<!--配置目標類--> <bean id="stuDao" class="aop.StuDaoImpl"></bean> <bean id="customDao" class="aop.CustomDao"></bean> <!--配置加強--> <bean id="myBeforeAdvice" class="aop.MyBeforeAdvice"></bean> <!--配置自動建立代理--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="*Dao"></property> <property name="interceptorNames" value="myBeforeAdvice"></property> </bean>
建立測試類日誌
@Test public void demo(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); StuDao stuDao = (StuDao) app.getBean("stuDao"); stuDao.save(); stuDao.modify(); stuDao.delete(); CustomDao customDao = (CustomDao) app.getBean("customDao"); customDao.add(); customDao.modify(); }
運行結果code
StuDao接口,代碼示例以下:xml
public interface StuDao { public void save(); public void modify(); public void delete(); }
StuDaoImpl實現類,代碼示例以下:blog
public class StuDaoImpl implements StuDao { @Override public void save() { System.out.println("保存"); } @Override public void modify() { System.out.println("修改"); } @Override public void delete() { System.out.println("刪除"); } }
建立CustomDao類,演示不使用接口的實例方式
public class CustomDao { public void add(){ System.out.println("添加客戶"); } public void modify(){ System.out.println("修改客戶"); } }
建立切面類,實現環繞加強
public class MyAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation m) throws Throwable { //加強方法 check(); //執行目標方法 Object obj = m.proceed(); //加強方法 log(); return obj; } public void check(){ System.out.println("模擬權限控制============"); } public void log(){ System.out.println("模擬日誌記錄============"); } }
在applicationContext.xml中配置
<!--配置目標類--> <bean id="stuDao" class="aop.StuDaoImpl"></bean> <bean id="customDao" class="aop.CustomDao"></bean> <!--配置加強--> <bean id="myAroundAdvice" class="aop.MyAspect"></bean> <!--配置切面--> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!--配置要加強的方法,\. 表示轉義字符--> <property name="pattern" value="aop\.CustomDao\.add"></property> <property name="advice" ref="myAroundAdvice"></property> </bean> <!--配置基於切面自動建立代理--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
建立測試類
@Test public void demo(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); StuDao stuDao = (StuDao) app.getBean("stuDao"); stuDao.save(); stuDao.modify(); stuDao.delete(); CustomDao customDao = (CustomDao) app.getBean("customDao"); customDao.add(); customDao.modify(); }
運行結果