2012-11-26 191 views
0

这是与此相关:春季误注入春天对象注射

Suddenly my spring.xml won't instantiate my classes and Error Exception occurs

我改变了我的春天XML来的applicationContext.xml的建议。我没有使用getbeans()从工厂实例化bean。相反,我使用了上述XML的对象注入。

但是我有一个问题。有一个错误:

SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordexistuserprofile' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'registeraction' while setting bean property 'registeraction'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'insertUserProfile' while setting bean property 'insertUserProfile'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'insertUserProfile' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'registeraction' of bean class [lotmovement.business.crud.InsertUserProfile]: Bean property 'registeraction' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701) 
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204) 
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registeraction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'insertUserProfile' while setting bean property 'insertUserProfile'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'insertUserProfile' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'registeraction' of bean class [lotmovement.business.crud.InsertUserProfile]: Bean property 'registeraction' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269) 
    ... 26 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'insertUserProfile' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'registeraction' of bean class [lotmovement.business.crud.InsertUserProfile]: Bean property 'registeraction' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269) 
    ... 39 more 
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'registeraction' of bean class [lotmovement.business.crud.InsertUserProfile]: Bean property 'registeraction' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:801) 
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651) 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78) 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276) 

这是奇怪,因为我自动通过NetBeans附加的制定者。应该没有错误。

这是我的applicationContext.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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

    <bean id="userprofile" class="lotmovement.business.entity.UserProfile"> 

    </bean> 

    <bean id="entitystart" 
      class="lotmovement.business.crud.EntityStart"> 

    </bean> 

    <bean id="recordexistuserprofile" 
      class="lotmovement.business.crud.RecordExistUserProfile"> 
     <property name="registeraction" ref="registeraction"/> 
     <property name="entitystart" ref="entitystart"/> 

    </bean> 


    <bean id="insertUserProfile" class="lotmovement.business.crud.InsertUserProfile"> 
     <property name="registeraction" ref="registeraction"/> 
     <property name="entitystart" ref="entitystart"/> 
     <property name="userprofile" ref="userprofile"/> 

    </bean> 

    <bean id="registeraction" class="lotmovement.action.RegisterAction"> 
     <property name="insertUserProfile" ref="insertUserProfile"/> 
     <property name="entitystart" ref="entitystart"/> 
     <property name="recordexistuserprofile" ref="recordexistuserprofile"/> 
    </bean> 


</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>register.jsp</welcome-file> 
    </welcome-file-list> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 


</web-app> 

我的课表:

