类是编译时编织的。我在文档中看到,对于在容器上下文之外构造的类,警告,但不是完全错误,但是我在maven构建中得到了以下测试错误:
testExecuteCommand(SportTimeExecutionCommandTest): Error creating bean with name 'SportTimeExecutionCommand': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'timeStrategyAnalyzer' is defined尽管我与Mockito junit类跑步者一起运行测试,但我还是这样做了。
@RunWith(MockitoJUnitRunner.class)
public class SportTimeExecutionCommandTest {
private static final Integer SPORT_ID = 1;
@Mock
private IdTimeRequestAdjuster idTimeRequestAdjuster ;
@Mock
private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;
@Mock
private SportStepExecutor stepExecutor ;
@Mock
private TimeRequestStrategy strategy ;
@Mock
private TimeStep step;
@Mock
private Future<List<EventData>> future;
private SportTimeExecutionCommand command;
private List<TimeStep> steps = new ArrayList<TimeStep>();
@Before
public void setUp() throws Exception {
command = new SportTimeExecutionCommand(SPORT_ID);
command.setIdTimeRequestAdjuster(idTimeRequestAdjuster);
command.setStepExecutor(stepExecutor);
command.setStrategyAnalyzer(strategyAnalyzer);
when(idTimeRequestAdjuster.getCurrentValue(SPORT_ID)).thenReturn(strategy);
when(strategyAnalyzer.calculateSteps(strategy)).thenReturn(steps);
steps.add(step);
when(stepExecutor.executeStep(SPORT_ID, step)).thenReturn(future);
when(future.get()).thenReturn(new ArrayList<EventData>());
}
@Test
public void testExecuteCommand() throws InterruptedException, ExecutionException {
command.executeCommand();
verify(idTimeRequestAdjuster).getCurrentValue(SPORT_ID);
verify(strategyAnalyzer).calculateSteps(strategy);
verify(stepExecutor).executeStep(SPORT_ID,step);
}
}供参考的实现类:
@Configurable(autowire=Autowire.BY_TYPE)
public class SportTimeExecutionCommand implements AdjustingCommand<List<EventData>,Integer>{
private final static Logger logger = Logger.getLogger(SportTimeExecutionCommand.class.getName());
@Autowired
private IdTimeRequestAdjuster idTimeRequestAdjuster ;
@Resource(name = "timeStrategyAnalyzer")
private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;
@Autowired
private SportStepExecutor stepExecutor ;
private final Integer sportId ;
public SportTimeExecutionCommand(Integer sportId)
{
this.sportId = sportId ;
}
@Override
public List<EventData> executeCommand() throws InterruptedException, ExecutionException {
List<EventData> eventData = new ArrayList<EventData>();
List<TimeStep> timeSteps = strategyAnalyzer.calculateSteps(idTimeRequestAdjuster.getCurrentValue(sportId));
List<Future<List<EventData>>> futureResults = new ArrayList<Future<List<EventData>>>();
for (TimeStep timeStep : timeSteps) {
futureResults.add(stepExecutor.executeStep(sportId, timeStep));
}
for (Future<List<EventData>> futureEventData : futureResults) {
eventData.addAll(futureEventData.get());
}
return eventData;
}
@Override
public Integer getCriteria() {
return sportId;
}
@Override
public void adjust() {
logger.warning("adjusting sportId "+sportId+" value is now : "+idTimeRequestAdjuster.getCurrentValue(sportId).getRequests());
idTimeRequestAdjuster.adjustUp(sportId);
}
public void setIdTimeRequestAdjuster(IdTimeRequestAdjuster idTimeRequestAdjuster) {
this.idTimeRequestAdjuster = idTimeRequestAdjuster;
}
public void setStrategyAnalyzer(StrategyAnalyzer<List<TimeStep>> strategyAnalyzer) {
this.strategyAnalyzer = strategyAnalyzer;
}
public void setStepExecutor(SportStepExecutor stepExecutor) {
this.stepExecutor = stepExecutor;
}
}有人能看到问题吗?
编辑:我怀疑它首先处理资源,这就是为什么这里会发生错误。如果我将注释切换为自动处理,那么它将在第一个自动处理bean上中断。
发布于 2012-06-01 16:50:00
我的解决方案是运行一个独立的maven构建概要文件,该配置文件在没有方面的情况下编译,然后在集成阶段使用方面进行编译。
发布于 2012-03-27 21:01:59
我认为问题是由@Resource注释中的@Resource属性引起的:
@Resource(name = "timeStrategyAnalyzer")
private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;可能,如果您删除它,由方面执行的依赖注入将很好地工作-请注意,实际上您没有在您发布的代码中任何地方公开这个名称的资源。在Spring之外构造的编织类(使用new操作符)也有资格进行依赖注入。
https://stackoverflow.com/questions/9893579
复制相似问题