I need to define a string value in Spring context XML file that is shared by multiple beans.
This is how I do it:
<bean id="aSharedProperty" class="java.lang.String"> <constructor-arg type="java.lang.String" value="All beans need me :)"/> </bean>
Creating a java.lang.String bean by passing a constructor argument of java.lang.String seems kludgy.
Is there a shortcut?
I know this property can be passed using PropertyOverrideConfigurer, but I want to keep this property within the XML file.
The name of that XML is simply adding "-servlet" after the name of the dispatcher servlet. So according to the above configuration in web. xml spring will load dispatcher-servlet. xml file from WEB-INF directory of the project.
Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application. Spring-servlet.
From my own experience annotations better than xml configuration. I think in any case you can override xmls and use annotations. Also Spring 4 give us a huge support for annotations, we can override security from xml to annotations e.t.c, so we will have not 100 lines xml but 10 lines Java Code.
Spring allows you to configure your beans using Java and XML.
You can use PropertyPlaceholderConfigurer
and keep values in xml:
<context:property-placeholder properties-ref="myProperties"/> <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="aSharedProperty">All beans need me :)</prop> </props> </property> </bean>
Then you reference it with:
<bean id="myBean" class="my.package.MyClass"> <property name="someField" value="${aSharedProperty}"/> </bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With