繁体   English   中英

应用程序如何通知Spring应用程序上下文在哪里找到用@Inject注释其构造函数的类?

[英]How can an application inform the Spring application context where to find a class whose constructor is annotated with @Inject?

我该如何解决,以告知下面的例子 应用程序上下文在哪里可以找到一类Application ,其构造都被注解@Inject ,但没有引入一个bean的方法来ApplicationConfiguration标注了@Bean返回类的实例Application

public class Application {
    private final A a;

    @Inject
    public Application(A a) {
        this.a = a;
    }

    public A getA() {
        return a;
    }
}

@Configuration
public class ApplicationConfiguration {
    @Bean
    public A getA() {
        return new A();
    }
}

public class A {
}

public class Start {
    public static void main(String[] arguments) {
        final ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        final Application application = context.getBean(Application.class);
        application.getA();
    }
}

您可以在GitHub上AtInject项目中查看源代码。

当我运行类Start ,Spring抱怨它找不到类Application

May 27, 2016 4:49:55 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eda2dbb: startup date [Fri May 27 16:49:55 EDT 2016]; root of context hierarchy
May 27, 2016 4:49:55 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.opessoftware.atinject.application.Application] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:975)
    at com.opessoftware.atinject.start.Start.main(Start.java:11)

按照M. Deinum建议 ,我用构造@Component注释了Application @Component以指示Spring将Application视作bean,并使用@ComponentScan注释了ApplicationConfiguration @ComponentScan ,以指示Spring在哪里可以找到组件Application

@Component
public class Application {
    private final A a;

    @Inject
    public Application(A a) {
        this.a = a;
    }

    public A getA() {
        return a;
    }
}

@Configuration
@ComponentScan({"me.derekmahar.atinject.application", "me.derekmahar.atinject.model"})
public class ApplicationConfiguration {
    @Bean
    public A getA() {
        return new A("A1");
    }
}

请注意,我还修改了类A以便它接受名称:

public class A {
    private final String name;

    public A(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Class Start现在可以正确打印"Name of a1 is "A1"."

May 30, 2016 11:14:49 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eda2dbb: startup date [Mon May 30 11:14:49 EDT 2016]; root of context hierarchy
May 30, 2016 11:14:49 AM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Name of a1 is "A1".

您可以在GitHub上AtInject项目中找到解决方案的源代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM