您的位置:首页 > 编程语言 > Java开发

spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍

2017-05-17 20:06 671 查看

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"?>
<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"
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();
}
}
}


运行结果:

说明读取信息并连接数据库成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