How to initialize a bean basis on propertiy file boolean key value pair - spring

I am new to spring, Is there any way to initialize spring bean using property file key-value pair;
; if like my config.properties file contains isSetup = true;
then only initialize, <bean id="a" class="com.Setup"/>

Please refer this post
How can I inject a property value into a Spring Bean which was configured using annotations?
This link can also help you : http://springinpractice.com/2008/12/02/new-stuff-in-spring-30/
You can pass the property as below to bean :
<bean id="a" class="com.Setup"/>
<property name="isSetup " value="#{systemProperties.isSetup }"/>
</bean>

Related

Spring Core-Setter Injection

I need help regarding mechanism of setter injection in Spring 3.0
Following is my code
package com.common;
class B{
public B(){}
}
class A{
B b;
public A(){}
public void setB(B b){
syso("I am in setter of B");
}
}
In XML,I have,
<bean id="A" class="com.common.A">
<property name="b" ref="B" />
</bean>
<bean id="B" class="com.common.B">
Now,my question is, How does spring container knows that it has to inject dependency through setB(B b) method? What if I change name of setter as newSetB(B b)? I hope I have made my doubt clear.
whenever you have something like this in your property file
<bean id="A" class="com.common.A">
<property name="b" ref="B" />
</bean>
spring container by default searches for setter of that property i.e setPropertyName() after invoking a no-argument constructor of that class whose bean is being prepared, and as it confines to JavaSpecification it searches for method named as setPropertyName()
You can verify this by chaning the name of method or by removing the method entirely
you will get the exception telling that, No Public setter found for that particular property
hope this helps!
Good luck!
The mapping here happens in this way.
Lets say we have following bean
<bean id="A" class="com.common.A">
<property name="testProperty" ref="B" />
</bean>
Spring container tries to get set method as setTestProperty(), if not found it will through an error.
Hope it helps - Cheers !
when ever SpringContainer reads the xml file and finds this
<bean id="A" class="com.common.A">
<property name="b" ref="B"/>
</bean>
<bean id="B" class="com.common.B"/>
firstly it creates object for Bean A, after that it initializes the member variables here "public B b", b is member variable for Bean class A, so it searches for setter method setB(B b) if it finds it sets else if NO setB(B b) SETTER Method is FOUND (OR) SETTER Method named DIFFERRENTLY ex: newSetB(B b) it throws exception as
Invalid property 'b' of bean class [com.common.A]: Bean property 'b' is not writable or has an invalid setter method.
setter methods convention for SpringContainer is setPropertyname()

With constructor autowiring Spring does not throw an exception when there are multiple implementation types

