[spring4] Could not autowire field NoSuchBeanDefinitionException:
Bonjour a tous
j'utilise spring4 avec un generic dao et generic service
GenericDao
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class GenericDao<T, PK extends Serializable> implements
IGenericDao<T, PK> {
@Autowired
private SessionFactory sessionFactory;
public Session getSession() {
return sessionFactory.getCurrentSession();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
...
} |
ArticleDao
Code:
1 2 3 4 5 6 7 8 9 10
| @Repository("articleDao")
@Transactional
public class ArticleDao extends GenericDao<Article, Serializable> implements
IArticleDao {
Logger logger1 = LoggerFactory.getLogger(ArticleDao.class);
public ArticleDao() {
super.type = Article.class;
}
...
} |
GenericService
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class GenericService<T,D extends IGenericDao<T, Serializable>> implements IGenericService<T,D>{
@Autowired
public D dao;
protected GenericService( D dao) {
setDao(dao);
}
public void setDao(D dao) {
this.dao = dao;
}
public D getDao() {
return dao;
}
...
} |
ArticleService
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Service("articleService")
@Transactional
public class ArticleService extends GenericService<Article,IArticleDao> implements IArticleDao{
@Autowired
public ArticleService(IArticleDao dao) {
super(dao);
}
public List<Article> search(){
return getDao.search(null, null, 0.0, 10000.0);
}
...
} |
BasePage
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Controller
@RequestScoped
public class BasePage {
protected String templateName;
protected FacesContext facesContext;
@Autowired
protected IArticleService articleService;
public FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
public void setArticleService(IArticleService articleService) {
this.articleService = articleService;
}
...
} |
RechercheArticleForm
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| @Controller
@SessionScoped
public class RechercheArticleForm extends BasePage implements Serializable {
private static final long serialVersionUID = 1L;
Logger logger = LoggerFactory.getLogger(RechercheArticleForm.class);
private String id;
private String marque;
private String modele;
private Double prixMin;
private Double prixMax;
private List<Article> articles;
private List<Article> commande;
private Article articleSelect;
public String getMarque() {
return marque;
}
public void setMarque(String marque) {
this.marque = marque;
}
public String getModele() {
return modele;
}
public void setModele(String modele) {
this.modele = modele;
}
public Double getPrixMin() {
return prixMin;
}
public void setPrixMin(Double prixMin) {
this.prixMin = prixMin;
}
public Double getPrixMax() {
return prixMax;
}
public void setPrixMax(Double prixMax) {
this.prixMax = prixMax;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Article getArticleSelect() {
return articleSelect;
}
public void setArticleSelect(Article articleSelect) {
this.articleSelect = articleSelect;
}
public List<Article> getCommande() {
return commande;
}
public void setCommande(List<Article> commande) {
this.commande = commande;
}
public void search(){
setArticles(articleService.search());
}
public void add(){
Article article=new Article();
article.setModele(modele);
article.setMarque(marque);
article.setPrix(prixMin);
article.setDisponible(true);
articleService.save(article);
}
public String detail(){
System.out.println(getId());
String id=getParameter("id");
System.out.println("id--"+id);
if(id!=null)
setArticleSelect(articleService.read(Long.parseLong(id)));
return "detail";
}
} |
les fichiers de config:
applicationContext
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true">
<!-- Activates scanning of @Autowired -->
<context:annotation-config />
<!-- Activates scanning of @Repository -->
<context:component-scan base-package="com.formations.beans" />
</beans> |
applicationContext-dao
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
default-lazy-init="true">
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- prop key="schemaUpdate">true</prop-->
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository -->
<context:component-scan base-package="com.formations.dao"/>
<!-- Activates scanning of @service-->
<context:component-scan base-package="com.formations.service"/>
</beans> |
applicationContext-resource
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!-- For mail settings and future properties files -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
<property name="validationQuery" value="${jdbc.validationQuery}"/>
<property name="testOnBorrow" value="true"/>
</bean>
</beans> |
Le probleme a l'exécution j'obtiens un :
Code:
Error creating bean with name 'rechercheArticleForm': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.formations.service.IArticleService com.formations.beans.BasePage.articleService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.formations.service.IArticleService] 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)}
Quelqu"un pour m'aider svp ?