我有两个bean,SimpleBean和ArgumentBean,我试图通过工厂方法连接bean并尝试在相同的bean定义中替换bean(我知道这样做非常糟糕,但我只是在做一个实验)
即。我有以下工厂实用工具类:
public class FactoryClassUtils {
public static ArgumentBean getArgumentBean(String firstArg, int secondArg) {
return new ArgumentBean(firstArg, secondArg);
}
public static SimpleBean getSimpleBean(ArgumentBean firstArg, int secondArg) {
return new SimpleBean(firstArg, secondArg);
}
}
和下面提到的配置已在spring config xml:
中指定 <bean name="argumentBean" class="com.view.spring.factory.factoryclasses.FactoryClassUtils"
factory-method="getArgumentBean">
<constructor-arg type="java.lang.String" index="0" value="Hey All!!!" />
<constructor-arg type="int" index="1" value="20" />
</bean>
<bean name="simpleBean"
class="com.view.spring.factory.factoryclasses.FactoryClassUtils"factory-method="getSimpleBean">
<constructor-arg>
<bean class="com.tcs.view.spring.dataobjects.ArgumentBean"/>
</constructor-arg>
<constructor-arg type="int" value="20" />
<property name="argumentBean" ref="argumentBean" />
</bean>
我在上面执行时收到此错误:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'argumentBean' of bean class [com.view.spring.dataobjects.SimpleBean]: Bean property 'argumentBean' 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:805)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:655)
值得一提的是,所有的getter和setter都出现在两个bean中。 SimpleBean.java具有Type ArgumentBean.java的属性。
Simple Bean的定义是:
package com.view.spring.dataobjects;
public class SimpleBean {
private ArgumentBean argumentBean;
int argumentCount;
public ArgumentBean getArgument() {
return argumentBean;
}
public void setArgument(ArgumentBean argumentBean) {
this.argumentBean= argumentBean;
}
public int getArgumentCount() {
return argumentCount;
}
public void setArgumentCount(int argumentCount) {
this.argumentCount = argumentCount;
}
public SimpleBean(ArgumentBean argument, int argumentCount) {
this.argumentBean= argument;
this.argumentCount = argumentCount;
}
public void letHimSay() {
argumentBean.saySomething();
System.out.println(this.argumentCount);
}
}
和ArgumentBean的定义是:
package com.view.spring.dataobjects;
public class ArgumentBean {
private String sayString;
private int anotherArg;
public ArgumentBean(String sayString, int anotherArg) {
this.sayString = sayString;
this.anotherArg = anotherArg;
}
public ArgumentBean(String sayString, Integer anotherArg) {
this.sayString = sayString;
this.anotherArg = anotherArg.intValue();
}
public ArgumentBean() {
}
public void saySomething() {
System.out.println("We say: "+this.sayString);
System.out.println(this.anotherArg+" times");
}
}
如果从simpleBean定义中删除属性标记,我不会收到任何错误。此标记不适用于工厂方法,但在使用构造函数参数尝试时效果很好。有人可以帮我理解这种情况吗?
答案 0 :(得分:2)
您需要在FactoryClassUtils类
中为argumentBean添加setter方法 public class FactoryClassUtils {
private ArgumentBean argumentBean = null;
public static ArgumentBean getArgumentBean(String firstArg, int secondArg) {
return new ArgumentBean(firstArg, secondArg);
}
public static SimpleBean getSimpleBean(ArgumentBean firstArg, int secondArg) {
return new SimpleBean(firstArg, secondArg);
}
public void setArgumentBean(Argumentbean argumentBean){
this.argumentBean = argumentBean;
}
}
这是必需的,因为你已经将argumentbean定义为xml中FactoryClassUtils的属性
<bean name="simpleBean"
class="com.view.spring.factory.factoryclasses.FactoryClassUtils"factory-method="getSimpleBean">
<constructor-arg>
<bean class="com.tcs.view.spring.dataobjects.ArgumentBean"/>
</constructor-arg>
<constructor-arg type="int" value="20" />
<property name="argumentBean" ref="argumentBean" />
按照Java命名约定
为属性设置setter和getter方法总是好的答案 1 :(得分:-1)
感谢大家投入时间并为我提供帮助。问题解决了。问题出在我的代码上。 SimpleBean.java中的bean名称是参数而不是argumentBean。我指的是变量名,但应该使用getter名称,除了'get'后缀(当然首字母小写)。我将配置更改为: &LT; property name =“argument”ref =“argumentBean”/&gt;它解决了这个问题。