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. nuxus 3在Maven项目的配置和POM文件的配置

    在nuxus 3中的Maven默认会创建三个仓库,这三个仓库的关系如下: public是release和snapshot的全集,release默认为关闭状态,所以在配置nexus 3时需要将其开启. ...

  2. spring-boot 速成(11) - 单元测试

    一.添加依赖项: testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE' 二.单元测试代码示例 im ...

  3. 两个div如何并列 (转)

    两个div如何并列?当用到div+css代替table时,我习惯用两个方法: 1 <div id="parent"> <div id="child_1& ...

  4. Java clone() 浅克隆与深度克隆(转)

    以下文字转自:桔子园 http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html 现在Clone已经不是一个新鲜词语了,伴随着“ ...

  5. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  6. 在eclipse中查看Android源码

    声明:高手跳过此文章 当我们在eclipse中开发android程序的时候.往往须要看源码(可能是出于好奇,可能是读源码习惯),那么怎样查看Android源码呢? 比方以下这样的情况 图1 如果我们想 ...

  7. AT91 USB Composite Driver Implementation

    AT91 USB Composite Driver Implementation 1. Introduction The USB Composite Device is a general way t ...

  8. systemtap 调试postgrel

    http://blog.163.com/digoal@126/blog/static/16387704020137140265557/   dtrace http://blog.163.com/dig ...

  9. linux后台开发核心技术

    3. 常用STL的使用 3.1. string (1)string类的实现(使用strlen.strcpy.strcat.strcmp等,注意判NULL). (2)C++字符串和C字符串的转换:dat ...

  10. cefsharp wpf 中文输入问题解决方法

    摘要 最近在搞一个客户端的项目,考虑使用wpf,内嵌webView的方式,访问h5页面.所以使用了CefSharp组件,但发现一个问题,就是在输入中文的时候,无法输入. 解决办法 去官方github的 ...