我们在Spring配置文件中引入的bean初始值是空的,依赖注入就是给他们赋值。这里主要介绍三种常用的方式。
1、setter注入
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="25">property>
<property name="gender" value="男">property>
bean>
利用properties
标签给对应的属性赋值(本质是调用set()方法)。
2、构造器注入
构造器注入要求在对应的实体类中编写有参构造,其本质是调用有参构造来进行初始化。
<bean id="student" class="com.lu.Spring.pojo.Student">
<constructor-arg value="1001">constructor-arg>
<constructor-arg value="李白">constructor-arg>
<constructor-arg value="25">constructor-arg>
<constructor-arg value="男">constructor-arg>
bean>
默认是按照构造器参数的顺序进行赋值,也可以用name属性指定赋值。
<bean id="student" class="com.lu.Spring.pojo.Student">
<constructor-arg value="1001" name="age">constructor-arg>
<constructor-arg value="李白">constructor-arg>
<constructor-arg value="25" name="id">constructor-arg>
<constructor-arg value="男">constructor-arg>
bean>
3、依赖注入P命名空间
p命名空间是根据属性来注入依赖的,相当于将property标签换成p:
来进行注入。
<bean id="student_p" class="com.lu.Spring.pojo.Student"
p:id="1001"
p:name="苏轼"
p:age="20"
p:gender="男"
>bean>
1、赋值null
当我们想要给属性赋值为null时,不能直接value="null"
,这样是给属性赋值字符串"null"。
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="null">property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
bean>
要利用标签来进行赋值。这样的话才能赋空值。
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" >
<null>null>
property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
bean>
输出name.toString(),会报空指针异常,代表name属性值为空。
@Test
public void test(){
//1.获取ApplicationContext
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取指定类对象
Student student = (Student) applicationContext.getBean("student");
applicationContext.getBean(Student.class);
System.out.println(student.getName().toString());
}
2、特殊字符的赋值
当我们给属性赋值内容中含有特殊字符时,比如给name赋值为<李白>
,含有尖括号是特殊字符,有两种解决办法。
①、用xml实体来代替特殊字符。
<
的xml实体是<
;>
的xml实体是>
;
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="<李白>">property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
bean>
②、用CDATA节。
CDATA节内的特殊字符都会被当作普通字符。
CDATA节只能写在value标签中,而不能写在value属性中。
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" >
<value> ]]>value>
property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
bean>
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="25">property>
<property name="gender" value="男">property>
<property name="hobbys">
<array>
<value>舞剑value>
<value>吟诗value>
<value>喝酒value>
array>
property>
bean>
当实体类中的属性含有内部类时,对该属性的赋值有三种方法:
1、引用外部bean
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
<property name="teacher" ref="teacher">property>
bean>
<bean id="teacher" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="裴旻">property>
<property name="age" value="40">property>
<property name="email" value="232156@qq.com">property>
bean>
利用ref
属性,引用外部bean。
2、内部bean
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
<property name="teacher" >
<bean id="teacher" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="裴旻">property>
<property name="age" value="40">property>
<property name="email" value="232156@qq.com">property>
bean>
property>
bean>
3、级联赋值
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="20">property>
<property name="gender" value="男">property>
<property name="teacher" ref="teacher">property>
<property name="teacher.name" value="裴旻">property>
<property name="teacher.age" value="40">property>
<property name="teacher.email" value="232156@qq.com">property>
bean>
<bean id="teacher" class="com.lu.Spring.pojo.Teacher">bean>
当属性为集合时,对集合的赋值分两种情况。
1、属性为list集合的赋值
①、内部list赋值
<bean id="teacher" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="孔子">property>
<property name="age" value="50">property>
<property name="email" value="001@qq.com">property>
<property name="studentList" >
<list>
<ref bean="student01">ref>
<ref bean="student02">ref>
<ref bean="student03">ref>
list>
property>
bean>
<bean id="student01" class="com.lu.Spring.pojo.Student">
<property name="id" value="3001">property>
<property name="name" value="颜回">property>
<property name="age" value="30">property>
<property name="gender" value="男">property>
bean>
<bean id="student02" class="com.lu.Spring.pojo.Student">
<property name="id" value="3002">property>
<property name="name" value="闵损">property>
<property name="age" value="28">property>
<property name="gender" value="男">property>
bean>
<bean id="student03" class="com.lu.Spring.pojo.Student">
<property name="id" value="3003">property>
<property name="name" value="仲由">property>
<property name="age" value="25">property>
<property name="gender" value="男">property>
bean>
②、引用外部list赋值
<bean id="teacher" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="孔子">property>
<property name="age" value="50">property>
<property name="email" value="001@qq.com">property>
<property name="studentList" ref="students">property>
bean>
<util:list id="students" >
<ref bean="student01">ref>
<ref bean="student02">ref>
<ref bean="student03">ref>
util:list>
<bean id="student01" class="com.lu.Spring.pojo.Student">
<property name="id" value="3001">property>
<property name="name" value="颜回">property>
<property name="age" value="30">property>
<property name="gender" value="男">property>
bean>
<bean id="student02" class="com.lu.Spring.pojo.Student">
<property name="id" value="3002">property>
<property name="name" value="闵损">property>
<property name="age" value="28">property>
<property name="gender" value="男">property>
bean>
<bean id="student03" class="com.lu.Spring.pojo.Student">
<property name="id" value="3003">property>
<property name="name" value="仲由">property>
<property name="age" value="25">property>
<property name="gender" value="男">property>
bean>
2、属性为map集合的赋值
<bean id="student" class="com.lu.Spring.pojo.Student">
<property name="id" value="1001">property>
<property name="name" value="李白">property>
<property name="age" value="25">property>
<property name="gender" value="男">property>
<property name="subject" ref="map">property>
bean>
<util:map id="map">
<entry key="术算" value-ref="teacher1">entry>
<entry key="剑术" value-ref="teacher2">entry>
util:map>
<bean id="teacher1" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="赵蕤">property>
<property name="age" value="40">property>
<property name="email" value="232156@qq.com">property>
bean>
<bean id="teacher2" class="com.lu.Spring.pojo.Teacher">
<property name="name" value="裴旻">property>
<property name="age" value="40">property>
<property name="email" value="232156@qq.com">property>
bean>
引入jdbc.properties:
1、Resource下面新建jdbc.properties
2、Spring配置文件中引入
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
管理数据源
1、引入Maven依赖
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.29version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.11version>
dependency>
2、配置Spring文件
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driver" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
3、获取连接
@Test
public void test() throws SQLException {
//1.获取ApplicationContext
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("a.xml");
//2.获取指定类对象
DruidDataSource dataSource = applicationContext.getBean(DruidDataSource.class);
//3.获取链接
Connection connection = dataSource.getConnection();
System.out.println(connection);
}