How to inject prototype dependency in a singleton bean


As we know once the singleton bean is created all the properties object defined in it are created once. But what about special and rare case in which one of the property object needs to be created everytime the singleton object is required. for example i have put a property in a singleton object as Timestamp, which can tell me when my singleton object was accessed. For this I need to know the exact time when the object is fetched and not created.
Possible solution could be everytime the method “getInstance()” is called a new Timestamp object is created and injected into spring object.
This is also possible in Spring using look-up method injection.
Lookup method injection refers to the ability of the container to override methods on container managed beans, to return the result of looking up another named bean in the container. The lookup will typically be of a prototype bean.

public abstract class SingletonBean{
public abstract Timestamp getCurrentTime();
}


In spring.xml
< bean id="mySingleton" class="com.SingletonBean" >
< lookup-method name="getCurrentTime" bean="currentTimeBean"/ >
< / bean >
< bean id ="currentTimeBean" class="java.sql.TimeStamp" scope="prototype"/ >

It is mandatory to make currentTimeBean as prototype otherwise same instance of bean will be passed.

What is difference between == and equals() in java?


== operator is used to compare the references of the objects.

public boolean equals(Object o) is the method provided by the Object class. The default implementation uses ==
operator to compare two objects.

But since the method can be overridden like for String class. equals() method can be used to compare the values of two
objects.

String str1 = new String("MyName");
String str2 = new String("MyName");

if(str1 == str2){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

if(str1.equals(str2)){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

Output:
Objects are not equal
Objects are equal

Lets look at other snippet:

String str2 = "MyName";
String str3 = str2;

if(str2 == str3){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

if(str3.equals(str2)){
System.out.println(“Objects are equal”)
}else{
System.out.println(“Objects are not equal”)
}

Objects are equal
Objects are equal

Can a static block throw exception?


Yes, static block can throw only Runtime exception or can use a try-catch block to catch checked exception.
Typically scenario will be if JDBC connection is created in static block and it fails then exception can be caught, logged and application can exit. If System.exit() is not done, then application may continue and next time if the class is referred JVM will throw NoClassDefFounderror since the class was not loaded by the Classloader.

how to access properties file in Spring


PropertyPlaceHolderConfigurer A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.

The default placeholder syntax follows the Ant / Log4J / JSP EL style: ‘$’

PropertyOverrideConfigurer A property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions
Example:
A simple class of which values are set from property file.

public class MyBean {

private String myName;
private String password;
/**
* @param args
*/
public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mybean.xml");
MyBean bean = (MyBean)ctx.getBean("myBean");
// getting the properties from the property file
System.out.println(bean.getMyName());
System.out.println(bean.getPassword());
}
public String getMyName() {
return myName;
}
public void setMyName(String myName) {
this.myName = myName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

The xml file config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">

<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>config.properties</value>
</list>
</property>
</bean>

<bean id="myBean" class="test.tetst.MyBean">
<property name="myName" value="${myname}" />
<property name="password" value="${password}" />
</bean>
</beans>

config.properties files
myname=testing
password=password