您的位置:首页 > 移动开发

Spring配置文件applicationContext.xml(2)配置详解

2016-09-20 19:49 429 查看
<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns si="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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
">

<context:component-scan base-package="com.persia">

<!-- 开启组件扫描 -->

</context:component-scan>

<context:annotation-config>

<!--开启注解处理器-->

</context:annotation-config>

<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 -->

<bean id="personServiceAnno"></bean>

<bean id="personDaoBeanAnno"></bean>

<bean id="personDaoBeanAnno2"></bean>

<!-- 自动注解 -->

<bean id="personServiceAutoInject" autowire="byName"></bean>

<bean id="personService">

<!-- 由spring容器去创建和维护,我们只要获取就可以了 -->

</bean>

<bean id="personService2" factory-method="createInstance" lazy-init="true"

init-method="init" destroy-method="destory">

<!-- 静态工厂获取bean -->

</bean>

<bean id="fac"></bean>

<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">

<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->

</bean>

<!-- ref方式注入属性 -->

<bean id="personDao"></bean>

<bean id="personService4">

<property name="personDao" ref="personDao"></property>

</bean>

<!-- 内部bean方式注入 -->

<bean id="personService5">

<property name="personDao">

<bean></bean>

</property>

<property name="name" value="persia"></property>

<property name="age" value="21"></property>

<property name="sets">

<!-- 集合的注入 -->

<set>

<value>第一个</value>

<value>第二个</value>

<value>第三个</value>

</set>

</property>

<property name="lists">

<!-- 集合的注入 -->

<list>

<value>第一个l</value>

<value>第二个l</value>

<value>第三个l</value>

</list>

</property>

<property name="properties">

<props>

<prop key="key1">value1</prop>

<prop key="key2">value2</prop>

<prop key="key3">value3</prop>

</props>

</property>

<property name="map">

<map>

<entry key="key1" value="value-1"></entry>

<entry key="key2" value="value-2"></entry>

<entry key="key3" value="value-3"></entry>

</map>

</property>

</bean>

<bean id="personService6">

<constructor-arg index="0" value="构造注入的name" ></constructor-arg>

<!-- 基本类型可以不写type -->

<constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">

</constructor-arg>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring 配置