我正在尝试实现一个简单的 Spring AOP 示例。
用例:
我有一个带有艺术家、标题等属性的光盘 (SgtPeppers) 类。它有一种播放特定曲目的方法。我创建了一个方面来计算播放曲目的次数。我有一个测试类来测试它。
问题:
光盘类与方面和测试类在不同的包中。使用 spring java config 方法配置 Bean。但是,自动装配似乎不适用于光盘类。它抛出 BeanCreationException。
请在下面找到代码: 光盘类(这里,它被配置为 SgtPeppers)
package com.springinaction.soundsystem.autoconfig;
import java.util.List;
public class SgtPeppers implements CompactDisc {
private String title, artist;
public SgtPeppers(String title, String artist) {
super();
this.title = title;
this.artist = artist;
}
public SgtPeppers() {
// TODO Auto-generated constructor stub
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
@Override
public void play() {
System.out.println("Playing "+title+" by "+ artist);
}
public void playTrack(int tracknumber) {
System.out.println("Playing track "+ tracknumber);
}
}
TrackCount 方面:
package com.springinaction.aspects.concert.trackCounter;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackCounter {
private Map<Integer,Integer> trackCounts = new HashMap<Integer, Integer>();
@Pointcut("execution(** com.springinaction.aspects.concert.trackCounter.SgtPeppers.playTrack(int)) && args(trackNumber)")
public void trackPlayed(int trackNumber){}
@Before("trackPlayed(trackNumber)")
public void countTrack(int trackNumber) {
int trackCount = getTrackCount(trackNumber);
trackCounts.put(trackNumber, trackCount+1);
}
public int getTrackCount(int trackNumber) {
return trackCounts.containsKey(trackNumber) ? trackCounts.get(trackNumber) : 0;
}
}
跟踪计数器配置:
package com.springinaction.aspects.concert.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.springinaction.aspects.concert.trackCounter.SgtPeppers;
import com.springinaction.aspects.concert.trackCounter.TrackCounter;
@Configuration
@EnableAspectJAutoProxy
public class TrackCounterConfig {
@Bean
public SgtPeppers sgtPeppers() {
SgtPeppers cd = new SgtPeppers("Sgt. Pepper's Lonely Hearts Club Band", "The Beatles");
return cd;
}
@Bean
public TrackCounter trackCounter() {
return new TrackCounter();
}
}
TrackCounterTest:
package com.springinaction.aspects.concert.trackCounter;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.springinaction.aspects.concert.config.TrackCounterConfig;
import com.springinaction.soundsystem.autoconfig.SgtPeppers;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TrackCounterConfig.class)
public class TrackCounterTest {
@Autowired
private TrackCounter trackCounter;
@Autowired
private SgtPeppers sgtPeppers;
@Test
public void testTrackCounter() {
sgtPeppers.playTrack(1);
sgtPeppers.playTrack(1);
sgtPeppers.playTrack(2);
sgtPeppers.playTrack(2);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(7);
sgtPeppers.playTrack(7);
assertEquals(2, trackCounter.getTrackCount(1));
assertEquals(2, trackCounter.getTrackCount(2));
assertEquals(4, trackCounter.getTrackCount(3));
assertEquals(2, trackCounter.getTrackCount(7));
}
}
在这里,SgtPeppers 类位于 com.springinaction.soundsystem.autoconfig 包中。TrackCounter 和 TrackCounterconfig 在 com.springinaction.aspects.concert.config 包中。我得到以下异常:
org.springframework.beans.factory.BeanCreationException:创建名为“com.springinaction.aspects.concert.trackCounter.TrackCounterTest”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.springinaction.soundsystem.autoconfig.SgtPeppers com.springinaction.aspects.concert.trackCounter.TrackCounterTest.sgtPeppers;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到 [com.springinaction.soundsystem.autoconfig.SgtPeppers] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释: org.springframework.beans.factory 中的 {@org.springframework.beans.factory.annotation.Autowired(required=true)}。RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com。 springinaction.soundsystem.autoconfig.SgtPeppers com.springinaction.aspects.concert.trackCounter.TrackCounterTest.sgtPeppers;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到 [com.springinaction.soundsystem.autoconfig.SgtPeppers] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509) 中的 {@org.springframework.beans.factory.annotation.Autowired(required=true)}。 beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290) ... 26 更多原因:org.springframework.beans .factory.NoSuchBeanDefinitionException:没有为依赖找到类型 [com.springinaction.soundsystem.autoconfig.SgtPeppers] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者。依赖注解:{@org.springframework.
注意:我已经尝试使用 basePackages、basePackageClasses 进行 ComponentScan。但是,它没有用。我不使用 Spring Boot 配置,因为它是出于学习目的。
我在 com.springinaction.aspects.concert.config 包中创建了相同的 SgtPeppers bean。自动装配工作。为什么 bean 创建的 java config 方式不适用于不同包中定义的 bean?
任何建议都会有所帮助。
谢谢。