У меня есть два 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 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)
Стоит отметить, что все геттеры и сеттеры присутствуют в обоих компонентах. SimpleBean.java имеет свойство типа ArgumentBean.java.
Определение простого компонента:
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. Этот тег не работает с фабричными методами, но хорошо работает с аргументами конструктора. Может ли кто-нибудь помочь мне в понимании этой ситуации?
SimpleBean
. Похоже, вы пытаетесь использовать инъекцию как конструктора, так и сеттера, и вы нигде не показали, что у вас действительно есть сеттер. - person chrylis -cautiouslyoptimistic-   schedule 07.01.2014Does the parameter type of the setter match the return type of the getter?
Так ли это? - person Callahan   schedule 07.01.2014