https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-metadata
3、HelloSpring
新建HelloSpring项目
操作过程
新建一个module:spring-02-hellospring
新建com.txy.pojo类
设置set和get方法
重写toString方法
package com.txy.pojo;
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 + '\'' +
'}';
}
}
新建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">
<!--使用spring来创建对象,在spring中这些都称为bean
bean=对象 new Hello();
id等价于变量名
class=new 的对象
property相当于给对象中的属性设置一个值
-->
<bean id="hello" class="com.txy.pojo.Hello">
<!-- collaborators and configuration for this bean go here -->
<property name="str" value="Spring"/>
</bean>
</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());
}
}
思考问题:
- 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();
}
}
————————————————
版权声明:本文为CSDN博主「工一木子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Prince140678/article/details/117201689