<!-- 拦截器1 --> <bean id="user" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.oys.zexaple.interceptor.IUser</value> </property> <property name="interceptorNames"> <list> <value>loginAdvice</value> </list> </property> <property name="target"> <ref bean="userTarget" /> </property> </bean> <!-- 拦截器2 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>helloSpeaker</value> <value>helloSpeaker2</value> </list> </property> <property name="interceptorNames"> <list> <value>logInterceptor</value> <value>agumentInterceptor</value> </list> </property> </bean> <!-- 拦截器3 事务 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="*">ISOLATION_SERIALIZABLE</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> <value>*DAO</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
拦截器1:
package com.oys.zexaple.interceptor; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; public abstract class BaseAdvice implements MethodBeforeAdvice, MethodInterceptor, AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args, Object target) { } public Object invoke(MethodInvocation invocation) throws Throwable { return invocation; } public void before(Method method, Object[] args, Object target) { } }
package com.oys.zexaple.interceptor; public interface IUser { public void Login(String username,String password); }
package com.oys.zexaple.interceptor; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInvocation; import org.springframework.stereotype.Repository; @Repository("loginAdvice") public class LoginAdvice extends BaseAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) { System.out.println("---------- 方法名:afterReturning ----------------"); // 将用户登录信息写入日志文件 } @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("---------- 方法名:invoke ----------------"); String username = invocation.getArguments()[0].toString(); String password = invocation.getArguments()[1].toString(); System.out.println(username+"===="+password); //在这里进行相关的验证操作 return invocation.proceed(); } @Override public void before(Method method, Object[] args, Object target) { System.out.println("---------- 方法名:before ----------------"); String username = (String) args[0]; String passowrd = (String) args[1]; // 在这里进行数据库查找操作 } }
package com.oys.zexaple.interceptor; import org.springframework.stereotype.Repository; @Repository("userTarget") public class UserImpl implements IUser { public void Login(String username, String password) { System.out.println("----------方法名:Login ----------------"); } }
拦截器2
package com.oys.zexaple.interceptor2; import org.springframework.stereotype.Service; @Service("helloSpeaker") public class HelloSpeaker implements IHello { public void sayHello(String name) { System.out.println("Hello," + name); } }
package com.oys.zexaple.interceptor2; import org.springframework.stereotype.Service; @Service("helloSpeaker2") public class HelloSpeaker2 implements IHello { public void sayHello(String name) { System.out.println("Welcome to world," + name); } }
package com.oys.zexaple.interceptor2; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.stereotype.Service; @Service("agumentInterceptor") public class InterceptorForAgument implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { String name = null; Object[] args = methodInvocation.getArguments(); for (int i = 0; i < args.length; i++) { if (args[i] instanceof String) { name = (String) args[i]; } } System.out.println("the argument is : " + name); Object result = methodInvocation.proceed(); System.out.println("the argument is proceed"); return result; } }
package com.oys.zexaple.interceptor2; public interface IHello { public void sayHello(String name); }
package com.oys.zexaple.interceptor2; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.stereotype.Service; @Service("logInterceptor") public class InterceptorForLog implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("Log start..."); Object result = null; result = methodInvocation.proceed(); System.out.println("Log end..."); return result; } }
测试代码:
package test; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.oys.base.domain.User; import com.oys.base.service.UserService; import com.oys.zexaple.aop.AServiceImpl; import com.oys.zexaple.aop.BServiceImpl; import com.oys.zexaple.interceptor.IUser; import com.oys.zexaple.interceptor2.IHello; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:conf/applicationContext.xml" }) //@ContextConfiguration(locations="classpath:conf/applicationContext.xml") // 加载配置 public class TestServiceTest { @Resource private UserService userService; @Autowired private IUser user; @Autowired private BServiceImpl bService; @Autowired private AServiceImpl aService; @Resource private IHello helloSpeaker; @Test public void findAllUser() { List<User> list=userService.findAllUser(); System.out.println(list.size()); } @Test public void testaop1(){ } @Test public void testaop2(){ bService.barB("msg", "type"); System.out.println("----------------------"); aService.barA(); } @Test public void interceptors(){ user.Login("username", "123456"); } @Test public void interceptors2(){ helloSpeaker.sayHello("longyg"); } }