Librerias
spring.jar
commons-logging.jar
Interface IPrototype
package org.dracof.spring.scope;
public interface IPrototype {}
Interface ISingleton
package org.dracof.spring.scope;
public interface ISingleton {}
Implementacion de IPrototype
package org.dracof.spring.scope.impl;
import org.dracof.spring.scope.IPrototype;
public class ClasePrototypeImpl implements IPrototype{
public ClasePrototypeImpl() {
System.out.println("Creando nueva instancia de ClasePrototypeImpl!");
}
}
Implementacion de ISingleton
package org.dracof.spring.scope.impl;
import org.dracof.spring.scope.ISingleton;
public class ClaseSingletonImpl implements ISingleton {
public ClaseSingletonImpl() {
System.out.println("Creando nueva instancia de ClaseSingletonImpl!");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- Por default es singleton -->
<bean id="claseSingleton"
class="org.dracof.spring.scope.impl.ClaseSingletonImpl"
scope="singleton"/>
<bean id="clasePrototype"
class="org.dracof.spring.scope.impl.ClasePrototypeImpl"
scope="prototype"/>
</beans>
Clase de prueba
package org.dracof.spring.test;
import org.dracof.spring.scope.IPrototype;
import org.dracof.spring.scope.ISingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PruebaScope {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ISingleton claseSingleton1 = (ISingleton)context.getBean("claseSingleton");
ISingleton claseSingleton2 = (ISingleton)context.getBean("claseSingleton");
ISingleton claseSingleton3 = (ISingleton)context.getBean("claseSingleton");
IPrototype clasePrototype1 = (IPrototype)context.getBean("clasePrototype");
IPrototype clasePrototype2 = (IPrototype)context.getBean("clasePrototype");
IPrototype clasePrototype3 = (IPrototype)context.getBean("clasePrototype");
}
}
Salida
Creando nueva instancia de ClaseSingletonImpl!
Creando nueva instancia de ClasePrototypeImpl!
Creando nueva instancia de ClasePrototypeImpl!
Creando nueva instancia de ClasePrototypeImpl!
Descargar proyectito
ScopeSpring.zip
Suerte.
No hay comentarios:
Publicar un comentario