How Spring Container manage if 2 same bean have different id/name - spring

How Many Objects will be created into container. if configuration will be as given below.
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"><br/>
<property name = "message" value = "Hello World"/>
</bean>
<bean id = "helloWorld1" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World"/>
</bean>

One singleton object per definition in the spring container, if you define N beans then N singleton object of the class will be created.
It will create two instances in your case, by definition Spring container creates a singleton instance per bean.

Related

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

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>

How to use factory bean to create beans in Camel flow

If I have a spring factory bean can I use this factory bean to create my beans within Camel route ?
<bean id="factoryBean" class = "ABC">
<route id ="someId">
<bean id = "someBean" ref ="factoryBean" method = "factoryMethod">
<bean ref = "someBean" method = "someMethod1" />
<bean ref = "someBean" method = "someMethod2" />
</route>
That is not possible in the <route> as it can only call a bean by either the id or the FQN classname. But a factory bean you can setup in Spring or OSGi Blueprint and then give that an id, which you can then just call from a Camel route.

Grails initialization

In my Grails app, I need access to configuration exposed by a Java class similar to the below
public class Config {
private Properties properties = new Properties();
static load(String path) {
File configFile = new File(path);
FileReader fileReader = new FileReader(configFile);
properties.load(fileReader);
}
String getProperty(String name) {
properties.getProperty(name);
}
}
I trigger the initialisation of this class in the first line of Bootstrap.groovy by calling Config.load("/conf.properties"). However, the initialization of various Spring beans needs properties that are exposed by Config, but by the time Bootstrap.groovy is executed, Spring initialization has already completed.
So I need to find a way to call Config.load() before construction of the Spring beans, is this possible? I guess there might be an event handler available in /script/_Events.groovy that I could invoke it from, but I'm not sure which handlers are available.
Unfortunately, changing the source code of Config.java isn't an option, and neither is eliminating my usage of this class.
You could try declaring a suitable bean in web-app/WEB-INF/applicationContext.xml, which is the definition of the root web application context as opposed to the GrailsApplication's internal context.
<bean id="initConfig" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.example.Config" />
<property name="targetMethod" value="load" />
<property name="arguments">
<list><value>/conf.properties</value></list>
</property>
</bean>
and modify the grailsApplication bean to depend on that:
<bean id="grailsApplication" depends-on="initConfig" class="...">

Spring 3.0 RmiProxyFactoryBean: how to change serviceUrl at runtime?

I have a bean definition like this:
<bean id="myService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface" value="org.myapp.MyService"/>
<property name="serviceUrl" value="rmi://localhost:1099/myService"/>
</bean>
I retrieve the service bean in this way:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:rmi-client-config.xml");
MyService myService = context.getBean("myService", MyService.class);
Of course it returns an Instance of "MyService" impl and not RmiProxyFactoryBean.
So how can I change "serviceUrl" parameter using the xml definition above and not manually instancing RmiProxyFactoryBean?
To get the FactoryBean instance instead of the bean created by the factory, use the BeanFactory.FACTORY_BEAN_PREFIX. ie
RmiProxyFactoryBean rpfb = (RmiProxyFactoryBean) contex.getBean("&myService");

Getting an instance of a bean excplicitly in runtime

I have a case where I have a bean (let's call it A) which needs some other bean (B).
This B is retrieved from a static method of a class using MethodInvokingFactoryBean.
This static method depends on the state of the system and will work after the web application is loaded.
I need to access the B only in runtime (no interaction in the constructor).
How can I configure the A to autowire bean B and only initialize it the first time A requires it?
Is using getBean on the Application context the only way?
Thanks!
*Edit - Added some xmls :) *
This is the definition of bean B.
<bean id="api" class="com.foo.API"/>
<bean id="B" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="true">
<property name="targetObject" ref="api"/>
<property name="targetMethod" value="getFactory"/>
<qualifier value="myQualifer"/>
</bean>
This is the definition of bean A.
<bean id="resources.someRESTResourceA" class="com.foo.MyRestResource"/>
I can't use Autowire to wire B into A because it will initialize it (B) on A's construction.
B's targetMethod will only work after the web app has been initialized.
I can use ApplicationContext.getBean("B") inside A, but it's not elegant and will be a problem with unit testing unless I do the following (which is also not desired):
public BInterface getB() {
if (b == null) {
b = ApplicationContext.getBean("B");
}
return b;
}
you should lazily initialize bean A.
<bean id="A" class="demo.A" lazy-init="true">
<property name="b" ref="B"/>
</bean>
You still need to retrieve the bean A from the Spring container when you need it with the getBean() method. It's easily accesible with the ApplicationContextAware interface.
If you autowire bean A into an another bean and that bean is retrieved before bean B is constructed, the Spring container will create bean A at the time it's injected as a property to the another bean.

Resources