spring其他配置 (3)
一、自动装配 Autowired
Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式
- byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配.
- byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
<bean id="service" class="com.hp.spring.ref.Service" autowire="byName"></bean>
<bean id="action" class="com.hp.spring.ref.Action" autowire="byType"></bean>
二、bean的作用于singleton,prototype
默认情况下 bean 是单例的!
但有的时候, bean 就不能使单例的. 例如: Struts2 的 Action 就不是单例的! 可以通过 scope 属性来指定 bean 的作用域
- prototype: 原型的. 每次调用 getBean 方法都会返回一个新的 bean. 且在第一次调用 getBean 方法时才创建实例
- singleton: 单例的. 每次调用 getBean 方法都会返回同一个 bean. 且在 IOC 容器初始化时即创建 bean 的实例. 默认值
<bean id="dao2" class="com.hp.spring.ref.Dao" scope="prototype"></bean>
三、引入外部资源properties文件
<!-- 导入外部的资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
db.properties
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
四、SpEL表达式 (可以为属性进行动态的赋值)
例子中,使用#{girl.userName}将bean为girl的userName动态注入到bean为boy的对象中
<!-- 测试 SpEL: 可以为属性进行动态的赋值 -->
<bean id="girl" class="com.hp.spring.helloworld.User">
<property name="userName" value="测试"></property>
</bean>
<bean id="boy" class="com.hp.spring.helloworld.User" >
<property name="userName" value="测试姓名"></property>
<property name="wifeName" value="#{girl.userName}"></property>
</bean>
关于SpEL的使用,查看 SpEL的使用.md
五、通过工厂方式类创建bean
1 使用静态工厂来创建bean
<!-- 在 class 中指定静态工厂方法的全类名, 在 factory-method 中指定静态工厂方法的方法名 -->
<bean id="dateFormat" class="java.text.DateFormat" factory-method="getDateInstance">
<!-- 可以通过 constructor-arg 子节点为静态工厂方法指定参数 -->
<constructor-arg value="2"></constructor-arg>
</bean>
测试代码
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");
DateFormat dateFormat = (DateFormat) ctx.getBean("dateFormat");
System.out.println(dateFormat.format(new Date()));
打印出:
2016-7-3
2 使用实例工厂来创建bean
<!-- ①. 创建工厂对应的 bean -->
<bean id="simpleDateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd hh:mm:ss"></constructor-arg>
</bean>
<!-- ②. 有实例工厂方法来创建 bean 实例 -->
<!-- factory-bean 指向工厂 bean, factory-method 指定工厂方法(了解) -->
<bean id="datetime" factory-bean="simpleDateFormat" factory-method="parse">
<!-- 通过 constructor-arg 执行调用工厂方法需要传入的参数 -->
<constructor-arg value="1990-12-12 12:12:12"></constructor-arg>
</bean>
测试代码
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");
Date date = (Date) ctx.getBean("datetime");
System.out.println(date);
打印出:
Wed Dec 12 00:12:12 CST 1990
六、通过FactoryBean方式创建bean
<!-- 配置通过 FactroyBean 的方式来创建 bean 的实例(了解) -->
<bean id="user" class="com.hp.spring.ref.UserBean"></bean>
UserBean这个类需要实现FactoryBean借口
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.FactoryBean;
import com.hp.spring.helloworld.Car;
import com.hp.spring.helloworld.User;
public class UserBean implements FactoryBean<User>{
/**
* 返回的 bean 的实例
*/
@Override
public User getObject() throws Exception {
User user = new User();
user.setUserName("abc");
user.setWifeName("ABC");
List<Car> cars = new ArrayList<>();
cars.add(new Car("ShangHai", "BuiKe", 180, 300000));
cars.add(new Car("ShangHai", "CRUZE", 130, 150000));
user.setCars(cars);
return user;
}
/**
* 返回的 bean 的类型
*/
@Override
public Class<?> getObjectType() {
return User.class;
}
/**
* 返回的 bean 是否为单例的
*/
@Override
public boolean isSingleton() {
return true;
}
}
关于User类跟Car类,则是User引用了一个Car类的集合
其中,测试代码:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-auto.xml");
User user = (User) ctx.getBean("user");
System.out.println(user);
打印出:
User [userName=abc, cars=[Car [company=ShangHai, brand=BuiKe, maxSpeed=180, price=300000.0], Car [company=ShangHai, brand=CRUZE, maxSpeed=130, price=150000.0]]]
七、使用init-method="init" destroy-method="destroy"在bean的初始化和销毁的时候调用
<bean id="user" class="com.hp.spring.helloworld.User" init-method="init" destroy-method="destroy">
</bean>
User类中有init和destroy方法
public void init(){
System.out.println("init method...");
}
public void destroy(){
System.out.println("destroy method...");
}
整个系列项目代码: http://git.oschina.net/nmc5/spring
spring其他配置 (3)的更多相关文章
- Spring Boot -- 配置切换指南
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- spring 定时任务配置
1.(易)如何在spring中配置定时任务? spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 (程序中一般我们都是到过写的,直观些) 1.定义任务 < ...
- 两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz
1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportT ...
- Spring Cloud 配置服务
Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...
- spring事务配置详解
一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...
- spring MVC配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring mvc 配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring动态配置多数据源
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- 为什么在Spring的配置里,最好不要配置xsd文件的版本号
为什么dubbo启动没有问题? 原文链接:http://www.tuicool.com/articles/YRn67zM 这篇blog源于一个疑问: 我们公司使了阿里的dubbo,但是阿里的开源网站h ...
- Spring事务配置的五种方式(转发)
Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...
随机推荐
- leetcode-163周赛-1260-二维网格迁移
题目描述: 自己的提交: class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int] ...
- Dart编程实例 - 第一个Dart程序
Dart编程实例 - 第一个Dart程序 main() { print("Hello World!"); } 本文转自:http://codingdict.com/article/ ...
- vue基础四
1.绑定Html Class(在 v-bind 用于 class 和 style 时, Vue.js 专门增强了它.表达式的结果类型除了字符串之外,还可以是对象或数组) 1.1对象语法 传给v-bin ...
- linux 下vi与vim区别以及vim的使用
vi与vim vi编辑器是所有Unix及Linux系统下标准的编辑器,他就相当于windows系统中的记事本一样,它的强大不逊色于任何最新的文本编辑器.他是我们使用Linux系统不能缺少的工具.由于对 ...
- CTSC2019
(upd:随机立方体AC) 太菜了只会部分分.以后慢慢补坑吧…… 随机立方体: 30分: 正常人都能想到的的转移状态(我的确是弱智),从大往小填数,记录有多少个极大值点和三个方向上各占了多少.转移可以 ...
- Visual Studio 2012常用快捷键总结
合理使用快捷键可以提高开发效率.收集整理一些常用的快捷键以方便查看使用 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键"Ctrl + -": 2) ...
- Dubbo入门到精通学习笔记(十五):Redis集群的安装(Redis3+CentOS)、Redis集群的高可用测试(含Jedis客户端的使用)、Redis集群的扩展测试
文章目录 Redis集群的安装(Redis3+CentOS) 参考文档 Redis 集群介绍.特性.规范等(可看提供的参考文档+视频解说) Redis 集群的安装(Redis3.0.3 + CentO ...
- db2,用户名密码不对导致无法连接数据库: Reason: User ID or Password invalid. ERRORCODE=-4214, SQLSTATE=28000
文章目录 背景 解决 背景 qa需要db2的demo,运维给安装完db2,启动报错 com.ibm.db2.jcc.am.io: [jcc][t4][2013][11249][4.7.112] Con ...
- Dubbo入门到精通学习笔记(六):持续集成管理平台之Hudson 持续集成服务器的安装配置与使用
文章目录 安装Hudson 使用Hudson tips:自动化部署 附录:两个脚本 安装Hudson IP:192.168.4.221 8G 内存(Hudson 多个工程在同时构建的情况下比较耗内存) ...
- Linux 下tomcat+jdk+mysql安装
JDK 1.用xftp把 jdk1.8.0_65上传到local下 给他赋予最高权限 1)切换到顶级目录 cd ~ 2)然后切换到上级目录 cd .. 3)切换到local路径下 cd /us ...