Bean基本管理
BeanFactory /Application
1. BeanFactory:负责读取Bean定义文件;管理对象的加载、生成:维护Bean对象与Bean对象之间的依赖关系;负责Bean的生命周期。对于简单的应用程序来说,使用BeanFactory "就已经足够来管理Bean了,在对象的管理上已可以获得许多的方便性, BeanFactory接口包括了6种方法可以调用。
- boolean containsBean(String) 测试BeanFactory中是否包含指定名称的
- Bean.Object getBean(String) 指定Bean定义文件中设置的名称,可取得相对应的Bean实例。
- Object getBean(String, Class) 指定Bean定义文件中设置的名称,取得相对应的Bean实例,并转换(Cast)至指定的类。
- Class getType(String name) 指定Bean定义文件中设置的名称,取得相对应的Bean的Class实例。
- boolean isSingleton(String) 指定Bean定义文件中设置的名称,测试指定的Bean之scope是否为Singleton (之后在Bean的scope中会说明)
- Stringl getAliases(String) 指定Bean定义文件中设置的名称,取得该Bean所有的别名(之后在Bean的识别名称与别名中会说明)
2. Application:基于beanFactory建立的,具备读取bean的定义文件,维护bean的关系,除此之外,Application还提供一个应用程序所需的更完整的框架功能。 - 提供更方便的资源读取(resource file)方法。
- 提供解析文字信息的方法。
- 支持国际化信息
- Application可以发布时间,对时间感兴趣的bean可以接胡搜到这些事件。
Application中最常使用的3个(都在org.springframework.context.support下): - FileSystemXmlApplicationContext:指定xml的文件的相对路径或者绝对路径。
- ClassPathXmlApplicationContext:从classPath设置路径中读取xml文件。
- XmlWebApplicationContext:在web应用程序的文件架构中,指定相对位置读取文件。
(1)读取相对路径下的文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-config.xml");//获取xml配置文件
HelloBean helloBean= (HelloBean) applicationContext.getBean("helloBean");
(2)读取多个Bean定义的文件,可以在实例化Application的实现类时,以数组的形式指定bean文件定义文件的位置。
ApplicationContext contexts=new ClassPathXmlApplicationContext(new String[]{"bean-config.xml","bean-config2.xml"});
(3)可以指定*字符,读取classpath路径下以bean开头的所有xml。注意:此方法只在文件系统中有用,如果是在jar文件中,以下指定无效。
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean*.xml");
(4)当需要多个Bean定义文件时,spring开发团队建议使用ApplicationContext的方式来读取,好处是bean定义文件之间是各自独立的,不要一时到彼此的存在,另一个替代方式是使用标签。如bean-config3.xml
bean-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloBean"
class="com.hello.HelloBean"
>
<property name="helloword" >
<value>你好呀!</value>
</property>
</bean>
</beans>
bean-config2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ServiceDemo" class="com.archer.ServiceDemo"></bean>
<bean id="dao" class="com.archer.DaoDemo"></bean>
</beans>
bean-config3.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="bean-config.xml"/>
<import resource="bean-config3.xml"/>
<bean id="DaoDemo" class="com.archer.DaoDemo"></bean>
</beans>
注意:标签必须放置在标签之前,定义文件必须放置在同一个目录(比如src/archer/bean.xml就不能导入到bean-config3.xml中),以相对路径指定Bean定义文件位置,而每个定义文件的内容必须包括根标签。