xml
<?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"
    xmlns:util
="http://www.springframework.org/schema/util"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <context:annotation-config/>
    
<!-- picks up and registers AppConfig as a bean definition -->
    
<context:component-scan base-package="com.web.spring.other" />
    

    
<bean class="com.web.spring.other.AppConfig"/>
访法一
    <context:property-placeholder location="classpath:jdbc.properties" />
     方法二
    
<util:properties id="jdbcProperties" location="classpath:jdbc.properties"/>
</beans>

实现一
package com.web.spring.other;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(
"classpath*:spring/spring-properties.xml")
public class AppConfig {
    
private @Value("${jdbc.driverClassName}") String driverClassName;
    @Bean(initMethod 
= "init")
    
public JDBCBean jdbc(){
        JDBCBean jdbc
=new JDBCBean();
        jdbc.setDriverClassName(driverClassName);
        
return jdbc;
    }

}
jdbc.driverClassName=org.hsqldb.jdbcDriver


实现二
package com.web.spring.other;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    
private @Value("#{jdbcProperties.driverClassName}") String driverClassName;
    //private @Value("#{jdbcProperties['jdbc.driverClassName']}") String driverClassName;
    @Bean(initMethod 
= "init")
    
public JDBCBean jdbc(){
        JDBCBean jdbc
=new JDBCBean();
        jdbc.setDriverClassName(driverClassName);
        
return jdbc;
    }

}
driverClassName=org.hsqldb.jdbcDriver



posted on 2010-09-29 12:09 岁月神偷 阅读(5553) 评论(1)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: