简体   繁体   English

为什么春天找不到我的豆子?

[英]Why spring can't find my bean?

I created an interface and a class: 我创建了一个接口和一个类:

public interface UserService {
    List<User> listAll();
}

@Transactional
public class DefaultUserService implements UserService {
    private String tableName;
    public List<User> listAll() { someDao.listAllFromTable(tableName); }
    public void setTableName(String tableName) { this.tableName = tableName; }
}

Also in my application context xml file context.xml , I defined: 另外在我的应用程序上下文xml文件context.xml ,我定义了:

<bean id="userService" class="mypackage.DefaultUserService">
    <property name="tableName" value="myusers" />
</bean>

Then I want to test the DefaultUserService : 然后我想测试DefaultUserService

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:context-test.xml"})
@TransactionConfiguration(transactionManager = "testTransactionManager")
@Transactional
public class UserServiceTest {

    @Autowired
    private DefaultUserService userService;

    @Before
    public void setup() {
        userService.setTableName("mytesttable");
    }
    @Test
    public void test() {
        // test with userService;
        userService.listAll();
    }
}

Notice it uses context-test.xml , which imported the original context.xml : 请注意它使用了context-test.xml ,它导入了原始的context.xml

<import resource="classpath:context.xml"/>

Unfortunately, when the test starts, spring throws exception: 不幸的是,当测试开始时,spring抛出异常:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mypackage.UserServiceTest': 
Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: 
private mypackage.DefaultUserService mypackage.userService

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [mypackage.DefaultUserService] 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)}

I'm not sure where is wrong, why spring can't find the bean DefaultUserService I defined? 我不确定哪里出错了,为什么spring无法找到我定义的bean DefaultUserService

Try to replace the class DefaultUserService to the interface UserService 尝试将类DefaultUserService替换为UserService接口

public class UserServiceTest {

    @Autowired
    private UserService userService;
    ....

}

It's because @Transactional places the bean is behind a jdk proxy implementing UserService interface, after that the bean is only available as UserService and not DefaultUserService . 这是因为@Transactional将bean放在实现UserService接口的jdk代理后面,之后bean只能用作UserService而不是DefaultUserService See https://stackoverflow.com/a/18875681/241986 . 请参阅https://stackoverflow.com/a/18875681/241986

You can try setting the table name with a property placeholder @Value("${someprop}") and define that property in test context, or create another interface that will expose setTableName() , and autowire that helper interface into the test case. 您可以尝试使用属性占位符@Value("${someprop}")设置表名,并在测试上下文中定义该属性,或者创建另一个将公开setTableName()接口,并将该帮助程序接口自动装入测试用例。

I'm not sure there are any easy solutions of the problem, I think this task can be subsumed under the problem of bean redefinition in Spring test-context framework Spring beans redefinition in unit test environment 我不确定是否有任何简单的问题解决方案,我认为这个任务可以归入Spring测试环境框架中的bean重新定义问题。单元测试环境中的Spring bean重新定义

您尚未在implementing class为属性tableName定义getter .Spring IOC容器适用于POJO模型

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

相关问题 为什么 Spring Boot 没有找到试图执行自动装配的 @Service bean? bean 存在但找不到它 - Why Spring Boot is not finding a @Service bean trying to perform autowiring? the bean exist but it can't find it Spring找不到bean并抛出NoSuchBeanDefinitionException - Spring can't find bean and throws NoSuchBeanDefinitionException 使用Spring的WebApp找不到bean - WebApp using Spring can't find bean Spring Boot找不到我的自动装配类(自动装配的成员必须在有效的Spring bean中定义) - Spring Boot can't find my autowired class (Autowired members must be defined in valid Spring bean) 为什么我的 Postman 找不到我的 Spring REST ZDB974238714CA8DE634A7CE1D083A14? - Why can't my Postman find my Spring REST API? “字段需要找不到类型的bean。” Spring找不到我的存储库界面 - 'Field required a bean of type that could not be found.' Spring can't find my repository interface Spring Boot-为什么我的应用程序找不到静态资源? - Spring boot - why can't my application find static resources? 为什么Postman找不到我的Spring REST API? - Why can't Postman find my Spring REST API? Spring 引导找不到我的 Java bean 映射器 - Spring Boot doesn't find my Java bean mapper 春天找不到我的豆子 - spring cannot find my bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM