1.在pom.xml中导包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
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.在resources编写spring文件,这里命名为beans.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">
<!--使用Spring来创建对象,在Spring这些都称为Bean
类型 变量名 = new 类型()
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值!
-->
<bean id="hello" class="com.yuan.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
4.测试
import com.yuan.pojo.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello helloSpring = (Hello) context.getBean("hello");
System.out.println(helloSpring);
}
}
思考
-
Hello 对象是谁创建的 ?
hello 对象是由Spring创建的
-
Hello 对象的属性是怎么设置的 ?
hello 对象的属性是由Spring容器设置的
这个过程就叫控制反转:
-
控制:谁在控制对象的创建,传统的应用程序的对象是由程序本身控制创建的,使用spring后,对象由spring来创建
-
反转:程序本身不创建对象,而变成被动的接收对象
依赖注入:就是利用set方法来进行注入的
IOC是一种编程思想,由主动的编程变成被动的接收
修改IOC的例子
1.在resources编写spring文件,这里命名为beans.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">
<!--使用Spring来创建对象,在Spring这些都称为Bean
类型 变量名 = new 类型()
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值!
-->
<bean id="mysqlImpl" class="com.yuan.dao.UserDaoMysqlImpl"/>
<bean id="sqlserverImpl" class="com.yuan.dao.UserDaoSqlserverImpl"/>
<bean id="UserServiceImpl" class="com.yuan.service.UserServiceImpl">
<property name="userDao" ref="sqlserverImpl"/>
</bean>
<!--
ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型!
-->
</beans>
2.测试
import com.yuan.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}
到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !
注:若文件里没有出现叶子图标,点击idea左上角File-->Project Structure,再点Modules模块,展开项目,项目里面会有Spring和叶子图标。点中叶子,然后点击+号,勾选xml文件,然后apple就行了