《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 ...
随机推荐
- THUPC2018 城市地铁规划
$n$ 个点,你可以随意连成一棵树,一个点的贡献为 $F(度数) \space mod \space 59393$ ,$F$ 为给定多项式函数,不超过 $10$ 次 求这 $n$ 个点的最大贡献,和最 ...
- New Year and Buggy Bot
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are d ...
- NOIp2018集训test-10-24(am&pm)
李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...
- oracle如何insert into 多个values
稍微熟悉Oracle的都知道,如果我们想一条SQL语句向表中插入多个值的话,如果如下语句 INSERT INTO 某表 VALUES(各个值),VALUES(各个值),.....; 这样会报错的,因为 ...
- qq群文件管理
一.怎样登录QQ群空间查看.管理群文件 1)登录自己的QQ,打开主面板!小编在这里以访问自己的群“我们的六班”为例.2)鼠标移动到主面板中“我们的六班”群图标处,右建单击——选择“访问QQ群空间”—— ...
- jenkins 参数化构建和增加环境变量
1.参数化构建 2.增加环境变量 prepare an environment for the run,需要安装Environment Injector插件
- div 遮罩层 弹窗
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 备份Ubuntu系统
1.工具:再生龙 2.备份方法:使用再生龙将系统备份(在U盘/boot目录下有很多文件),可以通过这个U盘去装机.将这个U盘生成的文件发送给应用,应用会将其打包生成iso可发布版本: 3.操作步骤:
- debug时打到了URLClassLoader.class里面,
一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.
- java中I/O类
总结:输入流/输出流 方法,变量: package com.aini; //流类.输入输出流 import java.io.*; public class rtyeew {// (File file) ...