标签:
1.典型的Spring配置文件
<?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-2.5.xsd"> </beans>
2.Spring beans 配置
<bean id="user" class="com.zbwang.demo.User"/> <bean id="user" class="com.zbwang.demo.User"> <constructor-arg value="10"/> </bean> <bean id="user" class="com.zbwang.demo.User"> <constructor-arg value="10"/> <constructor-arg ref="sonnet"/> </bean> <bean id="stage" class="com.zbwang.demo.Stage" factory-method="getInstance"/> <bean id="user" class="com.zbwang.demo.User" scope="prototype"/> <bean id="user" class="com.zbwang.demo.User" init-method="turnOn" destroy-method="turnOff"/>
InitializingBean声明了afterPropertiesSet方法作为初始化方法,而DisposableBean声明了destroy方法,使用这些接口的缺点是Bean和Spring的API产生了耦合,除非开发一个明显在Spring容器中使用的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-2.5.xsd" default-init-method="turnOn" default-destroy-method="turnOff"> </beans>
set注入
<bean id="user" class="com.zbwang.demo.User"> <property name="username" value="focusahaha"/> <property name="age" value="20"/>
<property name="neigbor" ref="melon"/>
</bean>
内部Bean
<bean id="user" class="com.zbwang.demo.User"> <property name="date"> <bean class="com.zbwang.demo.Date"/> </property> </bean>
P命名空间
<?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-2.5.xsd"> <bean id="user" class="com.zbwang.demo.User" p:username = "focusahaha" p:age = "20" p:neigbor-ref = "melon"/> </beans>
标签:
原文地址:http://www.cnblogs.com/focusahaha/p/4340805.html