SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect相关的知识,希望对你有一定的参考价值。

一、

技术分享

1.

package concert;

public interface CriticismEngine {
    public String getCriticism();
}

2.

 1 package concert;
 2 
 3 public class CriticismEngineImpl implements CriticismEngine {
 4     public CriticismEngineImpl() {}
 5     
 6     // injected
 7     private String[] criticismPool;
 8     
 9     public String getCriticism() {
10         int i = (int) (Math.random() * criticismPool.length);
11         return criticismPool[i];
12     }
13 
14     public void setCriticismPool(String[] criticismPool) {
15         this.criticismPool = criticismPool;
16     }
17 }

3.

 1 package concert;
 2 
 3 public class /*aspect*/ CriticAspect {
 4     public CriticAspect() {}
 5 //     pointcut performance() : execution(* perform(..));
 6 //     afterReturning() : performance() {
 7 //     System.out.println(criticismEngine.getCriticism());
 8 //     }
 9     private CriticismEngine criticismEngine;
10 
11     public CriticismEngine getCriticismEngine() {
12         return criticismEngine;
13     }
14 
15     public void setCriticismEngine(CriticismEngine criticismEngine) {
16         this.criticismEngine = criticismEngine;
17     }
18 }

4.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10 <bean id="criticismEngine" class="concert.CriticismEngineImpl">
11     <property name="criticismPool">
12         <list>
13             <value>Worst performance ever!</value>
14             <value>I laughed, I cried, then I realized I was at the wrong show.</value>
15             <value>A must see show!</value>
16         </list>
17     </property>
18 </bean>
19 <bean class="concert.CriticAspect" factory-method="aspectOf">
20     <property name="criticismEngine" ref="criticismEngine" />
21 </bean>
22 
23 </beans>

5.测试

package concert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-idol.xml")
public class AspectTest {
  @Autowired
  ApplicationContext context;

  @Test
  public void injectAspectJ() throws Exception {
    CriticAspect criticAspect = (CriticAspect) context.getBean("criticAspect");
    criticAspect.getCriticismEngine().getCriticism();
  }

}

例子跑不通

 1 严重: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]4657104d] to prepare test instance [[email protected]]
 2 java.lang.IllegalStateException: Failed to load ApplicationContext
 3     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
 4     at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
 5     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
 6     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
 7     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
 8     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
 9     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
10     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
11     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
12     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
13     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
14     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
15     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
16     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
17     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
18     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
19     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
20     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
21     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
22     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
23     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
24     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
25     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
26     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
27     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
28     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
29 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘concert.CriticAspect#0‘ defined in class path resource [spring-idol.xml]: No matching factory method found: factory method ‘aspectOf()‘. Check that a method with the specified name exists and that it is static.
30     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:549)
31     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
32     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
33     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
34     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
35     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
36     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
37     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
38     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
39     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
40     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
41     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
42     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
43     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
44     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
45     at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
46     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
47     at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
48     ... 25 more

 

以上是关于SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect的主要内容,如果未能解决你的问题,请参考以下文章

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-006-定义切面使用xml

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@ScopeProxyMode