Failed to create bean

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

Failed to create bean

问题

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

package guru.springframework.test.external.props;

import guru.springframework.test.jms.FakeJmsBroker;
import guru.test.config.external.props.ExternalPropsEnvironment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

/**
 * Created by jt on 5/7/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ExternalPropsEnvironment.class)
public class PropertySourceEnvTest {

    @Autowired
    FakeJmsBroker fakeJmsBroker;

    @Test
    public void testPropsSet() throws Exception {
        assertEquals("10.10.10.123", fakeJmsBroker.getUrl());
        assertEquals(3330, fakeJmsBroker.getPort().intValue());
        assertEquals("Ron", fakeJmsBroker.getUser());
        assertEquals("Burgundy", fakeJmsBroker.getPassword());
    }
}

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

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:

package guru.springframework.test.jms;

/**
 * Created by jt on 5/7/16.
 */
public class FakeJmsBroker {

    private String url;
    private Integer port;
    private String user;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
英文:

I tried to run the following test code:

				package guru.springframework.test.external.props;

			import guru.springframework.test.jms.FakeJmsBroker;
			import guru.test.config.external.props.ExternalPropsEnvironment;
			import org.junit.Test;
			import org.junit.runner.RunWith;
			import org.springframework.beans.factory.annotation.Autowired;
			import org.springframework.test.context.ContextConfiguration;
			import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

			import static org.junit.Assert.assertEquals;

			/**
			 * Created by jt on 5/7/16.
			 */
			@RunWith(SpringJUnit4ClassRunner.class)
			@ContextConfiguration(classes = ExternalPropsEnvironment.class)
			public class PropertySourceEnvTest {

			    @Autowired
			    FakeJmsBroker fakeJmsBroker;

			    @Test
			    public void testPropsSet() throws Exception {
				assertEquals("10.10.10.123", fakeJmsBroker.getUrl());
				assertEquals(3330, fakeJmsBroker.getPort().intValue());
				assertEquals("Ron", fakeJmsBroker.getUser());
				assertEquals("Burgundy", fakeJmsBroker.getPassword());
			    }

			}

Not sure why I am getting this error:

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:

    package guru.springframework.test.jms;

/**
 * Created by jt on 5/7/16.
 */
public class FakeJmsBroker {

    private String url;
    private Integer port;
    private String user;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

答案1

得分: 1

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

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

@Configuration
public class MyConfig {
    // 这将创建一个 FakeJmsBroker 的实例,将置于 Spring 上下文中
    @Bean
    public FakeJmsBroker createFakeBroker() {
        return new FakeJmsBroker();
    }
}

通过上述类/方法或使用注解驱动的方法,都可以使这个 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.

@Configuration
public class MyConfig {
    // This will create an instance of FakeJmsBroker that will be in the Spring context
    @Bean
    public FakeJmsBroker createFakeBroker() {
       return new FakeJmsBroker();
    }
}

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:

确定