Spring Core Learning (2) Managing the life cycle of beans

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.