spring的一些总结3
2009-01-15 10:04
225 查看
5.实现aop的例子
1.首先先来点预备类,咱定义一个表演的接口,代码如下:
Java代码
public interface Perform {
void perform();
}
public interface Perform { void perform(); }
就一个方法,表演节目,然后再定义2个实现类,ShowBoy和ShowGirl
Java代码
public class ShowBoy implements Perform{
public void perform() {
System.out.println("表演街舞");
}
}
public class ShowGirl implements Perform{
public void perform() {
System.out.println("表演肚皮舞");
}
}
public class ShowBoy implements Perform{ public void perform() { System.out.println("表演街舞"); } } public class ShowGirl implements Perform{ public void perform() { System.out.println("表演肚皮舞"); } }
这些要bean要让spring来帮我们管理,所以要把他们写到spring的配置文件中。现在先不写,一会统一写。
现在该干正事了,首先就是定义通知,也就是说,想在表演节目的时候插入什么事情呢?
我们定义一个观众类,让他们在表演的时候,做一些动作。
Java代码
public class Audience {
public Audience() {
}
public void takeSeat(){
System.out.println("观众们找到自己的座位,都坐下来了");
}
public void turnOffMobilePhone(){
System.out.println("请所有观众确定手机已经关闭");
}
public void appluad(){
System.out.println("观众们大声鼓掌,啪啦啪啦啪啦");
}
public void demandRefund(){
System.out.println("演的太差了,我们要退钱!");
}
}
public class Audience { public Audience() { } public void takeSeat(){ System.out.println("观众们找到自己的座位,都坐下来了"); } public void turnOffMobilePhone(){ System.out.println("请所有观众确定手机已经关闭"); } public void appluad(){ System.out.println("观众们大声鼓掌,啪啦啪啦啪啦"); } public void demandRefund(){ System.out.println("演的太差了,我们要退钱!"); } }
从这个类定义的方法大概可以看出,找座位和关手机应该是表演前发生的,鼓掌应该是表演后发生的,而要求退钱应该是表演发生意外后发生的。
总结一下,Spring的aop通知有5种形式
Before:org.springframework.aop.MethodBeforeAdvice,这个接口代表方法之前。
After-returning: org.springframework.aop.AfterReturningAdvice,这个代表返回后
After-throwing:org.springframework.aop.ThrowsAdvice,代表抛出异常后。
Around:org.aopalliance.intercept.MethodInterceptor,代表一个方法的周围。
Introduction:org.springframework.aop.IntroductionInterceptor,代表引入
现在来定义真正的通知,通知不是包含应该干什么和何时干吗,那就写把。
Java代码
public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{
private Audience audience;
public void setAudience(Audience audience) {
this.audience = audience;
}
public void before(Method method, Object[] objects, Object o) throws Throwable {
audience.takeSeat();
audience.turnOffMobilePhone();
}
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
audience.appluad();
}
public void afterThrowing(Throwable throwable){
audience.demandRefund();
}
}
public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{ private Audience audience; public void setAudience(Audience audience) { this.audience = audience; } public void before(Method method, Object[] objects, Object o) throws Throwable { audience.takeSeat(); audience.turnOffMobilePhone(); } public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { audience.appluad(); } public void afterThrowing(Throwable throwable){ audience.demandRefund(); } }
其中该干什么在 Audience中定义的,而什么时候,就是这些接口所实现的方法,带有before,after等。都表明了什么时候。
有了通知,就该定义切点了把,切点直接在配置文件里定义,这时,也顺便把通知和目标类一起定义到xml文件中。切点是干嘛的,切点是定义应该在哪些方法用切面的,他有2种定义方式,一种是用正则表达式,来匹配想要的方法,另一种是用aspectJ切点表达式。
<!--定义目标类,也就是想被织入通知的类-->
Xml代码
<bean id="showBoy" class="com.spring.springcase.ShowBoy"/>
<bean id="showGirl" class="com.spring.springcase.ShowGirl"/>
<!--定义了通知中的功能,此类做为通知的从属类-->
<bean id="audience" class="com.spring.springcase.Audience"/>
<!--定义通知-->
<bean id="audienceAdvice" class="com.spring.springcase.AudienceAdvice">
<property name="audience" ref="audience"/>
</bean>
<!--定义切点,声明想要的方法:spring提供的定义切点方式-->
<bean id="springpointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*perform"/>
</bean>
<!--定义切点,aspectJ定义的切点方式-->
<bean id="asPectJpoincut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
<property name="expression" value="execution(* Performer+.perform(..))"/>
</bean>
<bean id="showBoy" class="com.spring.springcase.ShowBoy"/> <bean id="showGirl" class="com.spring.springcase.ShowGirl"/> <!--定义了通知中的功能,此类做为通知的从属类--> <bean id="audience" class="com.spring.springcase.Audience"/> <!--定义通知--> <bean id="audienceAdvice" class="com.spring.springcase.AudienceAdvice"> <property name="audience" ref="audience"/> </bean> <!--定义切点,声明想要的方法:spring提供的定义切点方式--> <bean id="springpointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*perform"/> </bean> <!--定义切点,aspectJ定义的切点方式--> <bean id="asPectJpoincut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut"> <property name="expression" value="execution(* Performer+.perform(..))"/> </bean>
1.首先先来点预备类,咱定义一个表演的接口,代码如下:
Java代码
public interface Perform {
void perform();
}
public interface Perform { void perform(); }
就一个方法,表演节目,然后再定义2个实现类,ShowBoy和ShowGirl
Java代码
public class ShowBoy implements Perform{
public void perform() {
System.out.println("表演街舞");
}
}
public class ShowGirl implements Perform{
public void perform() {
System.out.println("表演肚皮舞");
}
}
public class ShowBoy implements Perform{ public void perform() { System.out.println("表演街舞"); } } public class ShowGirl implements Perform{ public void perform() { System.out.println("表演肚皮舞"); } }
这些要bean要让spring来帮我们管理,所以要把他们写到spring的配置文件中。现在先不写,一会统一写。
现在该干正事了,首先就是定义通知,也就是说,想在表演节目的时候插入什么事情呢?
我们定义一个观众类,让他们在表演的时候,做一些动作。
Java代码
public class Audience {
public Audience() {
}
public void takeSeat(){
System.out.println("观众们找到自己的座位,都坐下来了");
}
public void turnOffMobilePhone(){
System.out.println("请所有观众确定手机已经关闭");
}
public void appluad(){
System.out.println("观众们大声鼓掌,啪啦啪啦啪啦");
}
public void demandRefund(){
System.out.println("演的太差了,我们要退钱!");
}
}
public class Audience { public Audience() { } public void takeSeat(){ System.out.println("观众们找到自己的座位,都坐下来了"); } public void turnOffMobilePhone(){ System.out.println("请所有观众确定手机已经关闭"); } public void appluad(){ System.out.println("观众们大声鼓掌,啪啦啪啦啪啦"); } public void demandRefund(){ System.out.println("演的太差了,我们要退钱!"); } }
从这个类定义的方法大概可以看出,找座位和关手机应该是表演前发生的,鼓掌应该是表演后发生的,而要求退钱应该是表演发生意外后发生的。
总结一下,Spring的aop通知有5种形式
Before:org.springframework.aop.MethodBeforeAdvice,这个接口代表方法之前。
After-returning: org.springframework.aop.AfterReturningAdvice,这个代表返回后
After-throwing:org.springframework.aop.ThrowsAdvice,代表抛出异常后。
Around:org.aopalliance.intercept.MethodInterceptor,代表一个方法的周围。
Introduction:org.springframework.aop.IntroductionInterceptor,代表引入
现在来定义真正的通知,通知不是包含应该干什么和何时干吗,那就写把。
Java代码
public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{
private Audience audience;
public void setAudience(Audience audience) {
this.audience = audience;
}
public void before(Method method, Object[] objects, Object o) throws Throwable {
audience.takeSeat();
audience.turnOffMobilePhone();
}
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
audience.appluad();
}
public void afterThrowing(Throwable throwable){
audience.demandRefund();
}
}
public class AudienceAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice{ private Audience audience; public void setAudience(Audience audience) { this.audience = audience; } public void before(Method method, Object[] objects, Object o) throws Throwable { audience.takeSeat(); audience.turnOffMobilePhone(); } public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { audience.appluad(); } public void afterThrowing(Throwable throwable){ audience.demandRefund(); } }
其中该干什么在 Audience中定义的,而什么时候,就是这些接口所实现的方法,带有before,after等。都表明了什么时候。
有了通知,就该定义切点了把,切点直接在配置文件里定义,这时,也顺便把通知和目标类一起定义到xml文件中。切点是干嘛的,切点是定义应该在哪些方法用切面的,他有2种定义方式,一种是用正则表达式,来匹配想要的方法,另一种是用aspectJ切点表达式。
<!--定义目标类,也就是想被织入通知的类-->
Xml代码
<bean id="showBoy" class="com.spring.springcase.ShowBoy"/>
<bean id="showGirl" class="com.spring.springcase.ShowGirl"/>
<!--定义了通知中的功能,此类做为通知的从属类-->
<bean id="audience" class="com.spring.springcase.Audience"/>
<!--定义通知-->
<bean id="audienceAdvice" class="com.spring.springcase.AudienceAdvice">
<property name="audience" ref="audience"/>
</bean>
<!--定义切点,声明想要的方法:spring提供的定义切点方式-->
<bean id="springpointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*perform"/>
</bean>
<!--定义切点,aspectJ定义的切点方式-->
<bean id="asPectJpoincut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
<property name="expression" value="execution(* Performer+.perform(..))"/>
</bean>
<bean id="showBoy" class="com.spring.springcase.ShowBoy"/> <bean id="showGirl" class="com.spring.springcase.ShowGirl"/> <!--定义了通知中的功能,此类做为通知的从属类--> <bean id="audience" class="com.spring.springcase.Audience"/> <!--定义通知--> <bean id="audienceAdvice" class="com.spring.springcase.AudienceAdvice"> <property name="audience" ref="audience"/> </bean> <!--定义切点,声明想要的方法:spring提供的定义切点方式--> <bean id="springpointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*perform"/> </bean> <!--定义切点,aspectJ定义的切点方式--> <bean id="asPectJpoincut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut"> <property name="expression" value="execution(* Performer+.perform(..))"/> </bean>
相关文章推荐
- spring常用的一些注解以及注解注入总结
- 使用Spring、Hibernate、Struts的一些错误总结(不断补充)
- spring的一些总结1
- spring常用的一些注解以及注解注入总结
- [Java面试五]Spring总结以及在面试中的一些问题.
- 使用Spring、Hibernate、Struts的一些错误总结(转)
- 关于在Spring4 jdbctemplate 遇到的一些基本问题总结
- Spring 3 AOP总结 (包含注解式AOP定义, poincut及advice的一些定义方式)
- Spring和Hibernate集成的HibernateTemplate的一些常用方法总结
- SpringBoot中的一些注解总结
- spring常用的一些注解以及注解注入总结
- springboot + swagger一些注解总结
- 看了《spring技术内幕》简单的图解总结下spring一些请求调用流程
- spring restful的一些个人总结
- spring的一些总结3
- spring常用的一些注解以及注解注入总结
- Spring和Hibernate集成的HibernateTemplate的一些常用方法总结
- spring一些总结
- spring常用的一些注解以及注解注入总结(二)
- Spring和Hibernate集成的HibernateTemplate的一些常用方法总结