There are interfaces Work and You. There are implementation types WorkImpl and YouImpl and YouImpl2.
I use constructor autowire to inject a You implementation instance in a WorkImpl instance.
Because there are multiple You implementations types, I thought Spring would throw an exception. But Spring instantiates an instance of one of the implementation types, in my case it was YouImpl. This is what I do not understand.
The configuration file is partly,
<bean
id="work"
class="my.test.own.spring_book.WorkImpl"
autowire="constructor"
>
<property name="age" value="52"/>
<property" name="name" value="Foo Bar"></property>
</bean>
<bean
id="you"
class="my.test.own.spring_book.YouImpl"
>
</bean>
<bean
id="you2"
class="my.test.own.spring_book.YouImpl2"
>
</bean>
WorkImpl has one constructor,
public WorkImpl(You you) {
this.you=you;
}
There are few types of autowiring using configuration approach:
byName
byType
constructor
autodetect:- Similar to byType, but type applies to constructor arguments.
Spring container looks at the constructor of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file.
<bean id="you" class="my.test.own.spring_book.YouImpl">
</bean>
<bean id="you2" class="my.test.own.spring_book.YouImpl2">
</bean>
It will match with you as name of parameter used in constructor is you
public WorkImpl(You you) {
this.you=you;
}
In order to avoid this you can use autowire-candidate="false" hence that bean will not take part in autowiring
<!-- This bean will not be injected-->
<bean id="you" class="my.test.own.spring_book.YouImpl" autowiring-candidate="false">
</bean>
<bean id="you2" class="my.test.own.spring_book.YouImpl2">
</bean>
Above is the answer of your question. But I will try to explain more so I can use this answer for future if I forget.
Now suppose you don't give id attribute to the bean or value of id attribute is different than the constructor parameter name.
<bean id="you1" class="my.test.own.spring_book.YouImpl" autowiring-candidate="false">
</bean>
<bean id="you2" class="my.test.own.spring_book.YouImpl2">
</bean>
Spring container searches any bean with type You, yes found two. Do next step
Spring container sees any bean with name(i.e id="you") you. No
It throws exception Unsatisfied dependency Injection
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'work' defined in class path resource [autowire-contructor.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type
[my.test.own.spring_book.You]: : No unique bean of type [my.test.own.spring_book.You] is defined: expected single matching bean but found 2: [you1, you2]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

Bind values in property file dynamically using Spring

In my web application i have a scenario like i need to read the values from property file and the value has to be updated dynamically in spring bean. I have created the key value pair as follows,
message1=Hi {0} welcome. Your last signed in was {1}
How to substitute the values for {0} and {1}. I have read the value using property place holder configurer in spring.
You have to do it with MessageSource
In your applicationContext add:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
In your javacode you can write:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//...
String name = context.getMessage("message1", new Object[] { "user XY", "05.05.2010" }, Locale.US);
You even can use different message.properties files for different languages: Then you have to name these files: [filename]_[languageCode] (i.e. messages_en_US.properties or messages_de_DE.properties). And change your 3rd parameter of the getMessage method.

How can i use a specific proprety for each bean call in Spring

I'm developing an application using spring . I have a bean that i need to call multiple times but for each call i need to change the properties values dynamically . Is there a way to do this .
I had an idea to set the bean properties as an array ,in eatch array i put the parameters that i want to use . For example array[0] contains the params of the first call , array[1] params of the second call,... is it possible to do that ?
Here is a code sample :
<bean class="Dummy2">
<!-- or a list of values -->
<property name="foos">
<util:list>
<value>A,b,c</value>
<value>X,y,z</value>
<value>1,2,3</value>
<value>7,8,9</value>
</util:list>
</property>
</bean>
the setter
#Override
public void setFoo(list<String[]> args) {
...
}
If any one have a better idea or a usefull idea i will be grateful
Thank You
This is the propreties of the bean that calls the beans
<property name="activities">
<list>
<ref bean="1"/> //Calling bean 1
<ref bean="2"/> //Calling bean 2
<ref bean="1"/> //Calling bean 1 again
<ref bean="2"/>//Calling bean 2 again
<ref bean="2"/>
</list>
</property>
i need to use different parameters for each call (call the setter with different values)
Would using Spring's PostConstruct annotation on an initialiser method on your 'master' bean be useful? You could use simple setter methods to inject both the list of beans and the list of configurations into the master bean, and in the init method (annotated with #PostConstruct) configure each bean correctly.
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations
Are you familiar with AOP you can use aspect to do this using #Before Advice to set your method properties before calling it

Reading valued from properties file at Runtime

I want to get specific value based on request from the property file.how to do that?
I have following spring configuration.i want to set the value for Exprops as per the request and get corresponding values from the properties file
<bean id="Prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:ErrorMessage.properties</value>
</property>
</bean>
<bean id="PropertiesBean" class="com.util.PropertiesUtil">
<property name="Exprops" value="${EXampleExceptiion}"></property>
</bean>
Use the PropertiesFactoryBean to inject the Properties in a Bean.
<bean id="myPropertiesBean"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:ErrorMessage.properties"/>
</bean>
This provides a Properties Object/Bean which can be injected under the name myPropertiesBean in any Bean (<property name="x" ref="myPropertiesBean"/>).
In addition Spring provides the util namespace (since Spring 2.5):
There you can write the PropertyFactoryBean definition a bit shorter:
<util:properties id="myPropertiesBean"
location="classpath:ErrorMessage.properties"/>
#see Spring Reference Chapter C.2.2.3.
All use the following for doing this programmatically
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);
<util:properties id="" location="location of prop file" />
this return java.util.Properties object

Resources