Preamble: Start learning Spring core ideas, combined with a cottage lite version of spriing code to learn.
Content: 1. Abstract beanfactory--oriented interface is easier to expand beanfactory-interface is easier to expand. 2. Initialize the bean inside the abstractbeanfactory.
The beandefinition here compares to a richer, more beandefinition-containing class of beans, where we do not instantiate the bean to beandefinition before registering. Instead, the bean is instantiated to beandefinition when it is deferred until it is registered.
Beandefinition:
Package Step2.core;public class Beandefinition {private Object bean;private class Beanclass;private String beanclassname ;p ublic beandefinition () {}public Object Getbean () {return bean;} public void Setbean (Object bean) {This.bean = bean;} Public Class Getbeanclass () {return beanclass;} public void Setbeanclass (Class beanclass) {this.beanclass = Beanclass;} Public String Getbeanclassname () {return beanclassname;} public void Setbeanclassname (String beanclassname) {this.beanclassname = beanclassname;try {This.beanclass = Class.forName (beanclassname);} catch (ClassNotFoundException e) {e.printstacktrace ();}}}
Beanfactory:
Public interface Beanfactory {Object Getbean (string name), void Registerbeandefinition (string name, beandefinition beandefinition);}
Abstractbeanfactory:
Public abstract class Abstractbeanfactory implements Beanfactory{private map<string, beandefinition> Beandefinitionmap = new concurrenthashmap<string, beandefinition> (), @Overridepublic Object Getbean (String name) {return Beandefinitionmap.get (name). Getbean ();} @Overridepublic void Registerbeandefinition (String name,beandefinition beandefinition) {Object bean = Docreatebean ( beandefinition); Beandefinition.setbean (bean); beandefinitionmap.put (name, beandefinition);} /** * Initialize Bean * @param beandefinition * @return * * /protected abstract Object Docreatebean ( Beandefinition beandefinition);}
Autowirecapablebeanfactory:
public class Autowirecapablebeanfactory extends abstractbeanfactory{@Overrideprotected Object Docreatebean ( Beandefinition beandefinition) {try {Object bean = Beandefinition.getbeanclass (). newinstance (); return bean;} catch ( Instantiationexception e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} return null;}}
public class HelloWorldService {public void HelloWorld () { System.out.println ("Hello world! From Step2 ");} }
public class Beanfactorytest {@Testpublic void Test () {//1. Initialize beanbeanfactory beanfactory = new Autowirecapablebeanfacto Ry ();//2. Inject beanbeandefinition beandefinition = new Beandefinition (); Beandefinition.setbeanclassname (" Step2.test.HelloWorldService "); Beanfactory.registerbeandefinition (" HelloWorldService ", beandefinition);//3. Get Beanhelloworldservice HelloWorldService = (helloworldservice) beanfactory.getbean ("HelloWorldService"); Helloworldservice.helloworld ();}}
Spring Core Learning (2) Managing the life cycle of beans