SpringMvc 面向切面1
1.配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="log" class="com.gc.action.LogAround"></bean> <bean id="timeBook" class="com.gc.action.TimeBook"></bean> <!-- logaround --> <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> //该配置也可以去掉 <property name="proxyInterfaces"> <value>com.gc.action.ITimeBook</value> </property> <property name="target"> <ref bean="timeBook"></ref> </property> <!-- 指定要代理的类 --> <property name="interceptorNames"> <list> <value>log</value> </list> </property> </bean> </beans>
2.LogAround
package com.gc.action; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LogAround implements MethodInterceptor { @Override public Object invoke(MethodInvocation arg0) throws Throwable { // TODO Auto-generated method stub Object result=null; try { System.out.println("aop 之前"); result=arg0.proceed(); return result; } finally{ System.out.println("aop 之后"); } } }
3.TimeBook
package com.gc.action; public class TimeBook implements ITimeBook { @Override public void doAudit(String name) { // TODO Auto-generated method stub System.out.println("laaaaaaaaaaaaa"); } }
4.ITimeBook
package com.gc.action; public interface ITimeBook { public void doAudit(String name); }
5. 测试
ApplicationContext context=new FileSystemXmlApplicationContext("config-aop.xml"); ITimeBook tb=(ITimeBook)context.getBean("logProxy"); tb.doAudit("xiaox");