Failed to create bean

huangapple go评论134阅读模式
英文:

Failed to create bean

问题

我尝试运行以下测试代码:

  1. package guru.springframework.test.external.props;
  2. import guru.springframework.test.jms.FakeJmsBroker;
  3. import guru.test.config.external.props.ExternalPropsEnvironment;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import static org.junit.Assert.assertEquals;
  10. /**
  11. * Created by jt on 5/7/16.
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(classes = ExternalPropsEnvironment.class)
  15. public class PropertySourceEnvTest {
  16. @Autowired
  17. FakeJmsBroker fakeJmsBroker;
  18. @Test
  19. public void testPropsSet() throws Exception {
  20. assertEquals("10.10.10.123", fakeJmsBroker.getUrl());
  21. assertEquals(3330, fakeJmsBroker.getPort().intValue());
  22. assertEquals("Ron", fakeJmsBroker.getUser());
  23. assertEquals("Burgundy", fakeJmsBroker.getPassword());
  24. }
  25. }

不确定为什么会出现以下错误:

  1. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: guru.springframework.test.jms.FakeJmsBroker guru.springframework.test.external.props.PropertySourceEnvTest.fakeJmsBroker; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [guru.springframework.test.jms.FakeJmsBroker] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

FakeJmsBroker.java:

  1. package guru.springframework.test.jms;
  2. /**
  3. * Created by jt on 5/7/16.
  4. */
  5. public class FakeJmsBroker {
  6. private String url;
  7. private Integer port;
  8. private String user;
  9. private String password;
  10. public String getUrl() {
  11. return url;
  12. }
  13. public void setUrl(String url) {
  14. this.url = url;
  15. }
  16. public Integer getPort() {
  17. return port;
  18. }
  19. public void setPort(Integer port) {
  20. this.port = port;
  21. }
  22. public String getUser() {
  23. return user;
  24. }
  25. public void setUser(String user) {
  26. this.user = user;
  27. }
  28. public String getPassword() {
  29. return password;
  30. }
  31. public void setPassword(String password) {
  32. this.password = password;
  33. }
  34. }
英文:

I tried to run the following test code:

  1. package guru.springframework.test.external.props;
  2. import guru.springframework.test.jms.FakeJmsBroker;
  3. import guru.test.config.external.props.ExternalPropsEnvironment;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import static org.junit.Assert.assertEquals;
  10. /**
  11. * Created by jt on 5/7/16.
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(classes = ExternalPropsEnvironment.class)
  15. public class PropertySourceEnvTest {
  16. @Autowired
  17. FakeJmsBroker fakeJmsBroker;
  18. @Test
  19. public void testPropsSet() throws Exception {
  20. assertEquals("10.10.10.123", fakeJmsBroker.getUrl());
  21. assertEquals(3330, fakeJmsBroker.getPort().intValue());
  22. assertEquals("Ron", fakeJmsBroker.getUser());
  23. assertEquals("Burgundy", fakeJmsBroker.getPassword());
  24. }
  25. }

Not sure why I am getting this error:

  1. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: guru.springframework.test.jms.FakeJmsBroker guru.springframework.test.external.props.PropertySourceEnvTest.fakeJmsBroker; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [guru.springframework.test.jms.FakeJmsBroker] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

FakeJmsBroker.java:

  1. package guru.springframework.test.jms;
  2. /**
  3. * Created by jt on 5/7/16.
  4. */
  5. public class FakeJmsBroker {
  6. private String url;
  7. private Integer port;
  8. private String user;
  9. private String password;
  10. public String getUrl() {
  11. return url;
  12. }
  13. public void setUrl(String url) {
  14. this.url = url;
  15. }
  16. public Integer getPort() {
  17. return port;
  18. }
  19. public void setPort(Integer port) {
  20. this.port = port;
  21. }
  22. public String getUser() {
  23. return user;
  24. }
  25. public void setUser(String user) {
  26. this.user = user;
  27. }
  28. public String getPassword() {
  29. return password;
  30. }
  31. public void setPassword(String password) {
  32. this.password = password;
  33. }
  34. }

答案1

得分: 1

在这种情况下,FakeJmsBroker 只是一个 POJO。除非你将其标记为 @Component@Service 等,否则 Spring 对它一无所知。

你也可以在 @Configuration 类中将其作为 @Bean 返回一个新的实例。然而,从这个类的角度来看,除非它保存属性,否则它不作为组件是没有意义的,因为它不包含任何行为。下面是将其作为 bean 的示例配置:

  1. @Configuration
  2. public class MyConfig {
  3. // 这将创建一个 FakeJmsBroker 的实例,将置于 Spring 上下文中
  4. @Bean
  5. public FakeJmsBroker createFakeBroker() {
  6. return new FakeJmsBroker();
  7. }
  8. }

通过上述类/方法或使用注解驱动的方法,都可以使这个 bean 适用于自动装配。

英文:

In this instance, FakeJmsBroker is just a POJO. Unless you either annotate it as @Component, @Service, etc, then Spring knows nothing about it.

You can also return a new instance of this as a @Bean in a @Configuration class. However, looking at this class, unless it's holding properties, it doesn't make sense as a component as it doesn't contain any behavior. Here's an example of making it a bean from a configuration.

  1. @Configuration
  2. public class MyConfig {
  3. // This will create an instance of FakeJmsBroker that will be in the Spring context
  4. @Bean
  5. public FakeJmsBroker createFakeBroker() {
  6. return new FakeJmsBroker();
  7. }
  8. }

Either that class/method, or using the annotation-driven approach will make this bean eligible for autowiring.

huangapple
  • 本文由 发表于 2023年2月14日 07:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定