`
暮云凌轩
  • 浏览: 9133 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

spring--aop-动态代理---2013-08-08

    博客分类:
  • ssh
 
阅读更多

AOP

增加逻辑

1、写死在原来程序上 不灵活

2、继承的方式 不够灵活 耦合太强 且没加一段逻辑 加一层继承

3、组合的方式

 

将逻辑单独出来

然后动态代理

 

 动态代理实现

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的代理类 
@Test
	public 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" class="com.bjsxt.dao.impl.UserDaoImpl"> 
    </bean>   
	<bean id="userService" class="com.bjsxt.service.UserService" 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...");
		
	}
}

 

@Test
	public 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();
	}

 

2、
<?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" class="com.bjsxt.dao.impl.UserDaoImpl"> 
    </bean>   
	<bean id="userService" class="com.bjsxt.service.UserService" init-method="init">
		  <property name="userDao" ref="u"/>	
	</bean>
	<bean id="LogInterceptor" class="com.bjsxt.aop.LogInterceptor"/> 
	<aop:config>
		<aop:aspect id="logaspect" ref="LogInterceptor">
			<aop:pointcut
				expression="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...");		
	}
}
 
@Test
	public 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics