lundi 23 mars 2015

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private

I got this exception in my project(jsf+Spring+hibernate):



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.aa.customer.CustomerService com.aa.customer.UserBean.customerService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aa.customer.CustomerService] 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)}


Here is applicationContext.xml:



<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:c="http://ift.tt/MffYna"
xmlns:context="http://ift.tt/GArMu7"
xmlns:flow="http://ift.tt/19rsrif"
xmlns:jee="http://ift.tt/OpNaZ5"
xmlns:jms="http://ift.tt/1iMF6wC"
xmlns:lang="http://ift.tt/OGfeTY"
xmlns:osgi="http://ift.tt/1apCv6f"
xmlns:p="http://ift.tt/1jdM0fE"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:util="http://ift.tt/OGfeTW"

xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/18sW2ax
http://ift.tt/OpNdV1
http://ift.tt/JvbN5c
http://ift.tt/GArMu7
http://ift.tt/1bGeTcI
http://ift.tt/19rsrif
http://ift.tt/1iMF6wL
http://ift.tt/OpNaZ5
http://ift.tt/JvbN5h
http://ift.tt/1iMF6wC
http://ift.tt/1Fj1J6D
http://ift.tt/OGfeTY
http://ift.tt/1pFdnVn
http://ift.tt/1apCv6f
http://ift.tt/VRFjZG
http://ift.tt/OGfeU2
http://ift.tt/1cKeJ93
http://ift.tt/OGfeTW
http://ift.tt/1aj0W5e">

<!-- Activates scanning of @Autowired -->
<context:annotation-config/>

<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.aa.customer"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/obs" />
<property name="username" value="root" />
<property name="password" value="2323" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.aa.customer"
p:hibernateProperties-ref="hibernateProperties"/>

<util:properties id="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</util:properties>

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Detect @Transactional Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />

</beans>


This is web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://ift.tt/nSRXKP" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>Registration.xhtml</welcome-file>
</welcome-file-list>
</web-app>


Now here is UserBean :



@ManagedBean
@SessionScoped
@Component
public class UserBean {

private String username;
private String password;
private String email;
@Autowired
private CustomerService customerService;
//other methods
}


Here is CustomerService class:



@Service
public class CustomerService implements ServiceInterface<Customers, Integer> {

@Autowired
private CustomerDao customerDao;

public CustomerService() {
customerDao = new CustomerDao();
}
//other methods
}


And here is CustomerDao class:



@Repository
public class CustomerDao implements DaoInterface<Customers, Integer>, Serializable {

@Autowired
private SessionFactory sessionFactory;

private Session session;
private Transaction transaction;
//other methods
}


What is wrong with my code?


Aucun commentaire:

Enregistrer un commentaire