使用Java配置在Spring JPA中设置附加属性
我正在尝试设置SpringMVC应用程序Java-config样式的一些额外属性。在这种情况下,我想设置使用Java配置在Spring JPA中设置附加属性,java,spring,jpa,Java,Spring,Jpa,我正在尝试设置SpringMVC应用程序Java-config样式的一些额外属性。在这种情况下,我想设置spring.jpa.show-sql=true 在我的PersistenceJPAConfig.java中,我有以下内容: @PropertySource("classpath:application.properties") //other annotated configurations public class PersistenceJPAConfig{ @Autowired pri
spring.jpa.show-sql=true
在我的PersistenceJPAConfig.java
中,我有以下内容:
@PropertySource("classpath:application.properties")
//other annotated configurations
public class PersistenceJPAConfig{
@Autowired
private Environment environment;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.banks.myapp" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties()); //fetches the properties
return em;
}
@Bean
public DataSource dataSource(){ //All datasource properties are retrieved fine
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("spring.datasource.driverClassName"));
//set password, username, etc.
return dataSource;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.getProperty("spring.jpa.show-sql");//not fetching/setting property
return properties;
}
以及我的application.properties:
spring.datasource.url=jdbc:jtds:sqlserver://mydatabase.com:5555/db
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driverClassName=net.sourceforge.jtds.jdbc.Driver
spring.jpa.show-sql: true
我找到的所有教程都教我如何通过xml配置额外的道具。如何通过properties
方法获取此属性或任何其他属性
编辑
这不是一个解决方案,但是使用setProperties()
手动设置属性会起作用,并且会显示我的SQL
properties.setProperty("spring.jpa.show-sql", "true");
您是否定义了
属性SourcePlaceHolderConfigure
bean?我很确定这是使用@PropertySource
所必需的。如果您这样做,请尝试将@PropertySource
值设置为classpath:/application.properties
(注意前导/)谢谢@Josh,根据Spring文档,我只需要propertySourcePlaceHolderConfigure
来设置占位符${/myplaceholder}
,否则,@PropertySource
应该是我所需要的全部。当我的应用程序连接到数据库时,将从.properties文件中检索我的属性。那么为什么不使用environment.getProperty(“spring.jpa.show sql”)呢?