Cainiao looks at the deubg environment of spring source code (0) and the registration and dependency binding of BeanFactory, deubgbeanfactory
Paste the following basic classes:
Code List 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 List 2 # UserDao. java
Package test; public class UserDao {public void add (User user) {System. out. println ("add user:" + user. getName ());}}
Code List 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
Beanfatory injection and dependency binding are implemented in the Code, which makes it easy to understand what the getBean method is like.
The BeanDefinitionRegistry interface implements the DefaultListableBeanFactory interface,
DefaultListableBeanFactory implements the BeanFactory interface.
Code list 4 # test. java code-based beanfatory injection and dependency binding
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) {defalistlistablebeanfactory beanRegister = new register (); 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 to the registry in the container. registerBeanDefinition ("userDao", userDao); registry. registerBeanDefinition ("userService", userService); // bind dependency // 1. constructorArgumentValues argValues = new ConstructorArgumentValues (); argValues. addIndexedArgumentValue (0, userDao); // argValues. addIndexedArgumentValue (1 ,...); userService. setConstructorArgumentValues (argValues); // 2. use the setter method to inject MutablePropertyValues propertyValues = new MutablePropertyValues (); propertyValues. addPropertyValue ("userDao", userDao); userService. setPropertyValues (propertyValues); // return (BeanFactory) registry completed by binding ;}}
Through the xml configuration file implementation, debug starts from this code, and parses "classpath: userservice. xml "string, read resources through the resource interface (classpathResouce here), parse the bean tag, and generate the corresponding BeanDefinition. Of course, you need to make a proper choice. Otherwise, there will be too much code to complete, the key is not necessarily understandable.
Code List 5 # TestFromXML. java uses the 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 List 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>
To be continued ,,,