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>
分享到:
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
spring的Annotation注解.
Spring的Annotation配置相关讲义
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
spring annotation 从入门到精通
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
NULL 博文链接:https://wolfsoft.iteye.com/blog/799226
SpringAnnotation