你的位置:首页 > 软件开发 > Java > spring开发_Annotation_AOP_Before增强处理

spring开发_Annotation_AOP_Before增强处理

发布时间:2012-03-13 21:00:06
java_spring_annotatin_before_advice_aspcet Before增强处理

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112691.html

在此项目中除了要引入基本springjar包:

spring.jar和commons-logging.jar

还需要引入:

aspectjweaver.jar

aspectjrt.jar

两个jar包!!!

/spring_2000_aop_annotation/src/com/b510/app/test/SpringTest.java

 1 package com.b510.app.test;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPath 5
6 import com.b510.service.AnimalService;
7
8 /**
9 * 测试类
10 *
11 * @author Hongten
12 *
13 */
14 public class SpringTest {
15
16 public static void main(String[] args) {
17 ApplicationContext act = new ClassPath18 AnimalService cat = (AnimalService) act.getBean("cat");
19 // 调用printResult()方法
20 cat.printResult();
21 // 调用printHobby()方法
22 cat.printHobby();
23 }
24 }

/spring_2000_aop_annotation/src/com/b510/aspect/MyBeforeAdvice.java

 1 package com.b510.aspect;
2
3 import org.aspectj.lang.annotation.Aspect;
4 import org.aspectj.lang.annotation.Before;
5
6 /**
7 * 定义一个Before增强处理类
8 *
9 * @author Hongten
10 *
11 */
12 // 定义切面
13 @Aspect
14 public class MyBeforeAdvice {
15
16 private int i = 0;
17
18 /**
19 * 搜索com.b510.service.impl包下的所有类及其类的方法,作为本方法的切入点(Jionpoint)
20 */
21 @Before("execution(* com.b510.service.impl.*.*(..))")
22 public void getVisits() {
23 System.out.println("自定义切面MyBeforeAdvice类的getVisits()方法的执行此数为" + (++i));
24 }
25 }

/spring_2000_aop_annotation/src/com/b510/service/AnimalService.java

 1 package com.b510.service;
2
3 /**
4 * 定义动物接口
5 *
6 * @author Hongten
7 *
8 */
9 public interface AnimalService {
10 /**
11 * 打印信息
12 */
13 public void printResult();
14
15 /**
16 * 打印兴趣爱好
17 */
18 public void printHobby();
19 }

/spring_2000_aop_annotation/src/com/b510/service/impl/CatServiceBean.java

 1 package com.b510.service.impl;
2
3 import org.springframework.stereotype.Component;
4
5 import com.b510.service.AnimalService;
6 /**
7 * 定义一个动物的实现类CatServiceBean
8 * @author Hongten
9 *
10 */
11 @Component
12 public class CatServiceBean implements AnimalService {
13
14 /**
15 * 名字
16 */
17 private String name;
18 /**
19 * 兴趣爱好
20 */
21 private String hobby;
22
23 public String getHobby() {
24 return hobby;
25 }
26
27 public String getName() {
28 return name;
29 }
30
31 @Override
32 public void printHobby() {
33 System.out.println("我的兴趣爱好是" + getHobby());
34 }
35
36 @Override
37 public void printResult() {
38 System.out.println("大家好,我是" + getName());
39 }
40
41 public void setHobby(String hobby) {
42 this.hobby = hobby;
43 }
44
45 public void setName(String name) {
46 this.name = name;
47 }
48 }

/spring_2000_aop_annotation/src/beans.

要添加:

http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

请看下面:

 1 <? 2 <beans  3      4      5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context-3.0.xsd
9 http://www.springframework.org/schema/aop
10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
11 <context:component-scan base-package="com.b510.aspect,com.b510.service">
12 <context:include-filter type="annotation"
13 expression="org.aspectj.lang.annotation.Aspect" />
14 </context:component-scan>
15 <!-- 启动@AspectJ支持 -->
16 <aop:aspectj-autoproxy />
17 <!--
18 不适用spring的19 />
20 -->
21
22 <bean id="cat" >
23 <property name="name" value="加菲" />
24 <property name="hobby" value="吃,喝,睡觉" />
25 </bean>
26 </beans>

运行结果:

 1 2012-3-13 20:33:46 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPath 3 2012-3-13 20:33:46 org.springframework.beans.factory. 4 信息: Loading class path resource [beans. 5 2012-3-13 20:33:56 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPath 7 2012-3-13 20:33:56 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12a3722: defining beans [myBeforeAdvice,catServiceBean,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,cat]; root of factory hierarchy
9 自定义切面MyBeforeAdvice类的getVisits()方法的执行此数为1
10 大家好,我是加菲
11 自定义切面MyBeforeAdvice类的getVisits()方法的执行此数为2
12 我的兴趣爱好是吃,喝,睡觉

总结:每次运行的时候,都要等上12秒钟左右,有结果可以看出,Before增强处理的运行方式:

@Before("execution(* com.b510.service.impl.*.*(..))")

搜索com.b510.service.impl包下的所有类及其类的方法,作为本方法的切入点(Jionpoint)

原标题:spring开发_Annotation_AOP_Before增强处理

关键词:java,spring,annotation,aspect,before,@Before

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。