Put a few basic classes first:
Code Listing 1#user Class
Package Test;public class User{private String name; User () {}user (String name) {this.name = name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Code Listing 2#userdao.java
Package Test;public class Userdao{public void Add (user user) {System.out.println ("Add User:" +user.getname ())}}
Code Listing 3#userservice.java
Package Test;public class Userservice{private Userdao Userdao; UserService () {}userservice (Userdao userdao) {This.userdao = Userdao;} public void Add (user user) {userdao.add (user);} Public Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;};}
Start the test case below
Implementing Beanfatory injection and dependency binding in code is basically a way to understand what the Getbean method is about.
Interface Beandefinitionregistry implements the Defaultlistablebeanfactory interface,
The defaultlistablebeanfactory implements the Beanfactory interface.
Code Listing 4#test.java implementation of Beanfatory injection and dependency binding through code
Package Test;import Org.springframework.beans.mutablepropertyvalues;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.config.constructorargumentvalues;import Org.springframework.beans.factory.support.abstractbeandefinition;import Org.springframework.beans.factory.support.beandefinitionregistry;import Org.springframework.beans.factory.support.defaultlistablebeanfactory;import Org.springframework.beans.factory.support.rootbeandefinition;public class Test {public static void main (string[] args ) {Defaultlistablebeanfactory beanregister = new Defaultlistablebeanfactory (); Beanfactory beanfactory = getfactory (Beanregister); UserService UserService = (userservice) beanfactory.getbean ("UserService"); Userservice.add (New User ("Test"));} public static Beanfactory GetFactory (Beandefinitionregistry registry) {Abstractbeandefinition Userdao = new Rootbeandefinition (Userdao.class); Abstractbeandefinition userservice = new Rootbeandefinition (userservice.class);//Register the Bean in the container registry.registerbeandefinition ("Userdao", Userdao); Registry.registerbeandefinition ("UserService", UserService);//Bind dependency//1. constructorargumentvalues argvalues = new Constructorargumentvalues () by construction method; Argvalues.addindexedargumentvalue (0, Userdao);//argvalues.addindexedargumentvalue (1, ...); Userservice.setconstructorargumentvalues (argvalues)//2. Injecting Mutablepropertyvalues propertyvalues by setter method = new Mutablepropertyvalues ();p ropertyvalues.addpropertyvalue ("Userdao", Userdao); Userservice.setpropertyvalues ( propertyvalues);//Bind complete return (Beanfactory) registry;}}
Through the XML configuration file implementation, debug is starting from this code,, parsing "Classpath:userservice.xml" string, through the resource interface read resources (here is classpathresouce), Parse bean label, generate corresponding Beandefinition,, of course, need appropriate trade-offs, or code too much to see, the key is not necessarily understand
Code listing 5#testfromxml.java via XML configuration file
Package Test;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.support.defaultlistablebeanfactory;import Org.springframework.beans.factory.xml.xmlbeandefinitionreader;public class Testfromxml {public static void main ( String[] args) {defaultlistablebeanfactory beanregister = new Defaultlistablebeanfactory (); Beanfactory beanfactory = getfactory (Beanregister); UserService UserService = (userservice) beanfactory.getbean ("UserService"); Userservice.add (New User ("Test"));} private static Beanfactory GetFactory (Defaultlistablebeanfactory beanregister) {Xmlbeandefinitionreader Xmlbeanreader = new Xmlbeandefinitionreader (beanregister); Xmlbeanreader.loadbeandefinitions ("Classpath: Userservice.xml "); return beanregister;}}
Code Listing 6#userservice.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id=" UserService "class=" Test. UserService "><constructor-arg index=" 0 "><ref bean=" Userdao "/></constructor-arg></bean ><bean id= "Userdao" class= "test. Userdao "></bean></beans>
Cond
Look, rookie. Spring Source (0) DEUBG environment Construction and beanfactory registration and dependency binding