1) SPRINGIOC Core Simulation implementation
Idea: When initializing a spring container, read the defined bean information from the configuration file and deposit it into the spring container based on the configuration property initialization.
When a bean is needed, it is obtained directly from the container through the ID.
Steps:
A) Create a Java project that requires a dependency package (primarily used to parse XML files) such as:
b) First simulate spring to define two simple beans in the configuration file My-ioc.xml, as follows:
c) Follow the above ideas to simulate spring implementation of our own IOC container Myioccontext, implemented as follows:
public class Myioccontext {
Private final map<string, object> beanmap = new hashmap<string, object> ();
Public Myioccontext (String fileName) {
Init (fileName);
}
private void init (String fileName) {
}
Public Object Getbean (String ID) {
return Beanmap.get (ID);
}
}
Where Init is implemented as follows:
d) Test:
public class Myioctest {
Private Myioccontext CTX = null;
@Before
public void SetUp () throws Exception {
CTX = new Myioccontext ("My-ioc.xml");
}
@Test
public void Testioc () throws Exception {
Hello hello = (hello) ctx.getbean ("Hello");
Hello.say ("My IOC Context");
}
}
The result is:
At this point, the core implementation of the IOC container is complete.
2) Spring Dependent injection method
A) Manual injection
@Autowired: Injecting by type
@Resource: Injected by name if it fails and then injected by type. If name is specified, it is injected by name only.
b) Automatic injection
Configuration in XML, <bean id= "" class= "" autowire= "Bytype"/>
SPRINGIOC of Technical Summary