<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="spring.bean.scope.annotations"/>
</beans>
packagespring.bean.scope.annotations;
import org.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
packagespring.bean.scope.annotations;
importorg.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSingleton {
public static void main( String[] args ) {
ApplicationContext context = newClassPathXmlApplicationContext(
new String[] {"spring/bean/scope/annotations/Spring-Customer.xml"});
Employee custA = context.getBean("employee", Employee.class);
//retrieve it again
Employee custB = context.getBean("employee", Employee.class);
if(custA == custB) {
System.out.println("both are same instance : prototype");
} else {
System.out.println("both are different instance : prototype");
}
}
}
Output:
both are different instance : prototype
No comments:
Post a Comment