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的作用域、使用外部属性文件的更多相关文章

  1. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  2. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  3. IoC容器-Bean管理XML方式(引入外部属性文件)

    IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...

  4. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 使用外部属性文件配置Bean以及Bean的生命周期方法

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

  6. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  7. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

  8. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  9. Spring基础12——使用外部属性文件

    1.使用外部属性文件 在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径.数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改 ...

随机推荐

  1. 新浪微博基于MySQL的分布式数据库实践

    提起微博,相信大家都是很了解的.但是有谁知道微博的数据库架构是怎样的呢?在今天举行的2011数据库技术大会上,新浪首席DBA杨海潮为我们详细解读了新浪微博的数据库架构——基于MySQL的分布式数据库实 ...

  2. delphi teechrt中TChart 一些属性设置

    把图片设置成黑白 2.设置颜色

  3. Vue 插件写法

    都说Vue2简单,上手容易,但小马过河,自己试了才晓得,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前随便拿全局变量和修改dom的锤子不能用了,变换到关注数据本身.vue的 ...

  4. C#编程(二十九)----------泛型接口

    泛型接口 定义 先来看一个简单的例子: public class Sharp {} public class Rectangle:Sharp {} 上面定义了两个简单的类,一个是图形类,一个是矩形类; ...

  5. JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>

    JSP 标准标记库( Standard Tag Library , JSTL) 是一组以标准化格式实现许多通用的 Web 站点功能的定制标记. JSP 技术的优势之一在于其定制标记库工具.除了核心 J ...

  6. Unity中的内存泄漏

    在对内存泄漏有一个基本印象之后,我们再来看一下在特定环境——Unity下的内存泄漏.大家都知道,游戏程序由代码和资源两部分组成,Unity下的内存泄漏也主要分为代码侧的泄漏和资源侧的泄漏,当然,资源侧 ...

  7. HTML5学习笔记:跨域

    在跨域安全性方面,有多个地方会有限制,主要是XMLHttpRequest对象的跨域限制和iFrame的跨域限制,下面我们分别来看一下. Ajax跨域(CORS) CORS是一个W3C标准,全称是&qu ...

  8. java把指定文字输出为图片流,支持文字换行

    public class IamgeUtils { private static final int WIDTH = 350; private static final int HEIGHT = 10 ...

  9. 超级账本环境搭建fabric

    :gotar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz vi ~/.profile export PATH=$PATH:/usr/local/go/b ...

  10. 开源项目MultiChoiceAdapter详解(二)——MultiChoiceArrayAdapter的使用

    MultiChoiceArrayAdapter其实就是可以多选的ArrayAdapter了,ArrayAdpter我们已经很熟悉了.MultiChoiceArrayAdapter这个类是抽象类,所以使 ...