This article Source: GitHub · Click here || GitEE · Click here
A, IOC inversion of control
1, IOC container ideas
Coupling Java Object relations system is very complex, dependencies between each module of the system, call each service requests between micro-module, all the truth. Reduce system between modules, between objects, the degree of coupling between the service micro services, is one of the core problems in software engineering. Because the Spring Framework core idea is to IOC inversion of control, to achieve decoupling between objects.
2, Inversion of Control
- The traditional way
If you want to use the function object A object B method, create an instance of the object B in the time of need, call the method needs, with active control over the objects B.
- IOC container
When using IOC containers lost between the objects A and B directly, if the object A wants to use the functions of the method object B, IOC container will automatically create an object instance of B is injected into the desired object A functional module, so that object A He lost control of the initiative, which is the inversion of control.
3, dependency injection
IOC relation to the subject of direct action, called DI dependency injection (Dependency Injection); Dependence: A required target object B function, called object A dependent objects B. Injection: A object instantiated object B, the object B such that using the function, the operation is called injection.
Two, IOC container case
1, buy a ticket ride scene
- Simple class car
public class ByBus {
// 方式一:直接实例化
// private BuyTicket buyTicket = new BuyTicket () ;
private BuyTicket buyTicket ;
public BuyTicket getBuyTicket() {
return buyTicket;
}
public void setBuyTicket(BuyTicket buyTicket) {
this.buyTicket = buyTicket;
}
public void takeBus (){
String myTicket = this.getBuyTicket().getTicket() ;
if (myTicket.equals("ticket")){
System.out.println("乘车");
}
}
}
- Simple class tickets
public class BuyTicket {
public String getTicket (){
return "ticket" ;
}
}
2, Spring configuration file
Spring configuration file used herein, the class to drive, injecting ticket class, and then complete the full operation.
<bean id="byBus" class="com.spring.mvc.entity.ByBus">
<property name="buyTicket" ref="buyTicket" />
</bean>
<bean id="buyTicket" class="com.spring.mvc.entity.BuyTicket"/>
3, test code
public class Test1 {
@Test
public void test01 (){
ApplicationContext context =
new ClassPathXmlApplicationContext("/ioc-contain-01.xml");
ByBus byBus = (ByBus) context.getBean("byBus");
byBus.takeBus();
}
}
Third, the core API summary
Explained for several core API used above, continued follow-up summary.
1、BeanFactory
This is a plant for the generation of any bean. Take the lazy loading, Bean will initialize the first time getBean.
2、ApplicationContext
BeanFactory is sub-interfaces, and more powerful. (International process, event delivery, automatic assembly Bean, Context various application-layer). When the configuration file is loaded, the object will be instantiated.
3、ClassPathXmlApplicationContext
For loading CLASSPATH (class path, src) xml at runtime loading position xml: / WEB-INF / classes / ... xml
4、FileSystemXmlApplicationContext
Xml for loading the letter specified runtime loading position xml: / WEB-INF / ... xml, obtained by a specific drive letter configuration ServletContext.getRealPath ().
Fourth, the source address
GitHub·地址
https://github.com/cicadasmile/spring-mvc-parent
GitEE·地址
https://gitee.com/cicadasmile/spring-mvc-parent