Spring IOC(一)
介绍
IOC,inversion of control,控制反转。经典的,我们用关键字new去主动创建对象。而将创建对象的功能交给容器,再根据需要获取就称为控制反转。这里的容器称为IOC容器。
简单案例
1、pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2、bean类
public class User{
private String username;
private String password;
//getter和setter方法省略
}
3、spring配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.tj.entity.User">
</beans>
4、使用
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = applicationContext.getBean("user",User.class);
Spring IOC(一)
原文地址:https://www.cnblogs.com/heibaimao123/p/13792777.html