Spring Javaベースの構成
3373 ワード
1、Javaベースの構成がある前に、XMLを使用してSpringを構成することができます.例えば、propertiesファイルを読み取ることができます(mavenが構築したプロジェクトを仮定します.src/main/resourcesの下にjdbc.properties、messages.propertiesがあります).次のように構成できます.
Javaベースの構成に変更しました
このSpring Beanのlocationsは org.springframework.core.io.Resourceインタフェース配列classpathパスの下にあるpropertiesファイルを読み込むので、 org.springframework.core.io.ClassPathResource このインプリメンテーションクラス(このインプリメンテーションクラスを使用するとclasspathパスの下にあるファイルを読み込むことを示すので、XMLでclasspath接頭辞を指定する必要はありません).Resourceインタフェース記述情報には、UrlResource、FileSystemResource、PathResourceなどの実装クラスまたはサブインタフェースがいくつか見られ、必要に応じて対応する実装クラスを選択することができる.
2、P r o p e r t y S o u r ces PlaceholderConfigurerクラスには以下の情報が記載されています.
PropertySources PlaceholderConfigurerは、読み込んだpropertiesファイルの内容を使用して、XMLの${...}形式のプレースホルダまたはJavaベースの構成の
3.Javaベースの構成Spring Bean間の依存関係.たとえば、前のdataSourceというSpring Beanに依存するDataSourceTransactionManagerを作成します.
@Beanを作成するメソッドパラメータに直接入力します.Springコンテナに複数のDataSourceインスタンスがある場合は、@Qualifierを使用してどのインスタンスに入力するかを指定できます.
classpath:jdbc.properties
classpath:messages.properties
Javaベースの構成に変更しました
@Configuration
@ComponentScan(value = "xxx.yyy.zzz")
public class AppConfig {
@Bean
public PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setFileEncoding("UTF-8");
configurer.setLocations(new ClassPathResource("jdbc.properties"), new ClassPathResource("database.properties"));
return configurer;
}
}
このSpring Beanのlocationsは org.springframework.core.io.Resourceインタフェース配列classpathパスの下にあるpropertiesファイルを読み込むので、 org.springframework.core.io.ClassPathResource このインプリメンテーションクラス(このインプリメンテーションクラスを使用するとclasspathパスの下にあるファイルを読み込むことを示すので、XMLでclasspath接頭辞を指定する必要はありません).Resourceインタフェース記述情報には、UrlResource、FileSystemResource、PathResourceなどの実装クラスまたはサブインタフェースがいくつか見られ、必要に応じて対応する実装クラスを選択することができる.
2、P r o p e r t y S o u r ces PlaceholderConfigurerクラスには以下の情報が記載されています.
Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within
bean definition property values and @Value annotations against the current Spring
Environment and its set of PropertySources.
PropertySources PlaceholderConfigurerは、読み込んだpropertiesファイルの内容を使用して、XMLの${...}形式のプレースホルダまたはJavaベースの構成の
@Value
を置き換えます.@Bean(value = "dataSource")
public DataSource getBasicDataSource(@Value(value = "${driverClassName}") String driverClassName,
@Value(value = "${url}") String url, @Value(value = "${username}") String username,
@Value(value = "${password}") String password) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
3.Javaベースの構成Spring Bean間の依存関係.たとえば、前のdataSourceというSpring Beanに依存するDataSourceTransactionManagerを作成します.
@Bean(value = "transactionManager")
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
@Beanを作成するメソッドパラメータに直接入力します.Springコンテナに複数のDataSourceインスタンスがある場合は、@Qualifierを使用してどのインスタンスに入力するかを指定できます.
@Bean(value = "transactionManager")
public DataSourceTransactionManager getDataSourceTransactionManager(
@Qualifier(value = "dataSource") DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}