没有 xml bean 定义的 Spring 组件检测

发布于 2024-11-07 04:46:06 字数 1738 浏览 1 评论 0原文

只要配置了上下文组件扫描,就可以仅使用 @Component 注解来创建 spring bean,这是否正确?

将 spring 3.0.5 与 Java 6 结合使用。

我的测试用例是:

@ContextConfiguration(locations={"classpath:spring-bean.xml"})

public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests {
    @Autowired
    private ServerService serverService;

    @Test
    public void test_server_service() throws Exception {
           serverService.doSomething();
           //additional test code here
        }
}

spring-bean.xml 文件包含:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config/>
</beans>

我想要成为 bean 的类是:

@Component("ServerService")
public class ServerServiceImpl implements ServerService {
    private static final String SERVER_NAME = "test.nowhere.com";
        //method definitions.....'
}

这是否不足以让 spring 实例化ServerService bean 并进行自动装配?

我得到的错误是:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项的 [serversystem.ServerService] 类型的匹配 bean:预计至少有 1 个有资格作为此依赖项的自动装配候选者的 bean。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我确信我错过了一些简单的东西。

Is it correct that one can create spring beans using just the @Component annotation as long as context component scanning is configured?

Using spring 3.0.5 with Java 6.

My test case is:

@ContextConfiguration(locations={"classpath:spring-bean.xml"})

public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests {
    @Autowired
    private ServerService serverService;

    @Test
    public void test_server_service() throws Exception {
           serverService.doSomething();
           //additional test code here
        }
}

The spring-bean.xml file contains:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config/>
</beans>

My class I want to be a bean is:

@Component("ServerService")
public class ServerServiceImpl implements ServerService {
    private static final String SERVER_NAME = "test.nowhere.com";
        //method definitions.....'
}

Should that not be sufficient for spring to instantiate the ServerService bean and do the autowiring?

The error I get is:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [serversystem.ServerService] 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 sure I'm missing something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-11-14 04:46:06

您尚未在 spring-beans.xml 中定义 元素:

<context:component-scan base-package="the.package.with.your.service"/>

包含

<context:annotation-config/>

仅允许您使用 @Required<用于配置的 /code>、@Autowired@Inject 注释。通过指定 ,您可以告诉 Spring 在哪里查找 @Component 注释。

You have not defined in your spring-beans.xml the <context:component-scan> element:

<context:component-scan base-package="the.package.with.your.service"/>

The inclusion of

<context:annotation-config/>

only allows you to use @Required, @Autowired, and @Inject annotations for configuration. By specifying the <context:component-scan>, you are telling Spring where to look for @Component annotations.

似梦非梦 2024-11-14 04:46:06

如果您正在使用带注释的控制器和其他功能
您应该包括

<mvc:annotation-driven/>

您应该用来

<context:component-scan base-package="spring3.example.controllers"/>

指定存储控制器类的包。

if you are using annotated controllers and other features
you should include

<mvc:annotation-driven/>

you should use

<context:component-scan base-package="spring3.example.controllers"/>

to specify the package in which controller classes are stored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文