6:08 am - Sunday October 22, 2017

Spring Singleton Example Program in Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure Jar Files   How To Add Jar Files:  

 beans.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<!-- non-singleton examples -->
	<bean id="nonSingleton" class="java.lang.String" singleton="true">
		<constructor-arg>
			<value>Hello</value>
		</constructor-arg>

	</bean>

</beans>

  NonSingleton.java         

package com.candidjava;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class NonSingleton {

    public static void main(String[] args) {

        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("config/beans.xml"));

        String s1 = (String)factory.getBean("nonSingleton");
        String s2 = (String)factory.getBean("nonSingleton");
        System.out.println("Singleton is true following output will displayed");
        System.out.println("Identity Equal?: " + (s1==s2));
        System.out.println("Value Equal:? " + s1.equals(s2));
        System.out.println("s1="+s1);
        System.out.println("s2="+s2);
        System.out.println("s1.hashcode="+s1.hashCode());
        System.out.println("s2.hashcode="+s2.hashCode());

    }
}

OUTPUT 1

 OUTPUT 2

    DOWNLOAD + SOURCECODE +JARFILES

Filed in: Z-A Spring Codes

Comments are closed.