Spring container
Spring has two core interfaces: Beanfactory and ApplicationContext, where ApplicationContext is the sub-interface of Beanfactory, which represent the spring container,
The spring container is the factory that produces the bean instances and manages the beans in the container.
1.BeanFactory
The common implementation class for Beanfactory is Defaultlistablebeanfactory, which is simple to use:
public class Springcontainer {public static void Main (string[] args) { Resource rs = new Classpathresource ("Bean. xml "); Defaultlistablebeanfactory beanfactory = new Defaultlistablebeanfactory (); Load XML new Xmlbeandefinitionreader (beanfactory). Loadbeandefinitions (RS); Driver Driver = Beanfactory.getbean ("Driver", driver.class); Driver.drivercar (); }}
Configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><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" Xmlns:util= "Http://www.springframework.org/schema/util" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ Util http://www.springframework.org/schema/util/spring-util.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/http Www.springframework.org/schema/aop/spring-aop.xsd http://www.springFramework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/ SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <bean id=" Driver "class=" com.huan.s Pring.bean.Driver "> <constructor-arg ref=" Car "/> </bean> <bean id=" car "class=" COM.HUAN.SPR Ing.bean.Car "/></beans>
Driver class and Car class:
public class Driver { private car car; Public Driver (car car) { This.car = car; } public void Drivercar () { System.err.println (Car.carname ());} }
public class Car {public String carname () { return ' this is Jeep '; }}
2.ApplicationContext
In most cases, ApplicationContext is used as the spring container, which has all the features of Beanfactory and has additional functionality:
For example: By default all singleton beans are pre-initialized, inheritance Messagesource interfaces provide internationalization, event mechanisms,
Loading multiple configuration files at the same time
To modify code validation pre-initialization:
public class Driver { private car car; Public Driver (car car) { System.out.println ("===== constructor Set value = = =" +car); This.car = car; } public void Drivercar () { System.err.println (Car.carname ());} }
public class Car {public car () {System.out.println ("========= Pre-initialization car======"); } public String Carname () {return ' this is Jeep '; }}
Package Com.huan.spring.base;import Org.springframework.beans.factory.support.defaultlistablebeanfactory;import Org.springframework.beans.factory.xml.xmlbeandefinitionreader;import Org.springframework.context.applicationcontext;import Org.springframework.context.ApplicationContextAware; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.core.io.classpathresource;import Org.springframework.core.io.resource;import Com.huan.spring.bean.driver;public class Springcontainer {public static void main (string[] args) {//USERBEANFA Ctory (); Userapplicationcontext (); } private static void Userapplicationcontext () {ApplicationContext context = new Classpathxmlapplicationcontext ("Bean.xml"); } private static void Userbeanfactory () {Resource rs = new Classpathresource ("Bean.xml"); Defaultlistablebeanfactory beanfactory = new Defaultlistablebeanfactory (); Load XML New XmlbeandefinitionreaDer (Beanfactory). Loadbeandefinitions (RS); Driver Driver = Beanfactory.getbean ("Driver", Driver.class); Driver.drivercar (); }}
Springcontainer created ApplicationContext succeeded and did not get the bean
It is visible that using ApplicationContext to create a container invokes an parameterless construct and injects a dependency, while using beanfactory does not get the bean without
To invoke the parameterless construct and inject the dependent
If you need to prevent the spring container from initializing the singleton Bean in the container, you can set the property lazy-init= "true" in <bean>
3.ApplicationContext event mechanism
ApplicationContext event mechanism mainly Applicationevent class and Applicationlistener interface implementation
- Applicationevent: Container event, must be published by ApplicationContext
- Applicationlistener: Listener, can have any listener bean as
public class EmailEvent extends applicationevent{ private String address; private String text; Public emailevent (Object source) { super (source); } Public EmailEvent (Object source, string address, string text) { super (source); this.address = address; This.text = text; } Public String getaddress () { return address; } public void setaddress (String address) { this.address = address; } Public String GetText () { return text; } public void SetText (String text) { this.text = text; }}
public class Emailnotifier implements applicationlistener<emailevent>{ @Override public Void Onapplicationevent (EmailEvent event) { System.out.println ("email address: = = =" +event.getaddress ()); SYSTEM.OUT.PRINTLN ("message content = = =" +event.gettext ());} }
public class Eventtest {public static void Main (string[] args) { ApplicationContext ctx = new Classpathxmlapplica Tioncontext ("Bean.xml"); Create Event emailevent emailevent = new EmailEvent ("Test", "[email protected]", "test Event"); Ctx.publishevent (emailevent); }}
<?xml version= "1.0" encoding= "UTF-8"? ><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" Xmlns:util= "Http://www.springframework.org/schema/util" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ Util http://www.springframework.org/schema/util/spring-util.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/http Www.springframework.org/schema/aop/spring-aop.xsd http://www.springFramework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/ SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <bean id=" Driver "class=" com.huan.s Pring.bean.Driver "> <constructor-arg ref=" Car "/> </bean> <bean id=" car "class=" com.huan.sp Ring.bean.Car "/> <bean class=" Com.huan.spring.base.EmailNotifier "/></beans>
Of course spring also has some built-in events, such as Requesthandledevent.
4.Bean How to get the spring container
Get spring container can implement Beanfactoryaware interface or Applicationcontextaware interface
public class Book implements Applicationcontextaware { private applicationcontext ctx; @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception { This.ctx = ApplicationContext; } public void Printctx () { System.out.println (CTX); }}
public class Booktest {public static void Main (string[] args) { ApplicationContext ctx = new Classpathxmlapplicat Ioncontext ("Bean.xml"); Book book = Ctx.getbean ("book", Book.class); System.out.println (CTX); Book.printctx (); }}
<?xml version= "1.0" encoding= "UTF-8"? ><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" Xmlns:util= "Http://www.springframework.org/schema/util" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ Util http://www.springframework.org/schema/util/spring-util.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/http Www.springframework.org/schema/aop/spring-aop.xsd http://www.springFramework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/ SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <bean id=" Driver "class=" com.huan.s Pring.bean.Driver "> <constructor-arg ref=" Car "/> </bean> <bean id=" car "class=" com.huan.sp Ring.bean.Car "/> <bean class=" Com.huan.spring.base.EmailNotifier "/> <bean id=" book "class=" Com.huan.spr Ing.base.Book "/></beans>
Spring Learning Note 03