100 likes | 339 Views
Spring Bean Scope and Method Injection. www.java9s.com. Bean Scopes. <bean name =“student” class =“Student” scope =“prototype” />. Singleton scope. ctx.getBean(“student”). Spring Container. Single student instance. Prototype scope. ctx.getBean(“student”). Spring Container.
E N D
Spring Bean Scope and Method Injection www.java9s.com
Bean Scopes <bean name =“student” class =“Student” scope =“prototype”/>
Singleton scope ctx.getBean(“student”) Spring Container Single student instance
Prototype scope ctx.getBean(“student”) Spring Container Multiple Beans
Method Injection – Method Replace class MobileStore{ public String buyMobile(){ return "Bought a Mobile Phone"; }} class MobileStoreReplacer implements MethodReplacer{ public Object reimplement(Object obj, Method method, Object[] args) throws Throwable{ return “Bought an iPhone”; } } <bean id =“mobileStore” class =“MobileStore”> <replace-method name =“buyMobile” replacer =“mobileStoreReplacer”/> </bean> <bean id =“mobileStoreReplacer” class =“MobileStoreReplacer”/>
Lookup Method Injection public abstract class BookStore { public abstract Book orderBook(); } public interface Book { public String bookTitle(); } • The ability of the container to override methods on container managed beans, to return the lookup result for another named bean in the container. Managed by Spring public class StoryBook implements Book{ public String bookTitle() { return "HarryPotter“; } } public class ProgrammingBook implements Book{ public String bookTitle() { return "spring programming“; } }