spring開發_Annotation_AOP_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.ClassPathXmlApplicationContext;
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 ClassPathXmlApplicationContext("beans.xml");
18 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.xml

要添加:

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

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

請看下面:

 1 <?xml version="1.0" encoding="GBK"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:aop="http://www.springframework.org/schema/aop"
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的XML Shemale配置方式,可以采用如下配置 : <bean
19 class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
20 -->
21
22 <bean id="cat" class="com.b510.service.impl.CatServiceBean">
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.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Tue Mar 13 20:33:46 CST 2012]; root of context hierarchy
3 2012-3-13 20:33:46 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-13 20:33:56 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@12a3722
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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM