spring学习---2013-08-08
public class LogInterceptor implements InvocationHandler { private Object target; public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {this.beforeMethod();method.invoke(target,args);return null;} public void beforeMethod(){System.out.println("start now...");}public void setTarget(Object target) {this.target = target;}public Object getTarget() {return target;}}
?
//测试//在执行userDao.save()之前 执行了LogInterceptor.beforeMethod()//实际产生的类是名为proxy$0的代理类 @Testpublic void testProxy() throws Exception{UserDao userDao = new UserDaoImpl();LogInterceptor li = new LogInterceptor();li.setTarget(userDao);UserDao userDaoProxy = (UserDao)Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(),li);userDaoProxy.save(new User());}?
?
spring中
1、annotation(@注解的意思...文档)
2、xml配置
?
1、
xml文件
<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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="com.bjsxt"></context:component-scan> <aop:aspectj-autoproxy/> <bean id="u" init-method="init"> <property name="userDao" ref="u"/></bean></beans>
?
@Aspect@Component public class LogInterceptor {@Pointcut("execution(public void com.bjsxt.dao.impl.UserDaoImpl.save(com.bjsxt.model.User))")public void myMethod(){};@Before("myMethod()") public void beforeMethod(){System.out.println("start now...");}@After("myMethod()") public void afterMethod(){System.out.println("end now...");}@Around("myMethod()")public void aroundMethod(ProceedingJoinPoint pjp)throws Throwable{System.out.println("method start now...");pjp.proceed();System.out.println("method end now...");}}?
@Testpublic void testAdd() throws Exception {ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("bean.xml");UserService service = (UserService)factory.getBean("userService");User u = new User();service.add(u);service.getNames();}?
<?xml version="1.0" encoding="UTF-8"?><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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="com.bjsxt"></context:component-scan> <!-- aop:aspectj-autoproxy--> <bean id="u" init-method="init"> <property name="userDao" ref="u"/></bean><bean id="LogInterceptor" ref="LogInterceptor"><aop:pointcutexpression="execution(public void com.bjsxt.dao.impl.UserDaoImpl.save(com.bjsxt.model.User))"id="serviePointCut" /><aop:around method="aroundMethod"pointcut-ref="serviePointCut"></aop:around></aop:aspect></aop:config></beans>? public class LogInterceptor { public void beforeMethod(){System.out.println("start now...");}public void afterMethod(){System.out.println("end now...");}public void aroundMethod(ProceedingJoinPoint pjp)throws Throwable{System.out.println("method start now...");pjp.proceed();System.out.println("method end now...");}}?@Testpublic void testAdd() throws Exception {ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("bean.xml");UserService service = (UserService)factory.getBean("userService");User u = new User();service.add(u);service.getNames();}?jar包 aspectjrt.jar? aspectjweaver.jar