http://blog.sina.com.cn/fulaoshi[订阅][手机订阅]
字体大小: 正文
Spring 2.5 特性精简介绍(2009-02-22 16:00:40)

【IoC部分】
对于Properties属性的设置
<bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- typed as a java.util.Properties -->
    <property name="properties">
        <value>
            jdbc.driver.className=com.mysql.jdbc.Driver
            jdbc.url=jdbc:mysql://localhost:3306/mydb
        </value>
    </property>
</bean>

 

内部Bean,用于只注入一次的Bean
<bean id="outer" class="...">
    <property name="target">
        <!-- this is the inner bean -->
        <bean class="com.example.Person">
            <property name="name" value="Fiona Apple"/>
        </bean>
    </property>
</bean>

 

使用p-namespace简化Bean的属性定义
<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 name="john-classic" class="com.example.Person">
        <property name="name" value="John Doe"/>
        <property name="spouse" ref="jane"/>
    </bean>
    <!--时髦方式-->
    <bean name="john-modern" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/>

    <bean name="jane" class="com.example.Person">
        <property name="name" value="Jane Doe"/>
    </bean>
</beans>

 

depends-on属性
depends-on可以显示的声明bean之间的依赖关系,还可以决定bean初始化与销毁的顺序

 

PropertyPlaceholderConfigurer,如果bean的一些属性不想写在XML配置文件中,可以使用此类写到Properties文件中,方便用户修改。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

下面是Properties文件的内容
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

 

在Spring 2.5中利用context命名空间,也可以简写如下
<context:property-placeholder location="classpath:配置文件1,配置文件2"/>

 

MessageSources,用来加载资源文件,可实现I18N
<beans>
    <!-- this MessageSource is being used in a web application -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="test-messages"/>
    </bean>

    <bean id="example" class="com.foo.Example">
        <property name="messages" ref="messageSource"/>
    </bean>
</beans>

 

在web.xml中加载ApplicationContext,可以使用两种方式:Servlet或ServletListener,后者要求服务器实现Servlet 2.4规范,推荐使用。
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

其中参数contextConfigLocation的值支持ANT风格的通配符,比如/WEB-INF*Context.xml,表示WEB-INF及其子目录下所有以Context.xml结尾的文件

 

使用Annotation实现IoC
首先在配置文件中显式声明需要使用Annotation
<?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: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:annotation-config/>
</beans>

在setter方法前可以使用@Autowired表示自动装配、使用@Required表示属性需要被注入。@Autowired也可以用在非setter方法前、甚至用在构造函数或者属性前。

 

如果希望使用by-name注入,可以使用符合JSR-250的@Resource(name="xxxx")

 

@Repository, @Service, @Controller分别对应DAO,Service,控制器。可以用@Service(xxx)的方式给Bean起名字

 

自动检测组件<context:component-scan base-package="org.example"/>,可以用@scope控制实例数量

 

 


 

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...