gpt4 book ai didi

java - 应用程序如何通知 Spring 应用程序上下文在哪里可以找到其构造函数带有 @Inject 注解的类?

转载 作者:行者123 更新时间:2023-12-01 09:52:58 24 4
gpt4 key购买 nike

如何修复以下示例以便通知 Spring application context在哪里可以找到一个类 Application ,其构造函数带有 @Inject 注释,但是没有向使用@Bean注释的ApplicationConfiguration引入返回Application类实例的bean方法?

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();
}
}

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

当我运行类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. Deinumadvice ,我用构造型 @Component 注释了类 Application告诉 Spring 将 Application 视为一个 bean 并使用 @ComponentScan 注释类 ApplicationConfiguration为了引导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;
}
}

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".

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

关于java - 应用程序如何通知 Spring 应用程序上下文在哪里可以找到其构造函数带有 @Inject 注解的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com