First look at the spring, will feel good magic IOC. You do not want to set a value, use it every time you take it. It is how to implement? core is the IOC container. It is mysterious? $? is actually very simple, just read a configuration file, corresponding to the Bean, and then assign a value to the Bean on the line, the taken time to directly use the getBean method to get.
Crap, we have to write a simple IOC container, to prove that in fact it is.
First name we came exactly as it appears in the Spring, so that the test is feeling like in Spring.
Our test case is such that the two classes, User and People, and in the User name and age, People, and a reference to the User. Call the People in the User's say method print. Look at these two classes.
User class
People class
Then we write a configuration file, placed in the src directory.
Then, let's write the IOC, first declare an interface!
ApplicationContext
Followed by the implementation. ClassPathXMLApplicationContext class.
First of all be constructed method
This method is the core of the IOC, here I am using jdom to handle xml files, dom4j, does not matter.




No comments:
Post a Comment