`

spring annotation

阅读更多
Eg1.
package com.proxy.aop;

public class Human implements Sleepable{
   

   public void sleep(){
      System.out.println("睡觉了!梦中自有颜如玉!");
   }

}
package com.proxy.aop;

public interface Sleepable{
 
    void sleep(); 
}
package com.proxy.aop;
import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

    public void before(Method mtd, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("通常情况下睡觉之前要脱衣服!");
    }

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("起床后要先穿衣服!");
    }
    
}
package com.proxy.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {

    public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");
        sleeper.sleep();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<!-- DefaultPointcutAdvisor -->
<!--  
	<bean id="sleepHelper" class="com.proxy.aop.SleepHelper">
	</bean>
	<bean id="human" class="com.proxy.aop.Human"></bean>
	<bean id="spleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
		<property name="pattern" value=".*sleep" />
	</bean>
	<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="sleepHelper" />
		<property name="pointcut" ref="spleepPointcut" />
	</bean>
	<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="human" />
		<property name="interceptorNames" value="sleepHelperAdvisor" />
		<property name="proxyInterfaces" value="com.proxy.aop.Sleepable" />
	</bean>
-->

<!-- RegexpMethodPointcutAdvisor -->
	<bean id="sleepHelper" class="com.proxy.aop.SleepHelper">
	</bean>
	<bean id="human" class="com.proxy.aop.Human"></bean>
	<bean id="sleepAdvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice" ref="sleepHelper" />
		<property name="pattern" value=".*sleep" />
	</bean>
	<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="human" />
		<property name="interceptorNames" value="sleepAdvisor" />
		<property name="proxyInterfaces" value="com.proxy.aop.Sleepable" />
	</bean>
</beans>

Eg 2 :auto proxy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="sleepHelper" class="com.proxy.aop.SleepHelper">
	</bean>
	<bean id="sleepAdvisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice" ref="sleepHelper" />
		<property name="pattern" value=".*sleep" />
	</bean>
	<bean id="human" class="com.proxy.aop.Human">
	</bean>
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>

Eg 3. spring aop annotation
@Service("human")
public class Human implements Sleepable

package com.proxy.aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Controller;
@Aspect//声明切面
@Controller//把类交给spring管理
public class SleepHelper {

    public SleepHelper(){
        
    }
    
    @Pointcut("execution(* *.sleep())")
    public void sleeppoint(){}
    
    @Before("sleeppoint()")
    public void beforeSleep(){
        System.out.println("睡觉前要脱衣服!");
        throw new SecurityException("Exception");
    }
    
    @AfterReturning("sleeppoint()")
    public void afterSleep(){
        System.out.println("睡醒了要穿衣服!");
    }
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:component-scan base-package="com.proxy.aop" />
	<aop:aspectj-autoproxy />
	
</beans>
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics