Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有类型为“的合格bean”;myOwnType";-像字符串一样的原始bean可以工作_Java_Spring_Applicationcontext_Spring Bean - Fatal编程技术网

Java 没有类型为“的合格bean”;myOwnType";-像字符串一样的原始bean可以工作

Java 没有类型为“的合格bean”;myOwnType";-像字符串一样的原始bean可以工作,java,spring,applicationcontext,spring-bean,Java,Spring,Applicationcontext,Spring Bean,我有一个Spring项目,试图将服务应用程序上下文传递给另一个“Spring免费”的服务 我的春季计划: @Service public class MySpringService implements MyApi { ... private static MySpringService INSTANCE; @Autowired private MyRepository repo; @SuppressWarnings("resource") public static My

我有一个Spring项目,试图将服务应用程序上下文传递给另一个“Spring免费”的服务

我的春季计划:

@Service
public class MySpringService implements MyApi {

...

private static MySpringService INSTANCE;

@Autowired
private MyRepository        repo;

@SuppressWarnings("resource")
public static MySpringService implementation() {
    if (INSTANCE == null) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceConfig.class);
        INSTANCE = ctx.getBean(MySpringService.class);
    }

    return INSTANCE;
}
这是对应的AppConfig:

@Configuration
@ComponentScan("de.mypackage.my.serivce")
public class MyServiceConfig 
...
@Bean
public MySpringService mySpringService() {
    return new MySpringService();
}
在我的另一个项目中,我只是尝试调用MySpringService spgSvc=MySpringService.implementation()这会导致NosuchBeanDefinition异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [de.mypackage.my.serivce.MySpringService] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
at de.mypackage.my.serivce.MySpringService.implementation(MySpringService.java:54)
at de.mypackage.second.service.OtherService.resolveCampaignTree(OtherService.java:18)
at de.mypackage.second.service.ResolverTest.testMe(ResolverTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.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)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
另一方面,我可以轻松地传递仅为字符串或整数的基本bean。而且这种方法已经在其他项目中起作用了!我检查了每一行代码,但我仍然不知道我搞砸了什么。Maven安装成功。唯一不同的是,这次我在一个JUNIT测试中,该测试调用了抛出异常的“spring free”项目。有人有主意吗

编辑: 我试图在我的bean定义public MySpringService MySpringService()中使用一个显式的@Qualifier(myQualifier)来解决这个问题。但是尝试使用INSTANCE=(MySpringService)ctx.getBean(“myQualifier”)获取bean导致以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myQualifier' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:677)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at de.mypackage.my.serivce.MySpringService.implementation(MySpringService.java:54)
at de.mypackage.second.service.OtherService.resolveCampaignTree(OtherService.java:18)
at de.mypackage.second.service.ResolverTest.testMe(ResolverTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.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)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我有一个正确的概念,但我可以看到这里的问题是类及其实例加上bean的命名。尝试执行以下操作,您可以选择选项1或选项2在类MyApi实现中注入MySpringService对象

@Configuration
@ComponentScan({"de.mypackage.my.serivce"})
public class MyServiceConfig {

        @Bean
        public MySpringService mySpringService() {
            return new MySpringService();
        }
        ...
}

@Service
public class MyApiImplementation implements MyApi {

    /* Option: 1
     * The mySpringService field name below should match the method name
     * mySpringService() in the MyServiceConfig.
     */
    @Autowired
    private static MySpringService mySpringService;

    /* Option: 2
     * Or the below if the name of the field (in this case instance) does not
     * match the method name mySpringService() in the MyServiceConfig.
     */
    @Autowired
    @Qualifier("mySpringService")
    private static MySpringService instance;

    ...
}

我的问题是由于没有完全理解哪些使用的Spring注释是必要的

  • “组件扫描”完全没有必要
  • MySpringService的@Service注释和@Bean定义可能会导致这些问题
  • 删除@Service和@ComponentScan后,我的服务工作正常!这一见解让我想起了格泽戈兹: