五、Spring配置
5.1、别名
<!--别名:如果添加了别名 ,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="user2"/>
public static void main(String[] args) {
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user2");
user.show();
}
5.2、Bean的配置
<!--
id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的全限定名 : 包名 + 类名
name:别名,可以同时取多个别名
-->
<bean id="usert" class="com.zhang.pojo.User" name="user3 e">
<constructor-arg name="name" value="s"/>
<property name="name" value="z"/>
</bean>
这样输出的结果为z 思考吧~~~~~~~~~
5.3、import
一般用于团队开发使用,他可以将多个配置文件,导入合为一个 多合一
六、依赖注入
6.1、构造器注入———–上篇有写
6.2、Set方式注入(重点)
- 依赖注入:set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
环境搭建
实体类pojo
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> cards;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCards() {
return cards;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
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 + '\'' +
'}';
}
}
bean.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第一种 普通值注入 value-->
<property name="name" value="张三"/>
<!--第二种 Bean注入 ref-->
<property name="address" ref="address"/>
<!--第三种 数组注入-->
<property name="books">
<array>
<value>福尔摩斯</value>
<value>计算机网络</value>
<value>趣学算法</value>
</array>
</property>
<!--第四种 list注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--第五种 map注入-->
<property name="cards">
<map>
<entry key="身份证" value="2131242141212"/>
<entry key="银行卡" value="68574577875248"/>
</map>
</property>
<!--第六种 set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>LOL</value>
<value>CS</value>
</set>
</property>
<!--第七种 null-->
<property name="wife">
<null/>
</property>
<!--第八种 properties-->
<property name="info">
<props>
<prop key="学号">246363</prop>
<prop key="性别">男</prop>
<prop key="username">张三</prop>
</props>
</property>
</beans>
测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
结果:
Student{
name=‘张三’,
address=Address{address=‘null’},
books=[福尔摩斯, 计算机网络, 趣学算法],
hobbys=[听歌, 敲代码, 看电影],
cards={身份证=2131242141212,
银行卡=68574577875248},
games=[王者荣耀, LOL, CS],
wife=‘null’,
info={学号=246363, 性别=男, username=张三}}
6.3、其他方式注入
可以使用p命名空间和c命名空间
官方解释:
p 命名空间允许您使用元素的属性(而不是嵌套元素)来描述属性值协作 bean,或者两者并无关系。bean``<property/>
Spring 支持具有命名空间的可扩展配置格式,这些格式基于 XML 架构定义。p 命名空间未在 XSD 文件中定义,并且仅存在于 Spring 的核心中。beans
在 bean 定义中调用的 p 命名空间中显示一个属性。这告诉 Spring 包括属性声明。
p 命名空间不像标准 XML 格式那样灵活。例如,声明属性引用的格式与 以 结束的属性冲突,而标准 XML 格式则不冲突。我们建议您仔细选择方法,并传达给团队成员,以避免生成同时使用所有三种方法的 XML 文档
在 Spring 3.1 中引入的 c 命名空间允许用于配置构造函数参数而不是嵌套元素的内联属性。constructor-arg
命名空间使用与 bean 引用的约定相同的约定来设置构造函数参数的名称。同样,即使它不是在 XSD 架构中定义的(它存在于 Spring 内核中),也需要在 XML 文件中声明它。c:``p:``-ref
由于 XML 语法,索引表示法需要前导的存在,因为 XML 属性名称不能以数字开始(即使某些 I德允许)。相应的索引表示法也可用于元素,但通常不常用,因为声明的纯顺序通常足够。_``<constructor-arg>
<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"
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">
<!--p命名空间注入 可以直接注入属性的值 properties-->
<bean name="user" class="com.zhang.pojo.User" p:name="张三" p:age="16"/>
<bean name="user1" class="com.zhang.pojo.User" c:_0="李四" c:_1="59"/>
<bean name="user2" class="com.zhang.pojo.User" c:age="5289" c:name="王五"/>
</beans>
6.4、bean的作用域
类型表格
singleton | (默认)将单个 bean 定义范围到每个 Spring IoC 容器的单个对象实例。 |
---|---|
prototype | 将单个 bean 定义范围到任何数量的对象实例。 |
request | 将单个 bean 定义范围到单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有自己的 bean 实例,该实例是在单个 bean 定义背面创建的。仅在 Web 感知 Spring 的上下文中有效。ApplicationContext |
session | 将单个 bean 定义范围到 HTTP 的生命周期。仅在 Web 感知 Spring 的上下文中有效。Session``ApplicationContext |
application | 将单个 bean 定义范围到 的生命周期。仅在 Web 感知 Spring 的上下文中有效。ServletContext``ApplicationContext |
websocket | 将单个 bean 定义范围到 的生命周期。仅在 Web 感知 Spring 的上下文中有效。WebSocket``ApplicationContext |
-
singleton —–单例模式(Spring 默认)
<bean name="user2" class="com.zhang.pojo.User" c:age="5289" c:name="王五" scope="singleton"/>
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml"); User user = context.getBean("user2", User.class); User user2 = context.getBean("user2", User.class); System.out.println(user==user2);//true }
每次从容器中get的时候,都是同一个对象
运行机制图
-
prototype—–原型模式
<bean name="user2" class="com.zhang.pojo.User" c:age="5289" c:name="王五" scope="prototype"/>
@Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml"); User user = context.getBean("user2", User.class); User user2 = context.getBean("user2", User.class); System.out.println(user==user2);//false }
每次从容器中get的时候,都会产生一个新的对象
运行机制图
- 其他的request、session、application这些只能在web开发中使用