Spring Core Learning (3) inject attributes for Bean

Source: Internet
Author: User
Tags acer

Leading: Start learning Spring core ideas and learn with a cottage Lite spring code.


Content: 1. Propertyvalue-Save Property Injection information. 2.autowirecapablebeanfactory-can automatically assemble the beanfactory.

Here we redefine the beandefinition, add the attribute list to this field, we will add additional attributes for the bean, so we set the propertyvalues, propertyvalue to assist, When we define the bean for the same time set his properties, through the reflection mechanism to dynamically attach to the bean, to achieve similar configuration file effect.

Beandefinition:

public class Beandefinition {private Object bean;private class Beanclass;private String beanclassname;private Propertyvalues propertyvalues;public 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 ();}} Public propertyvalues getpropertyvalues () {return propertyvalues;} public void Setpropertyvalues (Propertyvalues propertyvalues) {this.propertyvalues = propertyvalues;}}
PropertyValue:

/** * User Bean attribute injection * @author Acer * */public class PropertyValue {private final String name;private final Object Value;publ IC PropertyValue (String name, Object value) {this.name = Name;this.value = value;} Public String GetName () {return this.name;} Public Object GetValue () {return this.value;}}

Propertyvalues:

/** * Package An object for all PropertyValue * @author Acer * */public class Propertyvalues {private final list<propertyvalue> Prope Rtyvaluelist = new arraylist<propertyvalue> ();p ublic propertyvalues () {}public void Addpropertyvalue ( PropertyValue PV) {//todo: Here can be used to judge the repetition of PropertyName, directly with List cannot do This.propertyValueList.add (PV);} Public list<propertyvalue> getpropertyvaluelist () {return propertyvaluelist;}}


Beanfactory:

Public interface Beanfactory {Object Getbean (string name), void Registerbeandefinition (string name, beandefinition beandefinition) throws Exception;}

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) throws exception{object bean = Docreatebean (beandefinition); Beandefinition.setbean (bean); beandefinitionmap.put (name, beandefinition);} /**     * Initialize Bean     * @param beandefinition     * @return *     *    /protected abstract Object Docreatebean ( Beandefinition beandefinition) throws Exception;}

Autowirecapablebeanfactory:

/** * can automatically load content of Beanfactory * @author Acer * */public class Autowirecapablebeanfactory extends abstractbeanfactory{@Overrid eprotected Object Docreatebean (beandefinition beandefinition) throws Exception{object Bean = createbeaninstance ( beandefinition); Applypropertyvalues (bean, beandefinition); return bean;} Protected Object createbeaninstance (Beandefinition beandefinition) throws Exception {return Beandefinition.getbeanclass (). newinstance ();} protected void Applypropertyvalues (Object bean, beandefinition mbd) throws Exception {for (PropertyValue propertyvalue:m Bd.getpropertyvalues (). Getpropertyvaluelist ()) {Field Declaredfield = Bean.getclass (). Getdeclaredfield ( Propertyvalue.getname ());d eclaredfield.setaccessible (True);d eclaredfield.set (Bean, Propertyvalue.getvalue ());}}

HelloWorldService:

public class HelloWorldService {    private String text;    public void HelloWorld () {        System.out.println (text);    }    public void SetText (String text) {        this.text = text;    }}

Beanfactorytest:

public class Beanfactorytest {@Testpublic void Test () throws exception{//1. Initialize beanfactorybeanfactory beanfactory = new Au Towirecapablebeanfactory ();//2.bean definition beandefinition beandefinition = new Beandefinition (); Beandefinition.setbeanclassname ("Step3.test.HelloWorldService");//3. Setting properties Propertyvalues propertyvalues = new Propertyvalues ();p ropertyvalues.addpropertyvalue (New PropertyValue ("text", "Hello world! From step3! ")); Beandefinition.setpropertyvalues (propertyvalues);//4. Generate Beanbeanfactory.registerbeandefinition (" HelloWorldService ", beandefinition);//5. Get Beanhelloworldservice HelloWorldService = (helloworldservice) Beanfactory.getbean ("HelloWorldService"); Helloworldservice.helloworld ();}}


Spring Core Learning (3) inject attributes for Bean

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.