码迷,mamicode.com
首页 > 编程语言 > 详细

code From Spring In Action

时间:2015-03-16 00:48:56      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

code From Spring In Action

标签:

原文地址:http://www.cnblogs.com/focusahaha/p/4340805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!