网上一直没有找到很好的博客说明,想着还是自己整合一份材料算了。。。
本文的结构是以一个个小样例的形式给出,通过小样例来介绍一个个标签的作用。
样例1:
样例1展示了一个最简单的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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao"> <!-- additional collaborators and configuration for this bean go here --> </bean> <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao"> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for data access objects go here --> </beans>
标签名 | 所属上级标签 | 说明 |
<beans> | 放置所有bean对象配置信息的标签 | |
<bean> | <beans> | 用来声明一个bean对象的标签,内置属性用来描述该对象配置的一些信息 |
<bean id> | <bean> | <bean>中的属性,用一个字符串来标识这个bean对象,其他bean对象可以用这个字符串来引用这个bean |
<bean class> | <bean> | <bean>中的属性,用来说明这个bean是用那个java类创建的,需要使用java类的全限定名。 |
样例2:
样例2中的bean引用是基于样例1中的两个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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for services go here --> </beans>
标签名 | 所属上级标签 | 说明 |
<property> | <bean> | 用来声明bean中那些对其他bean依赖的属性 |
<property name> | <property> | <property>中的属性,用来声明是哪个类中属性是需要外部bean注入的 |
<property ref> | <property> | <property>中的属性,用来声明需要注入哪个外部的bean对象,使用外部bean对象的id字符串来说明 |
样例3:
样例3展示了基于多配置文件的配置文件写法。
<?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="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> </beans>
标签名 | 所属上级标签 | 说明 |
<import> | <beans> | 用来声明那些需要导入到当前配置文件中的其他配置文件 |
<import resource> | <import> | <import>中的属性,用来声明需要导入的配置文件所在的路径信息,支持绝对路径和相对路径 |
样例4:
样例4展示了为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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountDao" name = "testDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao"> <!-- additional collaborators and configuration for this bean go here --> </bean> <alias name="accountDao" alias="aliasDao" /> <!-- more bean definitions for data access objects go here --> </beans>
标签名 | 所属上级标签 | 说明 |
<bean name> | <bean> | <bean>中的属性,为bean对象定义别名 |
<alias> | <beans> | 用来在bean的外部为bean定义别名,针对别名和bean声明不在一个文件的情况 |
<alias name> | <alias> | <alias>中的属性,用来说明需要声明别名的bean的名称 |
<alias alias> | <alias> | <alias>中的属性,用来说明定义的别名的名称信息 |
样例5:
样例5展示了当对象只能用静态工厂方法创建时如何配置的例子。
<?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="clientService" class="examples.ClientService" factory-method="createInstance"/> <!-- more bean definitions for data access objects go here --> </beans>
public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; } }
<bean class>中放置的是提供静态工厂方法的类的全限定名。
标签名 | 所属上级标签 | 说明 |
<bean factory-method> | <bean> | <bean>中的属性,声明创建bean对象使用的工厂类方法 |
样例6:
样例6展示了当对象只能用普通的对象工厂方法创建时如何配置的例子。
<?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"> <!-- the factory bean, which contains a method called createInstance() --> <bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/> <!-- more bean definitions for data access objects go here --> </beans>
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); public ClientService createClientServiceInstance() { return clientService; } }相对于静态工厂方法,我们这边需要先声明一个提供实例工厂方法的bean对象,也就是clientLocator。然后去配置我们需要的使用工厂方法创建的bean对象的信息。
标签名 | 所属上级标签 | 说明 |
<bean factory-bean> | <bean> | <bean>中的属性,声明提供工厂方法的bean对象 |
关于ApplicationContext配置的其他博客:
Spring Framework系列--ApplicationContext配置标签详解(二)构造器依赖配置
扫描二维码关注公众号,回复:
1552725 查看本文章