gpt4 book ai didi

java - 没有这样的 bean 定义并且不可能创建 bean

转载 作者:行者123 更新时间:2023-12-01 12:49:16 24 4
gpt4 key购买 nike

我正在开发包含 4 层的 Web 应用程序。我使用 Spring MVC JPA/HIBERNATE 框架。问题是我无法 Autowiring creerBilanImpl 类中的字段 opmanimpl我没有得到这样的 bean 定义异常。我需要帮助!!

ConstruireBilanImpl

@Service
public class ConstruireBilanImpl implements ConstruireBilan {

@Autowired(required=true)
@Qualifier("OperationManagerImpl")
private OperationManager opmanimpl ;

private float immobilisations_corporelles=sommeComptes(1266,1322);

public ConstruireBilanImpl(){};
public void setOpmanimpl(OperationManagerImpl x){opmanimpl=x;}

public float sommeComptes(int... doubles){
if (this.opmanimpl==null){System.out.println("BAAAAAAAAAAAAAADDDDDDDDDD");}
return opmanimpl.sommeComptes(doubles);


}

public float getImmobilisations_corporelles(){return immobilisations_corporelles;}
public void setImmobilisations_corporelles(float x){immobilisations_corporelles=x;}
}

OperationManagerImpl

@Repository
public class OperationManagerImpl implements OperationManager {

@PersistenceContext(unitName="ERP")
private EntityManager em;

public OperationManagerImpl(){};

public void setEm(EntityManager emm){em=emm;}


public void creerOperation(opération op) {
if (em!=null){System.out.print(" manager de bean initialisé");}

em.persist(op);



System.out.println("dou5oul---------2");}

public opération rechercherOperation(int clé){
opération oper=em.find(opération.class,clé);
return oper;

}

public void supprimerOperation(int clé)
{
opération oper=em.find(opération.class,clé);
em.remove(oper);
}


public void modifierId(int clé,int nv){
opération oper=em.find(opération.class,clé);
oper.setIdop(nv);
em.flush();
}
public void modifierJournal(int clé,char j){
opération oper=em.find(opération.class,clé);
oper.setJournal(j);
em.flush();

}
public void modifierCompte(int clé,int nvc){
opération oper=em.find(opération.class,clé);
oper.setCompte(nvc);
em.flush();
}
public void modifierLibellé(int clé,String nvl ){
opération oper=em.find(opération.class,clé);
oper.setLibellé(nvl);
em.flush();

}
public void modifierDate(int clé,Date nvd){

opération oper=em.find(opération.class,clé);
oper.setDateop(nvd);
em.flush();
}
public void modifierDr(int clé,float nvdr){

opération oper=em.find(opération.class,clé);
oper.setDr(nvdr);
em.flush();
}
public void modifierCr(int clé,float nvcr){

opération oper=em.find(opération.class,clé);
oper.setCr(nvcr);
em.flush();
}


public List<opération> listeOpération() {
String ss="select op from opération op";
Query req= em.createQuery(ss);

return req.getResultList();
}


public void SupprimerOpération(int ref){

opération op=em.find(opération.class, ref);
em.remove(op);

}

public float sommeComptes(int... doubles) {
String liste= new String();
String x;
for (int d : doubles) {
x=Integer.toString(d);
x=x.concat(",");
liste=liste.concat(x);
}
liste=liste.substring(0, liste.length()-1);
String cc =" select sum(op.cr) from opération op ";
System.out.println(cc);
Query req= em.createQuery(cc);
Number somme=(Number)req.getSingleResult();
return somme.floatValue();

}

}

堆栈跟踪

  juin 24, 2014 2:34:29 AM org.apache.catalina.core.StandardContext listenerStart
Grave: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'operationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ensi.erp.metier.ConstruireBilan com.ensi.erp.controllers.OperationController.bil; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
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:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
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: private com.ensi.erp.metier.ConstruireBilan com.ensi.erp.controllers.OperationController.bil; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
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)
... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
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.findAutowireCandidates(DefaultListableBeanFactory.java:873)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
... 37 more
Caused by: java.lang.NullPointerException
at com.ensi.erp.metier.ConstruireBilanImpl.sommeComptes(ConstruireBilanImpl.java:67)
at com.ensi.erp.metier.ConstruireBilanImpl.<init>(ConstruireBilanImpl.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 39 more

root-context.xml

<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/dberp"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="datasource"></property>
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
<property name="persistenceUnitName" value="ERP"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>


<bean name="operationmanager"
class="com.ensi.erp.dao.OperationManagerImpl"/>

<bean id="opser" class="com.ensi.erp.metier.gererOperationImpl">
<property name="opmanimpl" ref="operationmanager"></property>
</bean>


<!-- <bean name="opmanimpl2"
class="com.ensi.erp.dao.OperationManagerImpl"/>

<bean id="bilan" class="com.ensi.erp.metier.ConstruireBilanImpl">
<property name="opmanimpl" ref="opmanimpl2"></property>
</bean>-->


<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config/>

<context:component-scan base-package="com.ensi"/>
</beans>

最佳答案

您尚未将 application-context.xml 链接到 web.xml 文件中。

这就是问题所在。

检查 Spring 配置文件的 web.xml 中是否有任何条目

或者:

您可能有两个配置文件,例如:

  1. spring-servlet.xml
  2. spring-security.xml

因此在一个中定义的 bean 无法找到在另一个中定义的 bean。

所以你应该尝试像<import resource="spring-servlet.xml"/>这样的东西在 spring-security.xml 中。

关于java - 没有这样的 bean 定义并且不可能创建 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375588/

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