“无法自动装配字段”春季异常

发布于 2024-12-19 07:10:35 字数 3758 浏览 2 评论 0原文

我有一个 Spring 应用程序,并且正在使用 Spring 版本 3.0.4。 当我启动服务器时,它显示一个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.domain.Custom org.controller.CustomController.custom; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.domain.Custom] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

域类的代码

@Entity
@Configurable
@Table(name="tbltest")
public class Custom implements Serializable{

    @Autowired
    public transient CustomRepository customRepository;

    public static CustomRepository getRepository() {
        CustomRepository repository = new Custom().CustomRepository;
        return repository;
    }

    @Transactional(readOnly = true)
    public static Custom find(final Long id) {
        return getRepository().find(id);
    }
}

在应用程序上下文中包含配置

<context:load-time-weaver/>
<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="org.domain.custom"/>

测试类的代码:

@Test
@Transactional
public void testCreateNew() {
    Custom registration = new Custom("ttt");
    Custom.find(1l);
    registration.persist();
    Assert.assertEquals(true, registration.getId() > 0);
}

控制器

@Controller
@RequestMapping("/custom")
public class CustomController
{
    @Autowired
    public Custom Custom;
}

我已经为 VM 参数添加了 spring-instrument.jar

I have a Spring application and am using Spring version 3.0.4.
When I start the server it shows me an error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.domain.Custom org.controller.CustomController.custom; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.domain.Custom] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

Code for the domain class

@Entity
@Configurable
@Table(name="tbltest")
public class Custom implements Serializable{

    @Autowired
    public transient CustomRepository customRepository;

    public static CustomRepository getRepository() {
        CustomRepository repository = new Custom().CustomRepository;
        return repository;
    }

    @Transactional(readOnly = true)
    public static Custom find(final Long id) {
        return getRepository().find(id);
    }
}

Included configurations in application context

<context:load-time-weaver/>
<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="org.domain.custom"/>

Code for my test class:

@Test
@Transactional
public void testCreateNew() {
    Custom registration = new Custom("ttt");
    Custom.find(1l);
    registration.persist();
    Assert.assertEquals(true, registration.getId() > 0);
}

Controller

@Controller
@RequestMapping("/custom")
public class CustomController
{
    @Autowired
    public Custom Custom;
}

And I have added spring-instrument.jar for the VM argument.

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

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

发布评论

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

评论(1

青柠芒果 2024-12-26 07:10:35

看起来您忘记添加

<context:component-scan base-package="org.domain" />

到 application-context.xml 中,以便 spring 知道在哪里扫描您的 @Component 注释。仅启用 是不够的。

Looks like you forgot to add

<context:component-scan base-package="org.domain" />

to your application-context.xml so that spring knows where to scan for your @Component annotations. Just enabling <context:annotation-config /> is not enough.

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