类是编译的编译时间。我在文档中读到,编译类将在容器上下文之外构造的类警告但不是完全错误,但我在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上中断。
答案 0 :(得分:1)
我认为问题是由name
注释中的@Resource
属性引起的:
@Resource(name = "timeStrategyAnalyzer")
private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;
可能,如果你删除它,方面执行的依赖注入将正常工作 - 请注意,实际上你不会在你发布的代码中的任何地方公开带有名称的资源。
在Spring Container之外构建的weaved类(使用new
运算符)也有资格进行依赖注入。
答案 1 :(得分:0)
我的解决方案是运行一个单独的maven构建配置文件,它在没有方面的情况下编译,然后在集成阶段编译方面。