学习内容:依赖注入(DI)、Bean的作用域
文章目录
1. 概念
依赖注入(Dependency Injection,DI):
依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
2. 依赖注入(DI)
2.1、 构造器注入(之前介绍过)
详情见此链接中的“IOC创建对象的方式”
无参构造:
<!--无参构造-->
<bean id="user" class="pojo.User">
<property name="name" value="pym"/>
</bean>
有参构造(其一):
<!--第三种:直接通过参数名来设置-->
<bean id="user" class="pojo.User">
<constructor-arg name="name" value="ljy"/>
</bean>
2.2、Set方式注入
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is。
- 实体类
Address.java
和Student.java
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
这里因为Student 类中的属性太多,Getter和Setter方法用的lombok注解
@Data
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String > card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
- applicationContext.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">
<bean id="address" class="pojo.Address">
<property name="address" value="湖北"/>
</bean>
<bean id="student" class="pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="pym"/>
<!--第二种:Bean注入,ref-->
<property name="address" ref="address"/>
<!--第三种:数组-->
<property name="books">
<array>
<value>Java</value>
<value>Spring</value>
<value>JVM</value>
</array>
</property>
<!--数组-->
<property name="hobbies">
<list>
<value>学习</value>
<value>音乐</value>
<value>游戏</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="1321321321313"/>
<entry key="房卡" value="219"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>csgo</value>
<value>LoL</value>
<value>原神</value>
</set>
</property>
<!--null
<property name="wife" value=""/> 这个是为空,啥也不显示
-->
<!--<property name="wife" value=""/>-->
<!--这种是为null, 显示值为null-->
<property name="wife">
<null/>
</property>
<!--Properties
key=value 形式
-->
<property name="info">
<props>
<prop key="学号">306</prop>
<prop key="性别">男</prop>
<prop key="班级">3班</prop>
</props>
</property>
</bean>
</beans>
结果:
这里我为了方便截图,在toString里面加了换行符
2.3、其他方式注入(p命名和c命名注入)
User.java
@Data
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(一)p命名空间注入:
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
userbeans.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="pojo.User" p:name="gql" p:age="30"/>
</beans>
- 测试代码
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
结果:
(二)c命名空间注入:
注意实体类中要写有参构造方法
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
userbeans.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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="pojo.User" c:age="20" c:name="zhn"/>
</beans>
- 测试代码
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
结果:
3. Bean的作用域
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 。
几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。
- Singleton(单例模式,Spring默认机制)
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
测试:
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user",User.class);
User user2 = context.getBean("user",User.class);
System.out.println(user==user2);
}
结果:
- Prototype(原型模式:每次从容器中get的时候,都会产生一个新对象)
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:
<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>
或者
<bean id="account" class="com.foo.DefaultAccount" singleton="false"/>
其余的request、session、application、这些只能在web开发中用到!
- Request
当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="loginAction" class="cs.csdn.LoginAction" scope="request"/>
- Session
当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。