- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是 由Spring来创建的
- 反转 : 程序本身不创建对象 , 而变成被动的接收对象
Hello 对象是谁创建的 ? [显然是springc创建的]
Hello 对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器通过set方法设置的 】
我们将Hello的set删除
一、依赖注入(DI)
- 依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配
1、构造器注入
public Hello() {
System.out.println("无参构造被调用");
}
输出对象时,Hello对象已经通过无参构造初始化了!
//加入有参构造方法
public Hello(String str) {
this.str = str;
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.jia.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
<bean id = "hello1" class="com.jia.pojo.Hello">
<!-- 根据index参数下标设置-->
<constructor-arg index="0" value="spring1"/>
</bean>
<bean id="hello2" class="com.jia.pojo.Hello">
<!-- 根据参数名字设置-->
<constructor-arg name="str" value="spring2"/>
</bean>
<bean id="hello3" class="com.jia.pojo.Hello">
<!-- 根据参数类型设置,多个参数类型相同按照顺序-->
<constructor-arg type="java.lang.String" value="spring3"/>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//Object hello = context.getBean("hello");
Object hello1 = context.getBean("hello1");
Object hello2 = context.getBean("hello2");
Object hello3 = context.getBean("hello3");
System.out.println(hello1);
System.out.println(hello2);
System.out.println(hello3);
}
}
测试中没有调用,bean管理的对象也创建了!!
2、set注入 (重点)
<bean id="s1" class="com.jia.pojo.Student">
<!-- 普通注入-->
<property name="name" value="小王"/>
<!-- bean注入,ref-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!-- list注入-->
<property name="hobbys">
<list>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</list>
</property>
<!-- map注入-->
<property name="card">
<map>
<entry key="身份证" value="12345678910"/>
<entry key="银行卡" value="0987654321"/>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>PUBG</value>
</set>
</property>
<!-- null注入-->
<property name="wife"><null/></property>
<!-- properties注入-->
<property name="info">
<props>
<prop key="sno">20220001</prop>
<prop key="name">小王</prop>
</props>
</property>
</bean>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student s1 = context.getBean("s1", Student.class);
System.out.println(s1);
}
}
3、拓展注入实现()
p命名注入——其对应set方法,通过无参构造,set属性
xmlns:p="http://www.springframework.org/schema/p"
<bean id="u1" class="com.jia.pojo.User" p:name="小王" p:age="18"/>
c命名注入——对应有参构造
xmlns:c="http://www.springframework.org/schema/c"
<bean id="u1" class="com.jia.pojo.User" c:name="张三" c:age="18"/>
二、Spring配置
别名:alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<bean id="u1" class="com.jia.pojo.User" c:name="张三" c:age="18" />
<alias name="u1" alias="u11"/>
<alias name="u1" alias="u22"/>
Bean的配置
<!--bean就是java对象,由Spring创建和管理-->
<!--
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
import:合并依赖
<import resource="{path}/beans.xml"/>