RegisterAction

 /* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package lotmovement.action; 

import com.opensymphony.xwork2.ActionSupport; 
import lotmovement.business.crud.ApplicationContextProvider; 
import lotmovement.business.crud.EntityStart; 
import lotmovement.business.crud.InsertUserProfile; 
import lotmovement.business.crud.RecordExistUserProfile; 
import lotmovement.business.entity.UserProfile; 
import org.apache.commons.lang3.StringUtils; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.web.context.WebApplicationContext; 
import org.springframework.web.context.support.WebApplicationContextUtils; 

public class RegisterAction extends ActionSupport { 

    private static String userId; 
    private static String password; 
    private static String firstName; 
    private static String lastName; 
    private static int securityLevel; 

    EntityStart es; 
    InsertUserProfile iup; 
    RecordExistUserProfile reup; 


    ApplicationContextProvider acp = new ApplicationContextProvider(); 

    ApplicationContext ctx = ApplicationContextProvider.getApplicationContext(); 


    @Override 
    public void validate() { 
     if (reup.checkrecordexist()) { 
      addActionError("record already exist."); 
     } 
    } 

    @Override 
    public String execute() { 



     iup.Insert(); 


     return SUCCESS; 
    } 

    public String getUserId() { 
     return userId; 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public int getSecurityLevel() { 
     return securityLevel; 
    } 

    public void setSecurityLevel(int securityLevel) { 
     this.securityLevel = securityLevel; 
    } 
    private String retypepassword; 

    public String getRetypepassword() { 
     return retypepassword; 
    } 

    public void setRetypepassword(String retypepassword) { 
     this.retypepassword = retypepassword; 
    } 



    // * Object setter. 


    public void setEs(EntityStart es) { 
     this.es = es; 
    } 


    public void setIup(InsertUserProfile iup) { 
     this.iup = iup; 
    } 



    public void setReup(RecordExistUserProfile reup) { 
     this.reup = reup; 
    } 



} 

ApplicationContextProvider

package lotmovement.business.crud; 

import org.springframework.beans.BeansException; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 

public class ApplicationContextProvider implements ApplicationContextAware{ 
private static ApplicationContext ctx = null; 
public static ApplicationContext getApplicationContext() { 
return ctx; 
} 
public void setApplicationContext(ApplicationContext ctx) throws BeansException { 
this.ctx = ctx; 
} 
} 

EntityStart

package lotmovement.business.crud; 

import lotmovement.business.CRUDxxxx.*; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.EntityTransaction; 
import javax.persistence.Persistence; 
import lotmovement.business.entity.UserProfile; 

public class EntityStart { 
    EntityManagerFactory factory; 
    EntityManager em; 



    public void StartDbaseConnection() 
    { 

     factory = Persistence.createEntityManagerFactory("LotMovementPU"); 
     em = factory.createEntityManager(); 

    } 

    public void StartPopulateTransaction(Object entity){ 

     EntityTransaction userTransaction = em.getTransaction(); 

     userTransaction.begin(); 

     em.persist(entity); 
     userTransaction.commit(); 
     em.close(); 


    } 

    public void CloseDbaseConnection(){ 
     factory.close(); 
    } 

} 

InsertUserProfile

package lotmovement.business.crud; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.EntityTransaction; 
import javax.persistence.Persistence; 
import lotmovement.business.entity.UserProfile; 
import lotmovement.action.LoginAction; 
import lotmovement.action.RegisterAction; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class InsertUserProfile { 

    ApplicationContextProvider acp = new ApplicationContextProvider(); 

    ApplicationContext ctx = ApplicationContextProvider.getApplicationContext(); 

    UserProfile up; 
    RegisterAction ra; 
    EntityStart es; 

    public void Insert() { 


     up.setUserId(ra.getUserId()); 
     up.setFirstName(ra.getFirstName()); 
     up.setLastName(ra.getLastName()); 
     up.setPassword(ra.getPassword()); 
     up.setSecurityLevel(ra.getSecurityLevel()); 

     es.StartPopulateTransaction(up); 

     es.CloseDbaseConnection(); 

    } 

    public void Update() { 
    } 

    public void Delete() { 
    } 







    public void setUp(UserProfile up) { 
     this.up = up; 
    } 


    public void setRa(RegisterAction ra) { 
     this.ra = ra; 
    } 


    public void setEs(EntityStart es) { 
     this.es = es; 
    } 




} 

Checkrecordexist

package lotmovement.business.crud; 

import lotmovement.action.RegisterAction; 
import lotmovement.business.entity.UserProfile; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class RecordExistUserProfile { 

    RegisterAction ra; 
    EntityStart es; 
    ApplicationContextProvider acp = new ApplicationContextProvider(); 
    ApplicationContext ctx = ApplicationContextProvider.getApplicationContext(); 

    public boolean checkrecordexist() { 

     UserProfile up = es.em.find(UserProfile.class, ra.getUserId()); 

     if (ra.getUserId().equals(up.getUserId())) { 
      return true; 
     } else { 
      return false; 
     } 
    } 


    public void setRa(RegisterAction ra) { 
     this.ra = ra; 
    } 

    public void setEs(EntityStart es) { 
     this.es = es; 
    } 
} 

我在NetBeans中所做的是,我刚添加的春天库在我的类路径中。这些是struts 2.3.4,springframework 2.5.6 SEC03和SpringFramework 3.1.1。我也检查网络,但我找不到类似女巫的东西。

非常感谢。

回答

0

首先,我建议你探索Struts 2的Spring集成,而不是试图整合春天到Struts 2的自己。整合由Strut 2 Spring插件提供。你应该检查一下;它允许您轻松地将bean注入到struts 2操作中,更不用说框架创建的所有其他对象。 Here's a link

但是,即使基于代码片段中的内容,我也可以提供一些关于Spring基础知识的清晰内容。由setter定义的bean属性必须与bean属性名称匹配。没有这个,财产注入将无法工作。但是,这听起来像你改变了字段本身的名称来匹配;这将是现场注入。完全不同的机制。我建议你真的需要了解属性注入,现场注入和构造注入的概念,以及自动布线的概念。对不起,如果我误以为你没有拨打这些东西,好像你不这样做。

但是,我建议你有一个更大的问题,虽然。 bean中有一个依赖循环。考虑这两个:

<bean id="insertUserProfile" class="lotmovement.business.crud.InsertUserProfile"> 
     <property name="registeraction" ref="registeraction"/> 
     <property name="entitystart" ref="entitystart"/> 
     <property name="userprofile" ref="userprofile"/> 

    </bean> 

    <bean id="registeraction" class="lotmovement.action.RegisterAction"> 
     <property name="insertUserProfile" ref="insertUserProfile"/> 
     <property name="entitystart" ref="entitystart"/> 
     <property name="recordexistuserprofile" ref="recordexistuserprofile"/> 
    </bean> 

我不知道是肯定的春天是否允许这一点,但如果没有那就要做出关于注射如何往下走一些武断的决定,因为它是根本不可能满足一个周期一个正常的时尚,我想。

+0

我曾经在网上读过一些基本注射。据我所知,为了实例化这些类,我需要将它放在属性上。属性中的类需要从bean id类实例化。有关我的问题的任何建议?当然,我会尽我所能。我会读更多关于春天:)谢谢 – toksis

+0

是的,我建议你有你的依赖周期。我的意思是你的bean“insertUserProfile”依赖于“注册”bean,反之亦然。当实例化注册bean需要insertUserProfile bean已经创建时,Spring应该如何将“insertUserProfile”注入到“注册”bean中? – chad

+0

嗯。我懂了。 tnx人.. – toksis

0

为这些类添加适当的getters/setters。为简洁起见,我省略了其他代码。我认为这是抱怨,因为你没有定义get方法,它可能也不喜欢字段名称和bean名称不相关。

InsertUserProfile

public class InsertUserProfile { 

    RegisterAction registerAction; 

    public void setRegisterAction(String registerAction){ 
     this.registerAction = registerAction; 
    } 

    public RegisterAction getRegisterAction(){ 
     return this.registerAction; 
    } 
} 

RecordExistUserProfile

public class RecordExistUserProfile { 
     RegisterAction registerAction; 

     public void setRegisterAction(String registerAction){ 
      this.registerAction = registerAction; 
     } 

     public RegisterAction getRegisterAction(){ 
      return this.registerAction; 
     } 
} 
+0

你的意思是说bean名称和字段名称必须相同?与大小写字母? – toksis

+0

我不认为名字需要相同,我相信Spring试图按类型匹配bean。 –

+0

我已经将bean属性名称的值更改为对象数据的变量。像InsertUserProfile iup一样;属性名称= iup。 REF = “InsertUserProfile”;现在编译后运行。 Howerver在这段代码上有错误,目前正在检查为什么会发生这种情况。 显示java.lang.NullPointerException lotmovement.action.RegisterAction.validate(RegisterAction.java:43) 如果(reup.checkrecordexist()){ addActionError( “记录已经存在。”); } – toksis