1. Add maven support
org.springframework spring-context 4.0.2.RELEASE
Basic injection. You only need this dependency.
2. Compile several test classes
Suppose: There is a programmer jimmy who has several computers and a pet. After going home from work every day, the pet will welcome him home in some form.
MacBook "\tApple MAC Book"
ThinkPad "\tLenovo ThinkPad"
Dog "\twang! wang!"
List<Computer> .name = List<Computer> setComputers(List<Computer>.computers = .pet = "My name is " +", and I have " + computers.size() + " computer" + (computers.size() > 1 ? "s" : "") + ":""And I have a pet, everyday,when I go home, it will welcome me by "
3. spring configuration file
This configuration file SimpleBeans. in xml, a total of four bean instances are configured. After the configuration is loaded by the Spring container, these objects will be instantiated (by default, the singleton mode is instantiated) and kept in the container, you can use the getBean method of ApplicationContext to obtain
4. Test Injection
For ease of injection, first write a simple tool class SpringUtils
(applicationContext == = ClassPathXmlApplicationContext("SimpleBeans.xml" <T> T getBean(Class<T>
Then you can use this tool class to get the injected object instance through getBean.
= SpringUtils.getBean(Programmer.,"jimmy"
Running effect:
My name is jimmy. yang, and I have 2 computers:
Lenovo ThinkPad
Apple MAC Book
And I have a pet, everyday, when I go home, it will welcome me
Wang! Wang!