3、HelloSpring
新建HelloSpring项目
操作过程
-
新建一个module:spring-02-hellospring
-
新建pojo类
-
设置set和get方法
-
重写toString方法
-
-
新建beans.xml
去官网找模板:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
<?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="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
-
测试
public class MyTest { public static void main(String[] args) { //获取Spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); // 我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以! Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString()); } }
-
代码地址:
https://gitee.com/zengqiang_wang/spring-study-gongyi
-
思考问题:
-
Hello对象是谁创建的?
hello对象是由Spring创建的
-
Hello对象的属性是怎么设置的?
hello对象的属性是由Spring容器设置的
这个过程就叫控制反转。
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的
反转:程序本身不创建对象,而变成被动的接收对象
依赖注入:就是利用set方法来进行注入的
IoC是一种编程思想,由主动的编程变成被动的接收
可以通过new ClassPathXmlApplicationContext去浏览一下底层源码
OK,到了现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IoC,一句话搞定:对象由Spring来创建,管理,装配!
-
ClassPathXmlApplicationContext的类结构图
改进Spring-01-ioc1项目
-
核心代码:
配置文件:
<?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="userDaoImpl" class="com.gy.dao.UserDaoImpl"/> <bean id="userDaoMysqlImpl" class="com.gy.dao.UserDaoMysqlImpl"/> <bean id="userDaoOracleImpl" class="com.gy.dao.UserDaoOracleImpl"/> <bean id="userDaoSQLServerImpl" class="com.gy.dao.UserDaoSQLServerImpl"/> <bean id="userServiceGeneral" class="com.gy.service.UserServiceGeneralImpl"> <property name="userDao" ref="userDaoImpl"/> </bean> <!-- ref: 引用Spring容器中出创建好的对象 value: 具体的值,基本数据类型! --> </beans>
测试类:
public class MyTest3 { public static void main(String[] args) { //获取ApplicationContext;拿到Spring的容器 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //容器在手,天下我有,需要什么,就直接get什么! UserServiceGeneralImpl userServiceGeneral = (UserServiceGeneralImpl) context.getBean("userServiceGeneral"); userServiceGeneral.getUser(); } }
-
代码地址:
https://gitee.com/zengqiang_wang/spring-study-gongyi
代码show
代码结构图:
1.新建spring-02-hellospring module
2.新建pojo包及类
Hello.java
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.新建资源文件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.gy.pojo.Hello">
<property name="str" value="Hello Spring!"/>
</bean>
</beans>
4.新建测试类
public class MyTest {
public static void main(String[] args) {
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以!
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
彩蛋
1.一旦class被spring托管,那么class的左侧有个logo
2.被class托管的类无logo,bean.xml也无logo:
解决:
有提示后,观察project structure