spring Bean 属性“xxx”不可写或具有无效的 setter 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14888588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:44:01  来源:igfitidea点击:

Bean property 'xxx' is not writable or has an invalid setter method

springpropertiesjavabeanswritable

提问by Hatem

I have spring web application. I have defined the controller bean which takes the bean of service as property. Also service bean takes the Dao. Dao is tested and working fine. Now the problem with service. Actually i'd make sure about the setters there !

我有 spring 网络应用程序。我已经定义了将服务 bean 作为属性的控制器 bean。服务 bean 也取道。Dao 已经过测试并且工作正常。现在是服务问题。其实我会确定那里的二传手!

so what is the problem ?

那么问题是什么?

Controller Bean :

控制器豆:

<bean id="listTypeController" class="me.web.servlet.controller.ListTypeController">
<property name="typeService" ref="typeService" />
</bean>  

Service Bean :

服务豆:

<bean id="typeService"class="me.general.service.impl.TypeServiceImpl">
<property name="genericDao" ref="genericDao" />
<property name="typeDao" ref="typeDao" />
</bean>

Service class:

服务等级:

 public class TypeServiceImpl implements TypeService {

    private TypeDao typeDao;
        private GenericDao genericDao;
    public TypeDao getTypeDao() {
    return typeDao;
}

public GenericDao getGenericDao() {
    return genericDao;
}
public void setTypeDao(TypeDao typeDao) {
    this.typeDao = typeDao;
}

public void setGenericDao(GenericDao genericDao) {
    this.genericDao = genericDao;
}
}

List Controller:

列表控制器:

public class ListTypeController {

public static final String SEARCH_TYPE_FORM_ATTRIBUTE_NAME = "SearchTypeForm";

private TypeService typeService;

@ModelAttributeSEARCH_TYPE_FORM_ATTRIBUTE_NAME)
public SearchTypeForm createForm() {
    SearchTypeForm form = new SearchTypeForm();
    form.setPageSize(SystemConfiguration.getCurrentConfiguration().getDefaultPageSize());
    form.setActive(Boolean.TRUE);
    return form;
}

@RequestMapping("/administration/types")
public String listTypes(@ModelAttribute(SEARCH_TYPE_FORM_ATTRIBUTE_NAME) SearchTypeForm form,
                             Model model) {
    Page<Type> all = typeService.findTypes(form);
    model.addAttribute("all", all);
    return "/master/general/List";
}


public void setTypeServic(TypeService typeService) {
    this.typeService = typeService;
}
}

The Error :

错误 :

Invalid property 'typeService' of bean class 
[me.web.servlet.controller.ListTypeController]: 
Bean property 'typeService' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?

采纳答案by Ryan Stewart

ListTypeControllerdoesn't have a property of the appropriate type to receive the typeServicebean, or else the setter for that property is malformed. Note that if you have some proxying going on and your ListTypeControllerspecifies the type as TypeServiceImpl, then it may be because you should be referring to the bean by its interface type, TypeService. A proxy of your typeServicewould be a TypeService, but not a TypeServiceImpl.

ListTypeController没有适当类型的属性来接收typeServicebean,否则该属性的 setter 格式不正确。请注意,如果您正在进行一些代理并且您ListTypeController将类型指定为TypeServiceImpl,那么可能是因为您应该通过其接口类型来引用 bean TypeService。您的代理typeService将是TypeService,但不是TypeServiceImpl

Update:Based on your new code: setTypeServicshould be setTypeService, or else your property name is actually typeServic.

更新:根据您的新代码:setTypeServic应该是setTypeService,否则您的属性名称实际上是typeServic

回答by Klapsa2503

In my case i named my propery as: isMyPropertyand isin prefix caused an issue. I had to change the name to myProperty.

在我的情况下,我将我的财产命名为:isMyProperty并且is在前缀中引起了问题。我不得不将名称更改为myProperty.

回答by Mrkvozrout

In my case it was because I had correct setter and getter but each with different type.

就我而言,这是因为我有正确的 setter 和 getter,但每个都有不同的类型。

My setter took String and parsed it to target enum type and my getter returned directly the enum.

我的 setter 使用 String 并将其解析为目标枚举类型,我的 getter 直接返回枚举。

For some reason Spring (v3) got confused.

由于某种原因,Spring (v3) 感到困惑。