1、导包
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.19version>
- dependency>

2、编写代码
- public class Hello {
- private String str;
-
- public String getStr() {
- return str;
- }
-
- public void setStr(String str) {
- this.str = str;
- }
-
- @Override
- public String toString() {
- return "Hello{" +
- "str='" + str + '\'' +
- '}';
- }
- }

3、编写我们的spring文件,这里命名为beans.xml
- "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="hello" class="com.gt.pojo.Hello">
- <property name="str" value="Spring"/>
- bean>
-
- beans>

4、测试
- public class HelloTest {
- @Test
- public void test() {
- //解析beans.xml文件 , 生成管理相应的Bean对象
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- //getBean : 参数即为spring配置文件中bean的id .
- Hello helloSpring = (Hello) context.getBean("helloSpring");
- System.out.println(helloSpring);
- }
- }

答:hello 对象是由Spring创建的
答:hello 对象的属性是由Spring容器设置的
这个过程就叫控制反转:
控制:谁在控制对象的创建,传统的应用程序的对象是由程序本身控制创建的,使用spring后,对象由spring来创建
反转:程序本身不创建对象,而变成被动的接收对象
依赖注入:就是利用set方法来进行注入的
IOC是一种编程思想,由主动的编程变成被动的接收
1、新增一个spring配置文件beans.xml
- "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="mysqlImpl" class="com.gt.dao.UserDaoMysqlImpl"/>
- <bean id="oracleImpl" class="com.gt.dao.UserOracleImpl"/>
- <bean id="SqlserverImpl" class="com.gt.dao.UserDaoSqlserverImpl"/>
-
-
- <bean id="UserServiceImpl" class="com.gt.service.UserServiceImpl">
- <property name="userDao" ref="SqlserverImpl"/>
- bean>
-
-
-
- beans>

2、测试
- import com.gt.dao.UserDaoImpl;
- import com.gt.dao.UserDaoMysqlImpl;
- import com.gt.dao.UserDaoSqlserverImpl;
- import com.gt.service.UserService;
- import com.gt.service.UserServiceImpl;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class MyTest {
-
- // @Test
- // public void getUser(){
- // UserServiceImpl service = new UserServiceImpl();
- // service.getUser();
- // }
-
- public static void main(String[] args) {
-
- 用户实际调用的是业务层,dao层他们不需要接触!
- // UserServiceImpl userService = new UserServiceImpl();
- //
- // ((UserServiceImpl) userService).setUserDao(new UserDaoSqlserverImpl());
- //
- // userService.getUser();
-
- // 获取ApplicationContext:拿到Spring的容器
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
-
- // 容器在手,天下我有,需要什么,就直接get什么!
- UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
-
- userServiceImpl.getUser();
-
- }
-
-
- }

- @Test
- public void addUser2() {
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- UserServiceImpl service = (UserServiceImpl) context.getBean("userServiceImpl");
- service.addUser();
- }
- 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改
- 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !