spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
来源:互联网 发布:万讯网络中控 编辑:程序博客网 时间:2024/06/06 14:05
Bean的基本属性
id属性: Bean的唯一标识名。它必须是合法的XML ID,在配置文件中,不能有重复id的Bean,因为容器在获取Bean的实例时都用它来做唯一索引。
name属性: 用来为id创建一个或多个别名。它可以是任意的字母符合。多个别名之间用逗号,封号或空格分开。
class属性: 用来定义类的全限定名(包名+类名),class属性指明了Bean的来源。
下面是一个Bean的简单配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义第一个Bean实例:bean1 --> <bean id="bean1" name=”a;b;c” class="com.spring.Bean1" /> <!-- 定义第二个Bean实例:bean2 --> <bean id="bean2" class="com.spring.Bean2" /> </bean>
Abstract属性(默认为”false”):用来定义Bean是否为抽象Bean。它表示这个Bean将不会被实例化,一般用于父类Bean,因为父类Bean主要是供子类Bean继承使用。
(有些Bean中没有class,那他一定是被定义的模版,即它是一个抽象Bean)
抽象Bean的用法:
<?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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:orm="http://www.eclipse.org/eclipselink/xsds/persistence/orm" xmlns:oxm="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:persistence="http://www.eclipse.org/eclipselink/xsds/persistence" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "><bean id="car" class="org.spring.Car" p:name="奥迪" p:color="白色" p:clas="高级轿车" ></bean><bean id="person" class="org.spring.Person" p:name="张三" p:age="23" p:car-ref="car"></bean></beans>
Singleton属性(默认为“true”):定义Bean是否是Singleton(单例)。如果设为“true”,则在BeanFactory作用范围内,只维护此Bean的一个实例。如果设为“flase”,Bean将是Prototype(原型)状态,BeanFactory将为每次Bean请求创建一个新的Bean实例。
lazy-init属性(默认为“default”):用来定义这个Bean是否实现懒初始化。如果为“true”,它将在BeanFactory启动时初始化所有的SingletonBean。反之,如果为“false”,它只在Bean请求时才开始创建Singleton Bean。
Autowire属性(自动装配,默认为“default”):它定义了Bean的自动装载方式。
1、“no”:不使用自动装配功能。
2、“byName”:通过Bean的属性名实现自动装配。
3、“byType”:通过Bean的类型实现自动装配。
4、“constructor”:类似于byType,但它是用于构造函数的参数的自动组装。
5、“autodetect”:通过Bean类的反省机制(introspection)决定是使用“constructor”还是使用“byType”。
例如,通过Bean的属性名实现自动装配的配置文件:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="getDate" class="org.spring03.DateClass" autowire="byName"></bean><bean id="date" class="java.util.Date"></bean> </beans>
(自动装配虽然省事一些,但是会使结构变得不清晰,很少用,官方也不建议使用)
dependency-check属性(依赖检查,默认为“default”):它用来确保Bean组件通过JavaBean描述的所以依赖关系都得到满足。在与自动装配功能一起使用时。(自动装配很少用,所以…)
1、 none:不进行依赖检查。
2、 objects:只做对象间依赖的检查。
3、 simple:只做原始类型和String类型依赖的检查
4、 all:对所有类型的依赖进行检查。它包括了前面的objects和simple
depends-on属性(依赖对象):这个Bean在初始化时依赖的对象,这个对象会在这个Bean初始化之前创建。
init-method属性:用来定义Bean的初始化方法,它会在Bean组装之后调用。
destroy-method属性:用来定义Bean的销毁方法,它在BeanFactory关闭时调用。它只能应用于singletonBean。
关于init-method属性和destroy-method属性,前面已经单独介绍过,可以参考:spring之Bean的生命周期
factory-method属性:定义创建该Bean对象的工厂方法。它用于“factory-bean”,表示这个Bean是通过工厂方法创建。此时,“class”属性失效。
factory-bean属性:定义创建该Bean对象的工厂类。如果使用了“factory-bean”则“class”属性失效。
通常一些通用的配置,如连接数据库配置信息,会写到db.properties文件中,然后在XML配置文件中调用,那么调用外部properties等配置文件的方法是什么?
以连接数据库为例:
首先配置db.properties,写入数据库信息
接着在applicationContext.xml中调用它
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${user}"></property><property name="password" value="${password}"></property><property name="driverClass" value="${driverClass}"></property><property name="jdbcUrl" value="${jdbcUrl}"></property></bean></beans>
调用外部properties等配置文件,哪里用到哪里调用,注意不能放在Bean标签里面
最后,写一个Main方法函数测试一下:
public class Main {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");DataSource data = (DataSource)ac.getBean("dataSource");try {System.out.println(data.getConnection());} catch (SQLException e) {e.printStackTrace();}}}
运行结果:
说明读取信息并连接数据库成功。
- spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
- Spring中使用外部的properties配置文件
- Spring 中注入bean的properties配置文件位置问题解决
- Spring 中注入bean的properties配置文件位置问题解决
- spring配置文件中引用外部的properties文件
- spring框架访问properties配置文件的设置方法
- Spring配置文件中读取properties文件的属性
- Spring的applicationContext配置文件中<bean>标签的属性说明
- Spring 配置文件中Bean 属性id和name的区别
- Spring 配置文件中 Bean 的 property 属性使用示例
- Spring读取外部的.properties文件内容并设置给bean的两种方法
- JSP页面怎样调用Spring配置文件中定义的Bean
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring 配置文件的属性介绍
- 从spring配置文件中读取bean的方法
- tomcat中使用spring框架并调用其bean的一些方法
- Spring设置与读取.properties配置文件的bean
- 剑指offer:第10题求二进制1的个数
- Android开发(一、初窥安卓--2.运行应用)
- HashMap和HashTable的分析(ConurrentHashMap的衍生)
- iOS Copy与MutableCopy 和 Copy与Strong 深度解析
- Hibernate笔记一(Hibernate简单配置和操作)
- spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
- 【数据压缩】JPEG原理分析及JPEG解码器的调试
- hadoop Wordcount示例 出错
- Spring-2
- 文章标题
- C# 查询Xml文档
- 竞拍胜出者可以携一名宾客与库克在苹果新总部Apple Park共进午餐,时间不超过1小时。除了午餐费不用额外支付,旅行和食宿费都要有买家自己承担。此外,今年的竞拍与往年还有一个很大的不同:不包括苹果即
- Linux下命令行ssh密钥连接服务器
- SurfaceView