《Spring实战》-2
装配Bean
1、装配wiring,即创建应用对象之间的协作关系的行为,者也是依赖注入的本质。
2、创建Spring配置
从Sring3.0开始,Spring容器提供了两种配置Bean的方式:
XML文件配置方式
基于Java注解的配置方式
3、典型的xml配置文件:

beans命名空间不是唯一的Spring命名空间,Spring核心框架自带了10个命名空间配置,如下:

4、当Spring容器加载Bean时,Spring使用反射来创建Bean。
5、覆盖Spring默认的单例配置:Spring Bean默认都是单例,但有时我们每次都需要获得唯一的Bean实例,比如每个人的门票要唯一:
我们只需要配置Bean的scope属性为prototype即可:
<bean id="ticket" class="com....Ticket" scope="prototype" />
Spring提供的作用域选项:

6、初始化和销毁Bean
Auditorium(舞台)要保证做的最先和最后两件事情:
开灯,关灯。

需要在Bean中使用init-method和destory-method属性:

当有很多上下文定义的Bean有相同名字的初始化方法和销毁方法时,可以直接在上层beans元素中声明default-init-method和default-destory-method属性,从而避免每一个都要设置一遍的问题。
示例:
1、程序结构:

2、简单的Spring装配
Performer.java接口
//<start id="performer_java" />
package com.springinaction.springidol;
public interface Performer {
void perform() throws PerformanceException;
}
//<end id="performer_java" />
Juggler.java实现类
//<start id="juggler_java" />
package com.springinaction.springidol;
public class Juggler implements Performer {
private int beanBags = 3;
public Juggler() {
}
public Juggler(int beanBags) {
this.beanBags = beanBags;
}
public void perform() throws PerformanceException {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}
//<end id="juggler_java" />
spring-idol.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="duke"
class="com.springinaction.springidol.Juggler" />
</beans>
JugglerTest.java测试类
package com.springinaction.springidol;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by LTN on 2016/4/17.
*/
public class JugglerTest {
public static void main(String[] args) throws Exception{
ApplicationContext context =new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer) context.getBean("duke");
// Performer performer = (Performer) context.getBean("poeticDuke");
performer.perform();
}
}
3、有参数的装配过程
PoeticJuggler.java
其构造器需要一个int和Poem的参数
//<start id="poeticjuggler_java" />
package com.springinaction.springidol;
public class PoeticJuggler extends Juggler {
private Poem poem;
public PoeticJuggler(Poem poem) { //<co id="co_injectPoem"/>
super();
this.poem = poem;
}
public PoeticJuggler(int beanBags, Poem poem) { // <co id="co_injectPoemBeanBags"/>
super(beanBags);
this.poem = poem;
}
public void perform() throws PerformanceException {
super.perform();
System.out.println("While reciting...");
poem.recite();
}
}
//<end id="poeticjuggler_java" />
参数在xml文件中,需要以下配置:
<constructor-arg value="15" />
<constructor-arg ref="sonnet29" />
value可以指定基本类型;
ref是引用其他的bean定义。
Poem.java接口
//<start id="poem_java" />
package com.springinaction.springidol;
public interface Poem {
void recite();
}
//<end id="poem_java" />
Sonnet29.java实现类
//<start id="sonnet29_java" />
package com.springinaction.springidol;
public class Sonnet29 implements Poem {
private static String[] LINES = {
"When, in disgrace with fortune and men's eyes,",
"I all alone beweep my outcast state",
"And trouble deaf heaven with my bootless cries",
"And look upon myself and curse my fate,",
"Wishing me like to one more rich in hope,",
"Featured like him, like him with friends possess'd,",
"Desiring this man's art and that man's scope,",
"With what I most enjoy contented least;",
"Yet in these thoughts myself almost despising,",
"Haply I think on thee, and then my state,",
"Like to the lark at break of day arising",
"From sullen earth, sings hymns at heaven's gate;",
"For thy sweet love remember'd such wealth brings",
"That then I scorn to change my state with kings." };
public Sonnet29() {
}
public void recite() {
for (int i = 0; i < LINES.length; i++) {
System.out.println(LINES[i]);
}
}
}
//<end id="sonnet29_java" />
spring-idol.java配置文件
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--<start id="duke_bean" />-->
<bean id="duke"
class="com.springinaction.springidol.Juggler" />
<!--<end id="duke_bean" />-->
<!--<start id="poeticduke_bean" />-->
<bean id="poeticDuke"
class="com.springinaction.springidol.PoeticJuggler">
<constructor-arg value="15" />
<constructor-arg ref="sonnet29" />
</bean>
<!--<end id="poeticduke_bean" />-->
<!--<start id="sonnet29_bean" />-->
<bean id="sonnet29"
class="com.springinaction.springidol.Sonnet29" />
<!--<end id="sonnet29_bean" />-->
</beans>
《Spring实战》-2的更多相关文章
- 《从零开始做一个MEAN全栈项目》(1)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在本系列的开篇,我打算讲一下全栈项目开发的优势,以及MEAN项目各个模块的概览. 为什么选择全栈开发? ...
- 《从零开始做一个MEAN全栈项目》(2)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一节简单介绍了什么是MEAN全栈项目,这一节将简要介绍三个内容:(1)一个通用的MEAN项目的技 ...
- 《从零开始做一个MEAN全栈项目》(3)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一篇文章给大家讲了一下本项目的开发计划,这一章将会开始着手搭建一个MEAN项目.千里之行,始于足下, ...
- 《从零开始做一个MEAN全栈项目》(4)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在上一篇中,我们讲了如何去构建第一个Express项目,总结起来就是使用两个核心工具,express和 ...
- 一个关于vue+mysql+express的全栈项目(一)
最近学了mysql数据库,寻思着能不能构思一个小的全栈项目,思来想去,于是就有了下面的项目: 先上几张效果图吧 目前暂时前端只有这几个页面,后端开发方面,有登录,注册,完善用户信息,获取用 ...
- Vue、Nuxt服务端渲染,NodeJS全栈项目,面试小白的博客系统~~
Holle,大家好,我是李白!! 一时兴起的开源项目,到这儿就告一段落了. 这是一个入门全栈之路的小项目,从设计.前端.后端.服务端,一路狂飙的学习,发量正在欣喜若狂~~ 接触过WordPress,H ...
- Vue、Node全栈项目~面向小白的博客系统~
个人博客系统 前言 ❝ 代码质量问题轻点喷(去年才学的前端),有啥建议欢迎联系我,联系方式见最下方,感谢! 页面有啥bug也可以反馈给我,感谢! 这是一套包含前后端代码的个人博客系统,欢迎各位提出建议 ...
- SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践
一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...
- 全栈项目|小书架|服务器端-NodeJS+Koa2实现首页图书列表接口
通过上篇文章 全栈项目|小书架|微信小程序-首页水平轮播实现 我们实现了前端(小程序)效果图的展示,这篇文章来介绍服务器端的实现. 首页书籍信息 先来回顾一下首页书籍都有哪些信息: 从下面的图片可以看 ...
- 全栈项目|小书架|服务器开发-NodeJS 使用 JWT 实现登录认证
通过这篇 全栈项目|小书架|服务器开发-JWT 详解 文章我们对JWT有了深入的了解,那么接下来介绍JWT如何在项目中使用. 安装 $ npm install jsonwebtoken 生成 Toke ...
随机推荐
- CodeForces - 651D:Image Preview (双指针&)
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...
- JavaWeb框架_Struts2_(四)----->表达式语言OGNL
2. 表达式语言OGNL 2.1 OGNL简介 OGNL(Object-Graph Navigation Language)对象图导航语言的缩写,OGNL是一种表达式语言(Expression L ...
- QT 中“ std::cerr ”的使用方法【转载】
std::cerr 标准错误输出流 std::cout 标准输出流 std::cerr 与 std::cout的最大不同是 cerr 是 不带输出缓冲 的,直接就可以输出到显示器上, 而 cout ...
- BZOJ2120:数颜色(莫队版)
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- BZOJ2120:数颜色(分块版)
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- resize2fs: Bad magic number in super-block while trying to open /dev/centos/root Couldn't find valid filesystem superblock
今天在进行lvm扩容之后,按照惯例进行 resize2fs 操作,发现报如下错误: # resize2fs /dev/centos/root resize2fs 1.42.9 (28-Dec-2013 ...
- 在Azure上通过Powershell创建多Interface的Cisco CSR路由器
前面通过Json的Template在Azure上创建了Cisco的CSR路由器.但那个Json的template只支持1块网卡.如果需要多网卡的Cisco CSR路由器,可以改上篇文章中提到的Json ...
- hihoCoder#1322(树的判定)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶点 M 条边的无向图 G ,判断 G 是不是一棵树. 输入 第一个是一个整数 T ,代表测试数据的组 ...
- java添加背景图片
总结:我们通常实现添加背景图片很容易,但是再添加按钮组件就会覆盖图片.原因是: 有先后啊.setlayout();与布局有很大关系 请调试代码的时候,仔细揣摩.我晕了 还可以添加文本框,密码框 fra ...
- Rails:rails链接多个数据库【转】
之前查到Rails应用中连接多个数据库的方式如下: class Cookie < ActiveRecord::Base establish_connection :typo ... end 这样 ...