2016/1/18 9:01:56
1.资源文件的读取方式
<context:annotation-config></context:annotation-config>
<context:property-placeholder location="classpath:/Spring/src/jdbc.properties"/>
<bean class="com.Appconfig"></bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
或者在类中写注释:
package com.zjx.properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
@ImportResource("classpath:/Spring/src/jdbc.properties")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String passWord;
public DataSource dataSource(){
return new DriverManagerDataSource(url, userName, passWord);
}
}
2.@Bean and @Scope
- 默认@Bean是单例的