1 IoC
withDI
The IoC is an Inversion of Control. Typically, objects are created manually by the caller, in the form of new XXX(). When the Spring framework comes along, instances of objects are no longer created by the caller, but by the Spring container, so control is transferred from the caller to the Spring container, and control is reversed. This is the inversion of control of Spring. From the perspective of the Spring container, the Spring container is responsible for assigning the dependent object to the caller’s member variable, which is equivalent to injecting the caller’s dependent instance. This is Spring’s Dependency Injection (DI).
In a word:
IoC
: Control is given to the callerSpring
Container, the control has been reversedDI
By:Spring
The container injects the required value into the object
2 Spring IoC
The container
The Spring IoC container implements IoC in Spring, based on the following two interfaces:
BeanFactory
ApplicationContext
2.1 BeanFactory
Is located in the org. Springframework. Beans. Under the factory, provides a complete IoC service support, is a management Bean factory, mainly responsible for the initialization of all kinds of beans. You can use XmlBeanFactory to get beans from an XML file and assemble them, as shown in the following example:
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("/xxx/xxx/xxx/xxx/applicationContext.xml"));
TestInterface test = (TestInterface)factory.getBean("test");
test.hello();
Copy the code
Absolute paths are needed, and the method is outdated:
Therefore, it is not recommended.
2.2 ApplicationContext
ApplicationContext is a subinterface of the BeanFactory, also known as the ApplicationContext. In addition to the functions of the BeanFactory, it also adds support for internationalization, resource access, event propagation, and so on.
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
Web
Server instantiation
2.2.1 ClassPathXmlApplicationContext
This class finds the specified XML file from resources:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestInterface test = (TestInterface)context.getBean("test");
test.hello();
Copy the code
2.2.2 FileSystemXmlApplicationContext
This class reads configuration files with a prefix:
classpath:
: The prefix means read from the classpath, forMaven
For the projectresources
file:
: The prefix is obtained from an absolute path
Example:
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
//ApplicationContext context = new FileSystemXmlApplicationContext("file:/xxx/xxx/xxx/xxxx/xxx/applicationContext.xml");
Copy the code
2.2.3 Web
Server instantiation
Using ContextLoaderListener based implementation, modify web.xml and add the following code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</context-param>
Copy the code
3 DI
There are two ways
DI is typically implemented in two ways:
- Constructor injection
setter
injection
Let’s take a look at each of them.
3.1 Constructor injection
Spring can use reflection to do this via constructors, such as the following three classes:
public interface TestInterface {
void hello(a);
}
public class TestA implements TestInterface {
@Override
public void hello(a) {
System.out.println("Test A"); }}public class TestB {
private TestInterface test;
public TestB(TestInterface test)
{
this.test = test;
}
public void method(a)
{ test.hello(); }}Copy the code
TestB needs an object of type TestInterface, so we can inject a TestA into the constructor of TestB:
<bean id="testA" class="TestA"/> <! Insert a TestA object -->
<bean id="testB" class="TestB">
<constructor-arg index="0" ref="testA" /> <! Pass the above injected TestA as an argument to the constructor and pass it to the private member of TestB.
</bean>
Copy the code
Constructor-arg is used to define the tag that will be injected by way of a constructor, index defines the position, starting at 0, and ref is a reference to a Bean with a value of the Bean ID.
3.2 throughsetter
injection
In the above example, modify TestB as follows:
public class TestB {
private TestInterface test;
public void setTest(TestInterface test) {
this.test = test;
}
public void method(a)
{ test.hello(); }}Copy the code
Add a setter and modify the configuration file:
<bean id="testA" class="TestA"/>
<bean id="testB" class="TestB">
<property name="test" ref="testA" />
</bean>
Copy the code