Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域:
支持四种配置,分别是singleton,prototype,request,session。

singleton
默认情况下在spring confinguration xml文件中的一个bean配置中,如果不指定scope属性,则这个scope默认值为singleton。
如何把一个bean配置为scope='singleton':

Person.java
package com.dx.beans.scope;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {
System.out.println("Person's Constructor...");
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
springcfg.xml
<bean id="person" class="com.dx.beans.scope.Person" scope="singleton">
<property name="id" value="1"></property>
<property name="name" value="person demo"></property>
</bean>
配置为singleton的bean将会在初始化Spring时,初始化该bean的类。
package com.dx.beans.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("springcfg.xml");
}
}
打印信息为:
Person's Constructor...
而且在以后调用该bean时,不会再初始化给bean的类。
package com.dx.beans.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("springcfg.xml"); Person person = (Person) cxt.getBean("person");
Person person1 = (Person) cxt.getBean("person"); System.out.println(person);
System.out.println(person1);
}
}
打印信息为:
Person's Constructor...
Person [id=1, name=person demo]
Person [id=1, name=person demo]
prototype
每次调用时,都会实例化一次bean的class。
spring configuration 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:p="http://www.springframework.org/schema/p"
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-4.3.xsd"> <bean id="person" class="com.dx.beans.scope.Person" scope="prototype">
<property name="id" value="1"></property>
<property name="name" value="person demo"></property>
</bean>
</beans>
此时,客户端调用:
package com.dx.beans.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("springcfg.xml"); Person person = (Person) cxt.getBean("person");
System.out.println(person); Person person1 = (Person) cxt.getBean("person");
System.out.println(person1);
}
}
打印信息为:
Person's Constructor...
Person [id=1, name=person demo]
Person's Constructor...
Person [id=1, name=person demo]
web环境作用域 request,session
request,session应用在web环境作用域。
使用外部属性文件
1) 在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径、数据源配置信息等),而这些部署细节实际上需要和Bean配置相分离。
2)Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器。这个处理器允许用户将Bean配置的部分内容外移到属性文件中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性值来替换变量。
3)Spring还允许在属性文件中使用${propertyName},以实现属性之间的相互引用。
注册PropertyPlaceholderConfigurer
spring2.0时:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property >
</bean>
spring2.5之后:
可通过<context:property-placeholder>元素简化
--添加命名空间:<beans>中添加context Schema定义(xmlns:context="http://www.springframework.org/schema/context")
--在配置文件下加入配置:<context:property-placeholder location="classpath:db.properties"/>
需求:使用c3p0组件连接数据库,并把它注册到spring中。
使用外部文件之前:

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dx</groupId>
<artifactId>springlearning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<org.springframework.version>4.3.8.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Expression Language (depends on spring-core) Define this if you use
Spring Expression APIs (org.springframework.expression.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Application Context (depends on spring-core, spring-expression, spring-aop,
spring-beans) This is the central artifact for Spring's Dependency Injection
Container and is generally always defined -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,
spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency> </dependencies>
<build>
<finalName>demo</finalName>
</build>
</project>
bean-properties.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dlog32"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
</beans>
Client.java调用:
package com.dx.beans.properties; import javax.sql.DataSource; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) throws Exception {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource dataSource = (DataSource) cxt.getBean("dataSource"); System.out.println(dataSource.getConnection());
}
}
使用外部文件之后:
添加外部文件c3p0-db.properties
db.user=root
db.password=root
db.jdbcUrl=jdbc:mysql://localhost/dlog32
db.driverClass=com.mysql.jdbc.Driver
修改spring bean配置文件beans-properties.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 导入外部文件 -->
<context:property-placeholder location="classpath:c3p0-db.properties" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 使用外部文件的配置信息替代配置项中的值 -->
<property name="user" value="${db.user}"></property>
<property name="password" value="${db.password}"></property>
<property name="jdbcUrl" value="${db.jdbcUrl}"></property>
<property name="driverClass" value="${db.driverClass}"></property>
</bean>
</beans>
Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件的更多相关文章
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...
- IoC容器-Bean管理XML方式(引入外部属性文件)
IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...
- 十八 Spring的JDBC模板:引入外部属性文件
配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...
- spring 使用外部属性文件
一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...
- Spring基础12——使用外部属性文件
1.使用外部属性文件 在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径.数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改 ...
随机推荐
- 用Visio画泳道图
在一次会议中看到有个同事在讲解业务流程时画了一个与PD中很类似的泳道图,但是在图的左侧确有一个阶段的列,事后与他沟通,才知道他这个图是”拼”出来的,也就是说所有的图都是他一点点的在画图工具中做出来的. ...
- ASP.NET 2.0
http://www.cnblogs.com/linezero/p/nightlynetcore2.html
- muduo 的 shutdown() 没有直接关闭 TCP 连接?
http://blog.csdn.net/Solstice/article/details/6208634 今天收到一位网友来信: 在 simple 中的 daytime 示例中,服务端主动关闭时调用 ...
- 一个例子来看C#泛型是如何登场的
有这样一个有关汽车的类. public class Car { public int ID { get; set; } public string Make { get; set; } } 现在,在客 ...
- log4j直接输出日志到flume
log4j.properties配置: log4j.rootLogger=INFOlog4j.category.com.besttone=INFO,flumelog4j.appender.flume ...
- [转]浅论ViewController的加载 -- 解决 viewDidLoad 被提前加载的问题(pushViewController 前执行)
一个ViewController,一般通过init或initWithNibName来加载.二者没有什么不同,init最终还是要调用initWithNibName方法(除非这个ViewControlle ...
- layoutSubviews 在什么情况下会被触发
layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews 2.addSubview会触发layoutSubviews 3.设置view的Frame会触 ...
- 【Centos】centos查看磁盘使用情况
1.查看分区和磁盘 lsblk 查看分区和磁盘 2.查看空间使用情况 df -h 查看空间使用情况 3.分区工具查看分区信息 fdisk -l 分区工具查看分区信息 4.查看分区 cfdisk /de ...
- C++ inline内联函数
inline 函数避免函数调用的开销 // find longer of two strings const string &shorterString(const string &s ...
- HTML5学习笔记简明版(8):新增的全局属性
contenteditable属性 不论什么元素使用contenteditable属性的话,代表该元素是一个可编辑的区域. 用户能够改变元素的内容以及操作标记.比如: <p contentedi ...