web-dev-qa-db-ja.com

自動配線された依存関係の挿入に失敗しました。

小さなJava EE Hibernate Springアプリケーションを開発していますが、エラーが表示されました。
Error creating bean with name 'articleControleur': Injection of autowired dependencies failed;

oct. 26, 2011 3:51:44 PM org.Apache.catalina.core.ApplicationContext log
Grave: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleControleur': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.bd.service.ArticleService com.bd.controleur.ArticleControleur.articleService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bd.service.ArticleService] 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:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:425)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:442)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:458)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.Java:339)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.Java:306)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.Java:127)
at javax.servlet.GenericServlet.init(GenericServlet.Java:212)
at org.Apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.Java:1173)
at org.Apache.catalina.core.StandardWrapper.load(StandardWrapper.Java:993)
at org.Apache.catalina.core.StandardContext.loadOnStartup(StandardContext.Java:4350)
at org.Apache.catalina.core.StandardContext.start(StandardContext.Java:4659)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardHost.start(StandardHost.Java:785)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardEngine.start(StandardEngine.Java:445)
at org.Apache.catalina.core.StandardService.start(StandardService.Java:519)
at org.Apache.catalina.core.StandardServer.start(StandardServer.Java:710)
at org.Apache.catalina.startup.Catalina.start(Catalina.Java:581)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at Java.lang.reflect.Method.invoke(Unknown Source)
at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:289)
at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.bd.service.ArticleService com.bd.controleur.ArticleControleur.articleService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bd.service.ArticleService] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:502)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:282)
... 33 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bd.service.ArticleService] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.Java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:474)
... 35 more

oct. 26, 2011 3:51:44 PM org.Apache.catalina.core.StandardContext loadOnStartup
Grave: La servlet /ExempleAchref a généré une exception "load()"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bd.service.ArticleService] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.Java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:474)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:282)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:425)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:442)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:458)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.Java:339)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.Java:306)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.Java:127)
at javax.servlet.GenericServlet.init(GenericServlet.Java:212)
at org.Apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.Java:1173)
at org.Apache.catalina.core.StandardWrapper.load(StandardWrapper.Java:993)
at org.Apache.catalina.core.StandardContext.loadOnStartup(StandardContext.Java:4350)
at org.Apache.catalina.core.StandardContext.start(StandardContext.Java:4659)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardHost.start(StandardHost.Java:785)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardEngine.start(StandardEngine.Java:445)
at org.Apache.catalina.core.StandardService.start(StandardService.Java:519)
at org.Apache.catalina.core.StandardServer.start(StandardServer.Java:710)
at org.Apache.catalina.startup.Catalina.start(Catalina.Java:581)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at Java.lang.reflect.Method.invoke(Unknown Source)
at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:289)
at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:414)

そして、記事のリストを回復する機能を含む私のコントローラーArticleControleurは次のとおりです。

package com.bd.controleur;

import Java.util.HashMap;
import Java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bd.service.ArticleService;

@Controller
@RequestMapping("/Article/GererArticle")
public class ArticleControleur {

    @Autowired
    ArticleService articleService;

    public ModelAndView listarticle() {

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("articles", articleService.getAllArticles());

    // new ModelAndView("nom de la page jsp", model qui contient l'ensemble
    // des beans que tu vas passer a cette page)
    return new ModelAndView("GererArticle", model);

    }

}

そして、これが私のarticleServiceです:

package com.bd.service;

import Java.util.Collection;
import Java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bd.dao.ArticleDao;
import com.bd.entity.Article;

@Service
public class ArticleServiceImp implements ArticleService {

    @Autowired
    private ArticleDao articledao;



    @Override
    public Article getArticleById(int articleId) {
        // TODO Auto-generated method stub
        return articledao.getById(articleId);
    }

    @Override
    public void saveArticle(Article article) {
        // TODO Auto-generated method stub
        articledao.save(article);

    }

    @Override
    public void deleteArticle(Article article) {
        // TODO Auto-generated method stub
        articledao.delete(article);
    }

    @Override
    public List<Article> getAllArticles() {
        // TODO Auto-generated method stub
        return articledao.getAll();
    }

}

そして、これが私のアプリケーションコンテキストです。

<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Application context containing authentication, channel
  - security and web URI beans.
  -
  - Only used by "filter" artifact.
  -
  -->

<b:beans xmlns:b="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"



    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      ">


  <!-- Auto-detect components -->
  <context:component-scan base-package="com.bd.entity"/>


</b:beans>
44
achref05

このエラーは、com.bd.service.ArticleServiceが登録済みBeanではないことを示しています。アプリケーションコンテキストで自動配線されるBeanを含むパッケージを追加します。

<context:component-scan base-package="com.bd.service"/>
<context:component-scan base-package="com.bd.controleur"/>

または、com.bdにすべてのサブパッケージを含める場合:

<context:component-scan base-package="com.bd">
     <context:include-filter type="aspectj" expression="com.bd.*" />
</context:component-scan>

補足として、Spring 3.1以降を使用している場合は、 @ComponentScan アノテーションを利用できるため、コンポーネントスキャンに関するxml構成を使用する必要はありません。 @Configuration と組み合わせて使用​​します。

@Controller
@RequestMapping("/Article/GererArticle")
@Configuration
@ComponentScan("com.bd.service") // No need to include component-scan in xml
public class ArticleControleur {

    @Autowired
    ArticleService articleService;
    ...
}

Autowiring の深さのセクションでこの春を見つけるかもしれません。

54
Xavi López

「articleService」のIDを持つコンテキストファイルで宣言されたBeanがありますか?自動配線は、コンテキストファイル内のBeanのIDと、自動配線しようとしている変数名とを一致させると思います。

2
DaveH
public class Organization {

    @Id
    @Column(name="org_id")
    @GeneratedValue
    private int id;

    @Column(name="org_name")
    private String name;

    @Column(name="org_office_address1")
    private String address1;

    @Column(name="org_office_addres2")
    private String address2;

    @Column(name="city")
    private String city;

    @Column(name="state")
    private String state;

    @Column(name="country")
    private String country;

    @JsonIgnore
    @OneToOne
    @JoinColumn(name="pkg_id")
    private int pkgId;

    public int getPkgId() {
        return pkgId;
    }

    public void setPkgId(int pkgId) {
        this.pkgId = pkgId;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Column(name="pincode")
    private String pincode;

    @OneToMany(mappedBy = "organization", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<OrganizationBranch> organizationBranch = new HashSet<OrganizationBranch>(0);

    @Column(name="status")
    private String status = "ACTIVE";

    @Column(name="project_id")
    private int redmineProjectId;

    public int getRedmineProjectId() {
        return redmineProjectId;
    }

    public void setRedmineProjectId(int redmineProjectId) {
        this.redmineProjectId = redmineProjectId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Set<OrganizationBranch> getOrganizationBranch() {
        return organizationBranch;
    }

    public void setOrganizationBranch(Set<OrganizationBranch> organizationBranch) {
        this.organizationBranch = organizationBranch;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }
}

Change datatype intのprivate int pkgId行をプリミティブクラス名に変更するか、アノテーション@autowiredを追加します

0
gajera bhavin