Spring依赖注入
【了解】Spring管理对象的生命周期(只有在单例的模式有意义,这个是默认的)
-
由spring管理的对象可以有生命周期方法,这些生命周期方法是开发人员自定义的,方法名自定义,无参数
-
由spring管理的对象的类可以有初始化和销毁这两种生命周期方法,按需设计即可。
-
因为是spring自己调用的,因此要设置为无参数,无返回值的方法。
为单个bean指定生命周期方法
- 实例:
public class Person {
private String name;
private int age;
//person类的初始化方法
public void init(){
System.out.println("初始化方法");
}
public void destory(){
System.out.println("销毁方法");
}
}
- 在spring配置文件中配置
<!--
init-method : 定义初始化方法,直接写上方法名称即可
destroy-method: 定义销毁方法,直接写上方法名即可
-->
<bean id="" class="cn.tedu.spring.beans.Person" init-method="init" destroy-method="destory"></bean>
为容器中所有的bean指定生命周期方法
- 可以在顶级<beans>节点中添加
default-init-method
指定初始化方法和添加default-destroy-method
指定销毁方法
<beans default-init-method="init" default-destroy-method="destroy">
<bean id="person" class="cn.tedu.spring.bean.Person"></bean>
</bean>
【重要】注入属性值
实现目标
- 由spring管理的对象,其属性值可以为其中的某些属性注入值,是的最终获取对象时,属性就已经有值了
Setter注入
前提
- 必须为每一个属性添加
set
方法
基本数据类型变量的注入
实现
- 新建一个Person的实体类如下:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 在配置文件中配置bean
<bean id="person" class="cn.tedu.spring.beans.Person">
<!-- 直接使用property配置参数
name:指定属性的字段,这个是set方法后面的单词首字母小写的值,比如SetUsername(),那么此时的name值为username
value: 指定属性的值
ref : 指定前面定义的bean的id,用于设置引用类型的参数值
-->
<property name="name" value="陈加兵"></property>
<property name="age" value="22"></property>
</bean>
-
注意: 在配置XML文件时,<property>节点中的name属性的值其实是Java代码中Set方法名称中除去set单词并且将首字母小写后的名称,例如Set方法的名称为
setAge
,那么其中的name属性的值就是age
。所以这个name属性的值并不是java代码中的属性名称,只不过通常在java代码中,基于代码规范,这里的name属性值也是java代码中的属性名称。
引用类型的属性注入
- 引用类型即是在一个类中包含另外一个类的对象,即是一个类的成员变量是另外一个类的对象
实现
- 新建一个Address类
public class Address {
private String city; //城市
private String pro ; //省份
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPro() {
return pro;
}
public void setPro(String pro) {
this.pro = pro;
}
}
- 新建一个Person类,其中包含了Address这个类的对象
public class Person {
private String name;
private int age;
private Address address; // Address的对象作为成员变量
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 在配置文件中配置实例
<!-- 创建一个Address的实例 -->
<bean id="address" class="cn.tedu.spring.beans.Address">
<property name="city" value="无锡"></property>
<property name="pro" value="江苏"></property>
</bean>
<bean id="person" class="cn.tedu.spring.beans.Person">
<property name="name" value="陈加兵"></property>
<property name="age" value="22"></property>
<!-- 这里的ref引用的是上面配置的Address的实例中的id值 -->
<property name="address" ref="address"></property>
</bean>
【了解】构造器注入(无参,有参)
- 在前面已经讲过了无参构造注入,直接使用
<bean id="" class="">
即可
前提
- 有一个构造有参构造方法
实现
- 创建一个Person类
public class Person {
private String name;
private int age;
//构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 配置spring的配置文件
<bean id="person" class="cn.tedu.spring.beans.Person">
<!--
name: 这个相当于index,也是指定参数列表的属性,不过这里是直接使用参数列表中的变量名
value: 为参数设置的值
index: 构造方法的参数列表的索引,从0开始
ref:引用类型的值,这里的值应该是上面已经定义好的bean的id值
-->
<constructor-arg index="0" value="陈加兵"></constructor-arg>
<constructor-arg index="1" value="22"></constructor-arg>
</bean>
注入基本型
- 在spring注入值时,如果值的类型是String或者java中的基本数据类型,都称之为基本型,而其他的都是”非基本型“的数据
注入非基本型(ref)
- 前面所说的
引用类型的注入
就是基本型的注入,这里不再详细的讲述
注入集合
- 集合类型有
List
,Set
,Map
,Properties
实现
- 创建一个Message类,其中定义了各种集合类型的成员属性,并且添加了
set
方法
public class Message {
private List<String> cities; //城市 。List集合
private Set<String> friend; //Set集合
private Map<Integer,String> bookes; //Map集合
private Properties properties; //Properties集合
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public Set<String> getFriend() {
return friend;
}
public void setFriend(Set<String> friend) {
this.friend = friend;
}
public Map<Integer, String> getBookes() {
return bookes;
}
public void setBookes(Map<Integer, String> bookes) {
this.bookes = bookes;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
- 在spring的配置文件中配置注入
<bean id="message" class="cn.tedu.spring.beans.Message">
<!-- List集合的注入 -->
<property name="cities">
<list>
<value>徐州</value>
<value>无锡</value>
<value>常州</value>
</list>
</property>
<!-- Set集合的注入 -->
<property name="friend">
<set>
<value>Jack</value>
<value>Tom</value>
<value>陈加兵</value>
</set>
</property>
<!-- Map集合的注入 -->
<property name="bookes">
<map>
<entry key="1001" value="java编程基础"></entry>
<entry key="1002" value="java编程思想"></entry>
</map>
</property>
<!-- properties的集合的注入 -->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
- 测试方法
@Test
public void test2() {
// spring的配置文件
String conf = "applicationContext.xml";
// 获取spring容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
conf);
Message message=(Message) context.getBean("message");
List<String> cities=message.getCities();
Set<String> friends=message.getFriend();
Map<Integer, String> bookes=message.getBookes();
Properties properties=message.getProperties();
System.out.println(cities);
System.out.println(friends);
System.out.println(bookes);
System.out.println(properties);
context.close();
}
【重点了解Properties的注入】引用方式注入集合(<util:>
)
-
其中的
ref
指定的是配置集合的id
-
使用的还是上面的
Message
类 -
在
resource
中需要新建一个jdbc.properties
,我们便可以在spring的配置文件中使用<util:properties id="" location="classpath:jdbc.properties">
自动的读取其中的值
root=root
password=root
url=jdbc:mysql://localhost:3306/hirbernate?useUnicode=true&characterEncoding=UTF-8
driver=com.mysql.jdbc.Driver
- spring的配置文件中配置bean
<util:list id="cities">
<value>徐州</value>
<value>无锡</value>
<value>常州</value>
</util:list>
<util:set id="friends">
<value>Jack</value>
<value>Tom</value>
<value>陈加兵</value>
</util:set>
<util:map id="bookes">
<entry key="1001" value="java编程基础"></entry>
<entry key="1002" value="java编程思想"></entry>
</util:map>
<!-- 引入外部的Properties文件,location指定的就是位置 -->
<util:properties id="properties" location="classpath:jdbc.properties"></util:properties>
<bean id="message" class="cn.tedu.spring.beans.Message">
<!-- List集合的注入 ref指定的上面定义的List的id -->
<property name="cities" ref="cities"></property>
<!-- Set集合的注入 -->
<property name="friend" ref="friends"></property>
<!-- Map集合的注入 -->
<property name="bookes" ref="bookes"></property>
<!-- properties的集合的注入 -->
<property name="properties" ref="properties"></property>
</bean>
【了解】其他类型的注入
为数组注入值
- 新添加一个数组的属性
private String[] names;
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
- spring配置文件
<!-- 为数组赋值 -->
<property name="names">
<array>
<value>Alex</value>
<value>Billy</value>
</array>
</property>
- 甚至在配置XML时,
<List>
和<array>
可以随意挑选使用,即为List
类型的数据注入值时,既可以使用<List>
节点,也可以使用<array>
节点,反之为数组类型的数据值也是一样
注入空字符串
- 设置的
value
直接为""
为引用类型的数据注入null值
<property name="xxx">
<null/>
</property>
显示的确定数据类型
<property name="xxx">
<value type="数据类型">值</value>
</property>
网友评论