我想初始化使用Springbean继承的类。我遵循这个stackoverflow问题来实现继承Spring继承-注释,但我遇到了一个异常
这是我的家长班
public class DefaultScenarioResultAnalyzer implements ScenarioResultAnalyser, CommonConstants{
@Autowired
NotificationEmail notificationEmail;
@Bean
public DefaultScenarioResultAnalyzer defaultScenarioResultAnalyzer() {
return new DefaultScenarioResultAnalyzer();
}
public DefaultScenarioResultAnalyzer() {
}
public DefaultScenarioResultAnalyzer(ScenarioResult scenarioResult) {
this.scenarioResult = scenarioResult;
this.scenarioId = scenarioResult.getScenarioId();
this.scenarioIdAsString = SCENARIO_FILE_PREFIX + scenarioId;
}
我的孩子的课程如下
@Configuration
public class MaxUsersSAQOSScenarioResultAnalyzer extends DefaultScenarioResultAnalyzer {
private ScenarioResult scenorioResult;
@Bean
public MaxUsersSAQOSScenarioResultAnalyzer maxUsersSAQOSScenarioResultAnalyzer(){
return new MaxUsersSAQOSScenarioResultAnalyzer();
}
public MaxUsersSAQOSScenarioResultAnalyzer() {
}
public MaxUsersSAQOSScenarioResultAnalyzer(ScenarioResult scenarioResult) {
super(scenarioResult);
this.scenorioResult= scenarioResult;
setSuccessEmailTemplateId("velocity/scenario/maxusers-saqos-scenario-success.vm");
//scenario failure: use DefaultScenarioResultAnalyzer.failureEmailTemplateId
}
我得到了这个例外
2016-05-16 06:06:50,160 14983 ERROR [main] [org.springframework.test.context.TestContextManager] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@79befb56] to prepare test instance [com.asklytics.scenario.MIRScenarioTest@58e7de77]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'maxUsersSAQOSScenarioResultAnalyzer' defined in com.asklytics.scenario.MaxUsersSAQOSScenarioResultAnalyzer: factory-bean reference points back to the same bean definition
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:662)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:975)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:752)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 28 common frames omitted
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'maxUsersSAQOSScenarioResultAnalyzer' defined in com.asklytics.scenario.MaxUsersSAQOSScenarioResultAnalyzer: factory-bean reference points back to the same bean definition
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:662)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:975)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:752)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 28
更多
在我看来,问题在于Spring试图创建两个同名的不同bean。默认情况下,当您使用@Component
注释(包括@Configuration
、@Service
等注释,因为它们也是@Component
注释的)定义一个bean时,它的名称在本例中是类名。
使用@Bean
注释定义bean时,其名称是带注释方法的名称。
所以你可以做两件事
您可以通过设置批注的属性来设置Configuration
bean名称
@Configuration("maxUsersSAQOSScenarioResultAnalyzerFactory")
或者将@Bean
注释方法的名称更改为其他名称
如何使用注释在Spring中提供bean继承?在XML配置中,我使用了
问题内容: 使用基于注释的配置(等)是否可以实现相同的bean继承? http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework- reference/htmlsingle/#beans-child-bean- definitions 问题答案: java config中没有抽象bean的概念,因为Java语言已经
使用基于注释的配置(等)是否可以实现相同的bean继承? http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-child-bean-definitions
我写了一个示例Spring启动应用程序,它无法运行消息 `描述:com中的customerRepository字段。hibernatetutorial。服务CustomerServiceImpl需要“com”类型的bean。hibernatetutorial。存储库。找不到CustomerRepository“”。 操作:考虑在您的配置中定义类型为“com.hibernatetutorial.re
本文向大家介绍Java继承的注释,包括了Java继承的注释的使用技巧和注意事项,需要的朋友参考一下 示例 默认情况下,类注释不适用于扩展它们的类型。可以通过将@Inherited注释添加到注释定义中来更改 示例 请考虑以下2个注释: 和 如果这样注释了三个类: 运行此代码 将打印与此类似的结果(取决于注释的包): 请注意,注释只能从类继承,而不能从接口继承。
问题内容: 考虑此问题中列出的情况: 在Hibernate中映射多级继承 如何使用注释而不是hbm文件完成此映射? 问题答案: 您具体遇到什么问题?通过联接的子类映射类层次结构非常简单: 更新 (基于Michal的评论)。 如果您确实要使用区分符(并且您应该有 充分的 理由这样做),则可以通过将基于类的表策略与辅助表进行混合来实现: 这种方法的缺点是,您必须为每个映射的属性显式指定表: