开发者

Problems overriding a property to null in Spring bean declaration

开发者 https://www.devze.com 2022-12-24 11:12 出处:网络
Just wondering if there is a way in Spring to have a parent controller: <bean id=\"parentController\" class=\"org.springframework.web.portlet.mvc.SimpleFormController\" abstract=\"true\">

Just wondering if there is a way in Spring to have a parent controller:

<bean id="parentController" class="org.springframework.web.portlet.mvc.SimpleFormController" abstract="true">
     <property name="validator" ref="validatorImpl"/>  
     ...
</bean>

, and a class extending it:

<bean id="child1Controller" class="com.portlet.controller.Child1Controller" parent="parentController">
   <property name="validator"><null/></property>
     ...
</bean>

<bean id="child2Controller" class="com.portlet.controller.Child2Controller" parent="parentController"> 
     ...
</bean>

, in such a way that the child overrides a property to null.

I know if you don't declare the property in either the parent or the child, you get the wanted effect, but as in most of the places validator refers to validatorImpl, I thought as a inheritance principle, I would be able to override a property to null.

I keep getting:

15:29:50,141 ERROR [PortletHotDeployListener:534] org.springframework.beans.factory.BeanCreationException: 开发者_JS百科Error creating bean with name 'childController' defined in PortletContext resource [/WEB-INF/context/sugerencia-context.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'childController' defined in PortletContext resource [/WEB-INF/context/sugerencia-context.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException

On the other hand,

 <bean id="parentController" class="org.springframework.web.portlet.mvc.SimpleFormController" abstract="true">
  ...
</bean>

 <bean id="child1Controller" class="com.portlet.controller.Child1Controller" parent="parentController">
     ...
</bean>

 <bean id="child2Controller" class="com.portlet.controller.Child2Controller" parent="parentController">
    <property name="validator" ref="validatorImpl"/> 
     ...
</bean>

Thanks.


Explicitly setting a property to <null/> isn't the same as not setting it. The property's setter method may check that the value you're injecting is non-null, for example.

If you look at the source code for BaseCommandController (which is a superclass of your controller), you'll see that setValidator does no such checking. However, when the bean is initialized in initApplicationContext(), it iterates over the array of validators, assuming they're all non-null, and will throw a NPE if there's a null in that array, which if likely what's happening here.

Unfortunately, there is no way to "unset" a property that has been configured in a parent bean definition. You'll need to rearrange the definitions so that the parent doesn't set it.


I think the problem is in your parent you are either using the validator in either setter method or in afterPropertiesSet. That may be the reason for the NPE. Check it once, if you are not using the validator which in your case is null anywhere in the initialization phases in your parent class, it should work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消