Spring无法创建bean

时间:2015-11-18 23:15:51

标签: java spring hibernate spring-mvc

大家好我在使用spring mvc和hibernate开发web应用程序时遇到异常,问题是spring不知道我的bean 控制器

@Autowired
public PersonneMetierImpl personneMetier;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(Locale locale, Model model) {
    personneMetier.ajouterPersonne(new Personne("azrou", "zakaria.bariki@gmail.com", "bariki", "zakaria", "0641057217"));
    return "test";
}

DAO classe

@Repository
public class PersonneDaoImpl implements IPersonneDao {

@Autowired
private SessionFactory sessionFactory;

@Override
public void ajouterPersonne(Personne personne) {
    if(sessionFactory == null)
        System.out.println("aaaa");
    try{
        sessionFactory.getCurrentSession().save(personne);

    }catch(Exception e){
        e.printStackTrace();
    }
}

服务类     服务     public class PersonneMetierImpl实现IPersonneMetier {     @Autowired     私人PersonneDaoImpl personneDao;

@Override
@Transactional
public void ajouterPersonne(Personne personne) {
    personneDao.ajouterPersonne(personne);  
}

Servlet-Context文件

    <beans:bean id="datasource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3306/gestion_bp" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="" />
</beans:bean>
<beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="datasource" />
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>


<context:component-scan base-package="com.sqli.bap" />
<beans:bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="personneDao" class="com.sqli.bap.daoImpl.PersonneDaoImpl"/> 
<beans:bean id="personneMetier" class="com.sqli.bap.metierImpl.PersonneMetierImpl"/> 

异常

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.sqli.bap.metierImpl.PersonneMetierImpl com.sqli.bap.HomeController.personneMetier; nested exception is java.lang.IllegalArgumentException: Can not set com.sqli.bap.metierImpl.PersonneMetierImpl field com.sqli.bap.HomeController.personneMetier to com.sun.proxy.$Proxy16
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5253)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5543)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.sqli.bap.metierImpl.PersonneMetierImpl com.sqli.bap.HomeController.personneMetier; nested exception is java.lang.IllegalArgumentException: Can not set com.sqli.bap.metierImpl.PersonneMetierImpl field com.sqli.bap.HomeController.personneMetier to com.sun.proxy.$Proxy16
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 29 more
Caused by: java.lang.IllegalArgumentException: Can not set com.sqli.bap.metierImpl.PersonneMetierImpl field com.sqli.bap.HomeController.personneMetier to com.sun.proxy.$Proxy16
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:510)
    ... 31 more

有谁能告诉我,我该怎么做才能解决问题 非常感谢你

3 个答案:

答案 0 :(得分:3)

您需要更换控制器 - 替换

public PersonneMetierImpl personneMetier;

public IPersonneMetier personneMetier;

(即使用接口而不是实现)。

在某些情况下(包括你的),Spring并没有按原样使用bean(PersonneMetierImpl),而是将其包装在代理中。在这种情况下,它是JDK dynamic proxy,不能转换为PersonneMetierImpl,而只能转换为IPersonneMetier(JDK代理是基于接口的)。这就是为什么Spring无法自动装载字段PersonneMetierImpl personneMetier

哦,出于同样的原因(也因为那是什么接口),您应该更改PersonneMetierImpl类 - 将private PersonneDaoImpl personneDao替换为private IPersonneDao personneDao

答案 1 :(得分:0)

控制器不是类的真实表示,而是Spring生成的代理。这将实现该类的所有接口,因此您可以使用

@Autowired
public IPersonneDao personneMetier;

而不是

@Autowired
public PersonneMetierImpl personneMetier;

答案 2 :(得分:0)

对于IntelliJ IDEA用户,此错误也可能显示为错误。 Eclipse和Java for VS Code不会产生此问题。