《Spring》篇章整体栏目
—————————————————————————————
【第一章】spring 概念与体系结构
【第二章】spring IoC 的工作原理
【第三章】spring IOC与Bean环境搭建与应用
【第四章】spring bean定义
【第五章】Spring 集合注入、作用域
【第六章】Spring 自动装配
【第七章】spring AOP
【第八章】Spring 集成JdbcTemplate
【第九章】Spring数据库事务管理
【第十章】Spring 集成Redis
【第十一章】Spring实战之打造新闻系统后端接口
—————————————————————————————
目录
—————————————————————————————
1、Spring bean定义
由 Spring IoC 容器管理的对象称为 Bean,Bean 根据 Spring 配置文件中的信息创建。
Spring 配置文件支持两种格式,即 XML 文件格式和 Properties 文件格式。
- Properties 配置文件主要以 key-value 键值对的形式存在,只能赋值,不能进行其他操作,适用于简单的属性配置。
- XML 配置文件采用树形结构,结构清晰,相较于 Properties 文件更加灵活。但是 XML 配置比较繁琐,适用于大型的复杂的项目。
参考bean.xml
元素中包含多个属性或子元素
属性名称 | 描述 |
---|---|
id | Bean 的唯一标识符,Spring IoC 容器对 Bean 的配置和管理都通过该属性完成。id 的值必须以字母开始,可以使用字母、数字、下划线等符号。 |
name | 该属性表示 Bean 的名称,我们可以通过 name 属性为同一个 Bean 同时指定多个名称,每个名称之间用逗号或分号隔开。Spring 容器可以通过 name 属性配置和管理容器中的 Bean。 |
class | 该属性指定了 Bean 的具体实现类,它必须是一个完整的类名,即类的全限定名。 |
scope | 表示 Bean 的作用域,属性值可以为 singleton(单例)、prototype(原型)、request、session 和 global Session。默认值是 singleton。 |
constructor-arg | 元素的子元素,我们可以通过该元素,将构造参数传入,以实现 Bean 的实例化。该元素的 index 属性指定构造参数的序号(从 0 开始),type 属性指定构造参数的类型。 |
property | 元素的子元素,用于调用 Bean 实例中的 setter 方法对属性进行赋值,从而完成属性的注入。该元素的 name 属性用于指定 Bean 实例中相应的属性名。 |
ref | 和 等元素的子元索,用于指定对某个 Bean 实例的引用,即 元素中的 id 或 name 属性。 |
value | 和 等元素的子元素,用于直接指定一个常量值。 |
list | 用于封装 List 或数组类型的属性注入。 |
set | 用于封装 Set 类型的属性注入。 |
map | 用于封装 Map 类型的属性注入。 |
entry | |
init-method | 容器加载 Bean 时调用该方法,类似于 Servlet 中的 init() 方法 |
destroy-method | 容器删除 Bean 时调用该方法,类似于 Servlet 中的 destroy() 方法。该方法只在 scope=singleton 时有效 |
lazy-init | 懒加载,值为 true,容器在首次请求时才会创建 Bean 实例;值为 false,容器在启动时创建 Bean 实例。该方法只在 scope=singleton 时有效 |
2、Spring Bean属性注入
Spring 主要通过以下 2 种方式实现属性注入:
- 构造函数注入
- setter 注入(又称设值注入
2.1、构造函数注入
使用构造函数实现属性注入大致步骤如下:
- 在 Bean 中添加一个有参构造函数,构造函数内的每一个参数代表一个需要注入的属性;
- 在 Spring 的 XML 配置文件中,通过 及其子元素 对 Bean 进行定义;
- 在 元素内使用 元素,对构造函数内的属性进行赋值,Bean 的构造函数内有多少参数,就需要使用多少个 元素。
2.1.1、编写Java 实体类代码
例子 :
public class NewsType {
/**
* 类型编码
*/
private Integer code;
/**
* 类型名称
*/
private String typeName;
public NewsType(Integer code, String typeName) {
this.code = code;
this.typeName = typeName;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
@Override
public String toString() {
return "NewsType{" +
"code=" + code +
", typeName='" + typeName + '\'' +
'}';
}
}
public class News {
/**
* 新闻标题
*/
private String title;
/**
* 新闻内容
*/
private String content;
/**
* 新闻类型
*/
private NewsType newsType;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public NewsType getNewsType() {
return newsType;
}
public void setNewsType(NewsType newsType) {
this.newsType = newsType;
}
public News(String title, String content, NewsType newsType) {
this.title = title;
this.content = content;
this.newsType = newsType;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", newsType=" + newsType +
'}';
}
}
2.1.2、xml配置
bean.xml 放在src下面
<?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-3.0.xsd">
<bean id="news" class="com.hqyj.spring.bean.News">
<constructor-arg name="title" value="每日新闻"/>
<constructor-arg name="content" value="深圳市发布经济指数报表"/>
<constructor-arg name="newsType" ref="newsType"/>
</bean>
<bean id="newsType" class="com.hqyj.spring.bean.NewsType">
<constructor-arg name="code" value="100"/>
<constructor-arg name="typeName" value="时事新闻"/>
</bean>
</beans>
2.1.3、编写测试Java代码
import com.xxx.spring.bean.News;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class NewsApp {
public static void main(String[] args) {
//获取ApplicationContext容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("news.xml");
//获取名称为news的bean
News news = applicationContext.getBean("news", News.class);
System.out.println(news);
}
}
3、setter注入
使用 setter 注入的方式进行属性注入,大致步骤如下:
- 在 Bean 中提供一个默认的无参构造函数(在没有其他带参构造函数的情况下,可省略),并为所有需要注入的属性提供一个 setXxx() 方法;
- 在 Spring 的 XML 配置文件中,使用 及其子元素 对 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-3.0.xsd">
<bean id="news" class="com.xxx.spring.bean.News">
<property name="title" value="每日新闻"/>
<property name="content" value="深圳市发布经济指数报表"/>
<property name="newsType" ref="newsType"/>
</bean>
<bean id="newsType" class="com.xxx.spring.bean.NewsType">
<property name="code" value="100"/>
<property name="typeName" value="时事新闻"/>
</bean>
</beans>
3.1、setter 方式注入内部 Bean
<?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-3.0.xsd">
<bean id="news" class="com.hqyj.spring.bean.News">
<property name="title" value="每日新闻"/>
<property name="content" value="深圳市发布经济指数报表"/>
<property name="newsType">
<bean class="com.hqyj.spring.bean.NewsType">
<property name="code" value="100"/>
<property name="typeName" value="时事新闻"/>
</bean>
</property>
</bean>
<bean id="newsType" class="com.hqyj.spring.bean.NewsType">
<property name="code" value="100"/>
<property name="typeName" value="时事新闻"/>
</bean>
</beans>