Spring Advanced By Vyacheslav Grebenyuk AI dept KNURE
Spring Advanced By Vyacheslav Grebenyuk AI dept. , KNURE
Advanced Concepts n Containers n Instantiating Beans n Bean Post Processors n Event Handling n Auto Wiring 2
Containers n A container is basically a pool of beans created in a memory space by the framework when the application starts up n Beans are loaded lazily (except loading Singletons in the Application. Context container) n Spring containers primarily fall into two categories: Bean Factories and Application Contexts 3
Bean. Factory Container n org. springframework. beans. factory. Bean. Factory interface n Spring provides a few of the implementations: n Xml. Bean. Factory // Instantiate the factory with your beans config file Bean. Factory factory = Xml. Bean. Factory(new File. Input. Stream("trade-beans. xml")); // Use the factory to obtain your Trade. Service bean Trade. Service service = (Trade. Service) factory. get. Bean("trade. Service"); n Mobile phones 4
Application. Context Container n Application. Context extends the Bean. Factory n Three concrete implementations of Application. Context: File. System. Xml. Application. Context n Class. Path. Xml. Application. Context n Web. Xml. Application. Context n Application. Context ctx = new File. System. Xml. Application. Context( "/opt/temp/trade-beans. xml"); 5
Instantiating Beans n Using Constructors n Using Static Methods n Using Factory Methods 6
Using Static Methods public class Trade. Service { private static Trade. Service service = new Trade. Service(); public static Trade. Service get. Trade. Service(){ return service; } } <bean name="trade. Service" class="com. oreilly. justspring. ch 3. Trade Service“ factory-method="get. Trade. Service"/> 7
Using Factory Methods public class Employee. Creator { public Employee create. Employee() { Employee emp = new Employee(); return emp; } public Employee create. Executive() { Employee emp = new Employee(); emp. set. Title("EXEC"); emp. set. Bonus. Grade("GRADE-A"); return emp; } } 8
Using Factory Methods 2 <bean name="emp. Creator" factorbean="employee. Creator" factorymethod="create. Employee"/> <bean name="exec. Creator" factorbean="employee. Creator" factorymethod="create. Executive"/> <bean name="employee. Creator" class="com. oreilly. justspring. ch 3. Employee. Creator"/> 9
Bean Post Processors n Bean. Post. Processor interface n Two callback methods: post. Process. Before. Initialization and post. Process. After. Initialization n post. Process. Before. Initialization method is invoked just before calling your init-method or after. Properties. Set method on the bean n post. Process. After. Initialization method is called just after the initialization of the bean is completed 10
Bean Post Processors 2 n If the container is Bean. Factory, we have to invoke add. Bean. Post. Processor (our. Processor) on the Bean. Factory in order to set the post processor instance. n If the container is Application. Context, defining in the config file is enough! <bean name="bean. Post. Processor" class="com. oreilly. justspring. ch 3. Trade. Post. Processor"/> 11
Event Handling n Listen to the events that may allow you to react on the context n You can use Spring’s framework to publish your custom events n Application. Context publishes certain types of events when loading the beans: Context. Started. Event n Context. Stopped. Event n 12
Listening to Context Events n Bean should implement the Application. Listener interface public class Trade. Context. Event. Listener implements Application. Listener { public on. Application. Event(Application. Event event) { //handle the event here } } 13
Bind the listener to the context <bean id="trade. Ctx. Listener“ class="com. oreilly. justspring. ch 3. Trade. Context. Event. Listener"/> 14
Types of events n Context. Started. Event n Context. Stopped. Event n Context. Refreshed. Event n Context. Closed. Event n Request. Handled. Event 15
Publishing Custom Events public class Trade. Persisted. Event extends Application. Event{ private Trade t = null; private persisted. Time = null; private source = null; public Trade. Persiste. Event(String source, Trade t) {. . . } 16
publish(Application. Event event) public class Trade. Persisted. Event. Publisher implements Application. Event. Publisher. Aware{ private Application. Event. Publisher trade. Event. Publisher; . . . public void set. Application. Event. Publisher( Application. Event. Publisher) { this. trade. Event. Publisher = Publisher; } public void publish(Trade. Persisted. Event event) { trade. Event. Publisher. publish(event); } } n Declare Trade. Persisted. Event. Publisher in the XML file as a normal bean 17
Receiving Custom Events n Create a class that extends the Application. Listener n Handle the on. Application. Event 18
Receiving Custom Events 2 public class Trade. Persisted. Event. Listener implements Application. Listener { public on. Application. Event( Trade. Persisted. Event event) { //handle the event here } } 19
Auto Wiring n Autowiring by. Name n Autowiring by. Type n Autowiring by Constructor n Mixing Autowiring with Explicit Wiring 20
Autowiring by. Name public class Trade. Receiver{ private Persistor trade. Persistor = null; private Transformer trade. Transformer = null; //setters and getters of the above two variables } 21
Autowiring by. Name 2 <bean name="trade. Receiver" class="com. oreilly. justspring. ch 3. Trade. Receiver“ autowire="by. Name"/> <bean name="trade. Persistor" class="com. oreilly. justspring. ch 3. Trade. Persistor"/> <bean name="trade. Transformer" class="com. oreilly. justspring. ch 3. Trade. Transformer"/> 22
Autowiring by. Type n Setting the autowire="by. Type" tells the container that it should look for a bean of type Trade. Persistor n If the container finds the appropriate types, it will inject them into the bean n If container finds more than one bean with the same type defined in the config, a fatal exception is thrown 23
Autowiring by Constructor public class Trade. Persistor { public Trade. Persistor ( Data. Source datasource){. . } } <bean name="trade. Persistor" class="com. oreilly. justspring. ch 3. Trade. Persitor" autowire="constructor"/> 24
Mixing Autowiring with Explicit Wiring <bean name="trade. Receiver" class="com. oreilly. justspring. ch 3. Trade. Receiver" autowire="by. Name"> <property name="trade. Persistor" ref="trade. Persistor"/> </bean> <bean name="trade. Persistor" class="com. oreilly. justspring. ch 3. Trade. Persistor"/> <bean name="trade. Transformer" class="com. oreilly. justspring. ch 3. Trade. Transformer"/> 25
Summary n Fundamentals of Containers and their usage n Using autowiring beans where you do not have to set properties on the beans n Various ways of instantiating the beans, such as static methods or factory methods n Event handling supported by the framework 26
- Slides: 26