データベース関連のもの (EntityManager、DataSource、TransactionManager) をセットアップし、フィールドで使用@Value("${property.name}")
するプロパティを取得する DatabaseConfig クラスを作成したかったString
お気に入り
@Configuration
public class DataBaseConfig {
@Value("${hibernate.connection.username}")
private String hibernateConnectionUsername;
@Value("${hibernate.connection.password}")
private String hibernateConnectionPassword;
@Value("${hibernate.connection.driver_class}")
private String hibernateConnectionDriverClass;
@Value("${hibernate.connection.url}")
private String hibernateConnectionUrl;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.showSql}")
private String hibernateShowSql;
@Value("${hibernate.generateDdl}")
private String hibernateGenerateDdl;
// All my @Beans
}
問題は、プロパティ ファイルの値ではなく、これらすべての文字列が NULL であることです。
コードを自分のApplication
クラス (main
と で参照されているクラス) に入れるとSpringApplication.run(Application.class, args);
、値の注入が機能します。
つまり、 @Value は Application クラスでは機能しますが、カスタム @Configuration クラスでは機能しません:(
何が間違っている可能性がありますか?それとももっと情報が必要ですか?
更新: より多くのコード
方法 1、Application.java の DB Config と @Value は、PropertySourcesPlaceholderConfigurer の有無にかかわらず動作します
import java.beans.PropertyVetoException;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class)
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
// @Bean
// public static PropertySourcesPlaceholderConfigurer properties() {
// PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
// pspc.setLocations(new Resource[] { new ClassPathResource("application.properties") });
// return pspc;
// }
/*****************************/
@Value("${hibernate.connection.username}")
private String hibernateConnectionUsername;
@Value("${hibernate.connection.password}")
private String hibernateConnectionPassword;
@Value("${hibernate.connection.driver_class}")
private String hibernateConnectionDriverClass;
@Value("${hibernate.connection.url}")
private String hibernateConnectionUrl;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.showSql}")
private String hibernateShowSql;
@Value("${hibernate.generateDdl}")
private String hibernateGenerateDdl;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabasePlatform(hibernateDialect);
boolean generateDdl = Boolean.parseBoolean(hibernateGenerateDdl);
boolean showSql = Boolean.parseBoolean(hibernateShowSql);
vendorAdapter.setGenerateDdl(generateDdl);
vendorAdapter.setShowSql(showSql);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setDataSource(dataSource());
factory.setPackagesToScan("xxx");
return factory;
}
@Bean
public DataSource dataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(hibernateConnectionUsername);
dataSource.setPassword(hibernateConnectionPassword);
try {
dataSource.setDriverClass(hibernateConnectionDriverClass);
} catch (PropertyVetoException e) {
throw new IllegalArgumentException("Wrong driver class");
}
dataSource.setJdbcUrl(hibernateConnectionUrl);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
方法 2 (私がしたいこと)、独自のファイル (DatabaseConfing.java) 内の DB スタッフはPropertySourcesPlaceholderConfigurer
、DatabaseConfig 内の @Beans の後に常に呼び出されるため、(Application または DatabaseConfig) がある場所に関係なく機能しません:(
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
public class DatabaseConfig {
@Value("${hibernate.connection.username}")
private String hibernateConnectionUsername;
@Value("${hibernate.connection.password}")
private String hibernateConnectionPassword;
@Value("${hibernate.connection.driver_class}")
private String hibernateConnectionDriverClass;
@Value("${hibernate.connection.url}")
private String hibernateConnectionUrl;
@Value("${hibernate.dialect")
private String hibernateDialect;
@Value("${hibernate.showSql}")
private String hibernateShowSql;
@Value("${hibernate.generateDdl}")
private String hibernateGenerateDdl;
// @Bean
// public static PropertySourcesPlaceholderConfigurer properties() {
// PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
// pspc.setLocations(new Resource[] { new ClassPathResource("application.properties") });
// return pspc;
// }
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabasePlatform(hibernateDialect);
boolean generateDdl = Boolean.parseBoolean(hibernateGenerateDdl);
boolean showSql = Boolean.parseBoolean(hibernateShowSql);
vendorAdapter.setGenerateDdl(generateDdl);
vendorAdapter.setShowSql(showSql);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setDataSource(dataSource());
factory.setPackagesToScan("xxx");
return factory;
}
@Bean
public DataSource dataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(hibernateConnectionUsername);
dataSource.setPassword(hibernateConnectionPassword);
try {
dataSource.setDriverClass(hibernateConnectionDriverClass);
} catch (PropertyVetoException e) {
throw new IllegalArgumentException("Wrong driver class");
}
System.err.println(hibernateConnectionUrl);
dataSource.setJdbcUrl(hibernateConnectionUrl);
return dataSource;
}
}