Drop Down

Saturday, September 21, 2019

Spring_App_6

package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amit.Beans.HelloBean;

public class Test {

public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("beanconfig.xml");

HelloBean bean = (HelloBean)context.getBean("tesingscope");
System.out.println("1>>"+bean);
System.out.println("1>>"+bean);

HelloBean bean2 = (HelloBean)context.getBean("testingscope2");
System.out.println("2>>"+bean2);
System.out.println("2>>"+bean2);

HelloBean bean3 = (HelloBean)context.getBean("testingscope2");
System.out.println("3>>"+bean3);
System.out.println("3>>"+bean3);


bean.setName("Amit");
String name = bean.getName();
System.out.println(name);

}

}

//--- for prototype
/*
 * 1>>com.amit.Beans.HelloBean@3cbbc1e0
 * 1>>com.amit.Beans.HelloBean@3cbbc1e0
 * 2>>com.amit.Beans.HelloBean@35fb3008
 * 2>>com.amit.Beans.HelloBean@35fb3008
 * 3>>com.amit.Beans.HelloBean@7225790e
 * 3>>com.amit.Beans.HelloBean@7225790e
 * Amit
 */
//--- for singleton
/*
1>>com.amit.Beans.HelloBean@3cbbc1e0
1>>com.amit.Beans.HelloBean@3cbbc1e0
2>>com.amit.Beans.HelloBean@35fb3008
2>>com.amit.Beans.HelloBean@35fb3008
3>>com.amit.Beans.HelloBean@35fb3008
3>>com.amit.Beans.HelloBean@35fb3008
Amit
* Amit
*/


=======================================================================
package com.amit.Beans;

public class HelloBean
{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}
=======================================================================
<?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.xsd">

<bean id="tesingscope" class ="com.amit.Beans.HelloBean" scope="singleton">

</bean>
<!--<bean id ="testingscope2" class="com.amit.Beans.HelloBean" scope="prototype"> -->
<bean id ="testingscope2" class="com.amit.Beans.HelloBean" scope="singleton">
</bean>
        
</beans>        

No comments:

Post a Comment

Java 8 Notes Pics