Spring Beans By Vyacheslav Grebenyuk AI dept KNURE
Spring Beans By Vyacheslav Grebenyuk AI dept. , KNURE
Introduction n For Spring, all objects are beans! n Object instances are created by the spring framework by looking at their class definitions n The framework then creates a plan objects need to be instantiated n dependencies need to be set and injected n scope of the newly created instance n n …based on configuration metadata n in a simple XML file n as Annotation or Java Configuration 2
Configuring using XML n Define a bean n name my. Bean n corresponds to a class com. oreilly. justspring. ch 2. My. Bean n expects a String value as a constructor argument n defines two properties, property 1 and other. Bean 3
XML file with definition <? xml version="1. 0" encoding="UTF-8"? > <beans xmlns="http: //www. springframework. org/schema/beans" xmlns: xsi="http: //www. w 3. org/2001/XMLSchemainstance" xsi: schema. Location= "http: //www. springframework. org/schema/beans/ spring-beans-3. 0. xsd"> <bean name="my. Bean" class="com. oreilly. justspring. ch 2. My. Bean"> <constructor-arg value="My. Constructor. Argument"/> <property name="property 1" value="value 1"/> <property name="other. Bean" ref="my. Reference. Bean"/> </beans> 4
Bean definition n The name n The class n The scope n The dependencies n The developer should use the name as an API starting point to query the bean instance 5
Beans definition in multiple files n split the bean definitions across multiple files n business-beans. xml n util-beans. xml n dao-beans. xml 6
Anonymous beans n Each bean should either have a name or id n Can create the beans with neither of these things, making them anonymous beans n Are not available to query in your client code 7
Creating Beans n The instances (aka beans) are created, the associations are established, and dependencies are injected by the Spring Framework entirely 8
Life Cycle n The framework factory loads the bean definitions and creates the bean n The bean is then populated with the properties as declared in the bean definitions. If the property is a reference to another bean, that other bean will be created and populated, and the reference is injected prior to injecting it into this bean n If your bean implements any of Spring’s interfaces, such as Bean. Name. Aware or Bean. Factory. Aware, appropriate methods will be called 9
Life Cycle n The framework also invokes any Bean. Post. Processor’s associated with your bean for pre-initialzation n The init-method, if specified, is invoked on the bean n The post-initialization will be performed if specified on the bean 10
Method Hooks n Spring Framework provides a couple of hooks in the form of callback methods: init-method and destroy-method 11
init-method <bean name="file. Reader" class="com. oreilly. justspring. ch 2. File. Reader" initmethod="init"> public class File. Reader implements Reader { private List<Location> locations = null; public void init(){ locations = new Array. List<Location>(); } } 12
destroy-method <bean name="file. Reader" class="com. oreilly. justspring. ch 2. File. Reader" destroy-method="clean. Up"> public class File. Reader implements Reader { private List<Location> locations = null; public void clean. Up(){ locations = null; } } 13
Bean Post Processors n Interfaces: Initializing. Bean or Disposable. Bean public class Connection implements Initializing. Bean, Disposable. Bean { private Object. Name object. Name; public void after. Properties. Set(){ connection. register. To. Jmx(object. Name); } public void destroy(){ connection. unregister. From. Jmx(object. Name); } } 14
Bean Scopes n Singleton Scope n Prototype Scope 15
Singleton Scope n need one and only one instance of a bean (per context or container) <bean name="file. Reader" class="com. oreilly. justspring. ch 2. File. Reader" singleton="true"> The same as <bean name="file. Reader" class="com. oreilly. justspring. ch 2. File. Reader"> 16
Prototype Scope <bean name="trade" ref="com. oreilly. justspring. ch 2. Trade" singleton="false" init-method="init. Id"/> public class Trade { private Atomic. Integer unique. Id = -1; public void Trade() { } public int init. Id() { unique. Id = Atomic. Integer. increment. And. Get(); } } 17
Property Editors n From the declaration, it sounds like the properties are just Strings or Java Primitives n What if we have other types of properties, such as object references, Lists, Custom classes, and other Collections? n Spring follows the Java Bean style property editor mechanism to resolve the actual types 18
Injecting Java Collections: Properties public class JMSSource{ private Properties source. Props = null; public void set. Properties(Properties props){ source. Props = props; } } 19
Injecting Java Collections: Properties <bean name="jms. Source" class="com. oreilly. justspring. ch 2. JMSSource" > <props name="source. Props"> <prop key="1">system_1</prop> <prop key="2">system_2</prop> <prop key="3">system_3</prop> </props> </bean> 20
Injecting Java Collections: Lists, Sets and Maps <bean name="jms. Source" class="com. oreilly. justspring. ch 2. JMSSource"> <props name="source. Props"> <list> <value>system_1</value> <value>system_2</value> <value>system_3</value> </list> </props> </bean> 21
Injecting Java Collections: Lists, Sets and Maps 2 <bean name="jms. Source" class="com. oreilly. justspring. ch 2. JMSSource"> <props name="source. Props"> <map> <entry key="threads"> <value>10</value> </entry> <entry key="number. Of. Retries"> <value>3</value> </entry> <entry key="unique. Key. Gen"> <ref bean="com. oreilly. justspring. ch 2. JMSSource. Key. Gen"/> </entry </map> </props> </bean> 22
Summary n Discussed the Spring framework in detail n Concept of beans and bean factories n The life cycle of the bean, scope, properties editors 23
- Slides: 23