Problems overriding a property to null in Spring bean declaration - spring

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: 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.

Related

Init-method from properties

I would like to declare a bean for which init-method is not a constant value, but depends on a property:
<bean id="XXX" class="YYY" init-method="${some.property}"/>
I have a PropertyPlaceholderConfigurer bean already configured, but unfortunately spring tries to call method named ${some.property} instead of the property value.
What should I configure in order to spring call the method pointer by some.property value?
To do this, you'll need to include the following bean and put the path to the properties file in the location property:
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean" id="appConfig">
<property name="location" value="classpath:appConfig.properties"></property>
</bean>
Let's suppose your properties file contains the following entry:
init_method = test
Then, by using your own code snippet, you should refer to aforementioned property in this way:
<bean id="XXX" class="YYY" init-method="#{appConfig['init_method ']}"/>
The appConfig must match bean's id declared on the first snippet

Spring EL template in a spring bean definition

I have a bean that has a map injected. The entries in the map are of type myBean (see below) and are defined in my spring XML. It may be that not all properties of myBean are defined in the xml, so I thought I would provide defaults in the form of a template where the variable bit in the template is provided by another property that will be present (p1 below). I hope that makes sense.
I am sure there are other ways to achieve this; well, I know there are, but I am new to Spring and I came across Spring EL and it sounded like it was suited to this kind of thing, so I tried this:
<bean id="myDefaults" class="com.myco.MyDefaults">
<property name="prop1" value="abc#{this.p1}def"/>
</bean>
<bean id="myBean" class="com.myco.MyBean" abstract="true">
<property name="theDefaults" ref="myDefaults"/>
</bean>
In MyBean the value of p1 is 100, and I was hoping that the value of theDefaults would be abc100def, but instead I get an error:
Error creating bean with name 'myDefaults' defined in class path resource [myapp-spring.xml]:
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'this' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Thanks for any help
Paul
The variable is #this, not this but in any case, it's not needed in this context, it's implied.
Use value="abc#{p1}def"

Spring bean creation

Is it possible to create to bean with same id with same class with different property in spring ? Like:
<bean id ="a" class= "com.tofek.A"
<property message = "khan"/>
</bean>
<bean id = "a" class = "com.tofek.A"
<property message="tofek"/>
</bean>
As per my understanding it will create, but while fetching the bean using getBean() method it will give exception like NoBeanDefinitionFoundException.
Please correct my understanding if I'm wrong?
Make sure your spring context is loaded sucessfully.
Answering your question. You can have two identical bean definitions in two different sprintContext configurations.
The bean from second context will override bean created by first one.
For example :
context1.xml
<bean id="bean1" class="org.springframework.beans.TestBean"/>
context2.xml
<bean id="bean1" class="org.springframework.beans.TestBean"/>
then, the bean from context2.xml will override bean created by contex1.xml.
It of course depends on order of creating spring contexts. The laters overrides the ones made before.
You can use getBean() to fetch bean by type or name. In this case, both bean have same id's and types, the spring wouldn't know which one you want to fetch.

How to assign java.util.logging.Level to a bean?

I created an application and want to config its logging.Level thorugh bean
<bean id="loggingLevel" class="java.util.logging.Level"> <constructor-arg> <value>INFO</value> </constructor-arg> </bean>
but it failed. Here is the error message:
Unsatisfied dependency expressed through constructor argument with index 1 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
So how to do it the right way?
Instances of java.util.logging.Level are not intended to be instantiated this way, they should be obtained from static fields. In Spring you may use <util:constant>:
<util:contstant id = "loggingLevel" static-field="java.util.logging.Level.INFO" />

Create bean of type Set<Class<?>>

How can I create a bean of type Class?
I found a way using getClass() but that requires an instance and cannot be used via factory-method since it is not static. It also requires an extraneous bean be created for this express purpose:
<bean id="foo" class="Foo" />
<bean id="fooClass" factory-bean="foo" factory-method="getClass" />
This isn't so bad if the Foo class is easy to construct, but what if the constructor has required parameters?
I then need to create a Set of Class to wire into another bean via a property. I would create the Set such as:
<util:set id="classSet">
<ref local="fooClass"/>
</util:set>
If you really wanted to do what you describe, then you can do it like this:
<bean id="myClass" class="java.lang.Class" factory-method="forName">
<constructor-arg value="com.MyClass"/>
</bean>
But as #ChssPly76 said, if you want to inject it into another bean, you only need inject the class name, and Spring will auto-convert it into a class instance for you.
Why would you? Can you provide an example where that's actually needed?
If you only need this as a dependency (e.g. some other bean has a property of type Class), Spring's built-in ClassEditor property editor would convert a regular string into a Class instance with that name for you:
<property name="someClass" value="java.lang.String"/>
The above would result in setSomeClass(Class clazz) setter being called on the bean whose property that is.

Resources