gpt4 book ai didi

java - Spring不能创建bean

转载 作者:行者123 更新时间:2023-11-30 06:57:09 24 4
gpt4 key购买 nike

大家好,我在使用 spring mvc 和 hibernate 开发 Web 应用程序时遇到异常,问题是 spring 不知道我的 beans Controller

@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 类

@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)类 PersonneMetierImpl 实现 IPersonneMetier{ @Autowired private PersonneDaoImpl personneDao;

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

Servlet 上下文文件

    <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

谁能告诉我我该怎么做才能解决这个问题非常感谢

最佳答案

你需要改变你的 Controller ——更换线路

public PersonneMetierImpl personneMetier;

public IPersonneMetier personneMetier;

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

在某些情况下(包括您的情况),Spring 不按原样使用 bean (PersonneMetierImpl),而是将其包装在代理中。在这种情况下,它是一个 JDK dynamic proxy ,不能转换为 PersonneMetierImpl,只能转换为 IPersonneMetier(JDK 代理是基于接口(interface)的)。这就是 Spring 无法 Autowiring 字段 PersonneMetierImpl personneMetier 的原因。

哦,出于同样的原因(也因为这就是接口(interface)的用途),您应该更改 PersonneMetierImpl 类 - 将 private PersonneDaoImpl personneDao 替换为 private IPersonneDao personneDao.

关于java - Spring不能创建bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com