With a great understanding of the vernacular to talk about spring IOC, DI's

IOC inversion of control
start with the most basic mvc three terms, the new controller general in the service implementation class, in service in the new dao implementation class, the layers are very close contact, is coupled with high professional terms, to understand the coupling spring, no longer in the form of new objects want to get an instance, then there inversion of control said, look at the reverse literally felt like I did not take the initiative to get the instance of the object I want, and is a way to pass me, indeed, spring ioc container has a say, this vessel is the xml configuration file, the configuration file can be configured bean, a bean is equivalent to a class, you can be removed from the container you want examples of objects, such as the following:

<bean id="person" class="cn.hp.bean.Person"></bean>
public static void main(String[] args) {
	ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
	Person person = app.getBean("person",Person.class);
	System.out.println(person);
}

The first code snippet is defined in the configuration file, the bean, is the second instance of the object person acquired, but see the main method, which is not the object to obtain an instance manually Well, there is still some coupling ah, this time to use attribute value of the injection member;

DI DI
This injection is given attribute value class, we said before inversion of control, in the hope that gave us automatically bean instance, DI can achieve this operation, or in three sub-example and facing interfaces, the controller defining a service layer interface that interfaces to the service you can annotate manner, the service class that implements the interface by imparting xml or configuration, provided that the controller and the service class to be implemented in the container, the following codes:

@Controller
public class UserController {
	@Autowired
	private UserService service;
	
	public void hello(){
		service.hello();
	}
}
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao dao;
	
	@Override
	public String hello() {
		return dao.hello();
	}
}

DI in the container can be matched to a consistent injection type instances corresponding to the interface, so that inversion does implement automatic injector and effects the ultimate decoupled, and that if a plurality of types of implementations consistent Existence container how to do you can use @Qualifier(value="")or @Resource(name="")annotations to obtain a flexible and an implementation class, also without changes to Java code.

To cite a small example of IOC and DI scene of a life
you give your daughter birthday, cake shop in town where you are not, you only have to worry about myself, and later the town opened a cake shop, you just have to shop in a phone call to tell your needs, the store will ride the cake delivered to your doorstep, you feel very happy.

Published 12 original articles · won praise 3 · Views 239

Guess you like

Origin blog.csdn.net/qq_38599840/article/details/104852277