这是我的基本DAO实现类:
@Repository
public class MeetingDaoImpl implements MeetingDao {
@Autowired
JdbcTemplate jdbcTemplate;
public boolean insertNewMeeting(String initials, String meetingId, int numYears) {
int numRowsAffected = jdbcTemplate.update(SQLConstants.INSERT_NEW_MEETING,
new Object[] {initials.toLowerCase(), meetingId, numYears});
return numRowsAffected > 0;
}
}
jdbcTemplate
会自动从spring.datasource
文件中读取application.properties
属性,这很棒,但它包含了我不想提交的数据库密码。相反,我想从本地server.properties
文件中读取它,而我可以从Java类中轻松读取它。
有没有办法用Java配置jdbcTemplate
?我已经看过使用bean和XML的多个示例,但没有使用Java。
答案 0 :(得分:3)
只需声明一个JdbcTemplate
bean:
@Bean
JdbcTemplate jdbcTemplate() throws IllegalAccessException, InvocationTargetException, InstantiationException {
// extract this 4 parameters using your own logic
final String driverClassName = "org.h2.Driver";
final String jdbcUrl = "jdbc:h2:mem:test";
final String username = "sa";
final String password = "";
// Build dataSource manually:
final Class<?> driverClass = ClassUtils.resolveClassName(driverClassName, this.getClass().getClassLoader());
final Driver driver = (Driver) ClassUtils.getConstructorIfAvailable(driverClass).newInstance();
final DataSource dataSource = new SimpleDriverDataSource(driver, jdbcUrl, username, password);
// or using DataSourceBuilder:
final DataSource dataSource = DataSourceBuilder.create().driverClassName(driverClassName).url(jdbcUrl).username(username).password(password).build();
// and make the jdbcTemplate
return new JdbcTemplate(dataSource);
}
另一种方法是不在application.properties
文件中设置数据源参数,而是在运行时声明它。当您运行应用时,您可以覆盖application.properties
中定义的任何属性或定义新属性。
例如:
java -jar my-spring-boot-app.jar --spring.datasource.url=jdbc:h2:mem:test --spring.datasource.username=sa --spring.datasource.password=secret
更复杂的方法是使用Language Override entries that can work in Outlook 2016 and OWA或spring-cloud-config-server来管理您的设置。
答案 1 :(得分:0)
您可以提供以下属性:
var config = {
"map": {
"*" : {
'Magento_Ui/js/lib/validation/rules' : 'Test_Custom/js/validation/rules'
}
}
};
创建配置类:
datasource.local.url=
datasource.local.driver-class-name=
datasource.local.username=
datasource.local.password=
在主应用程序类中,包含此配置文件:
@Configuration
@ConfigurationProperties("datasource.local")
@EnableJpaRepositories(
basePackages = "com...",
transactionManagerRef = "localTransactionManager",
entityManagerFactoryRef = "localEntityManagerFactory"
)
public class OracleConfiguration {
@NotNull
private String username;
@NotNull
private String password;
@NotNull
private String url;
private String driverClassName;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUrl(String url) {
this.url = url;
}
@Bean
DataSource localDataSource() throws SQLException {
return DataSourceBuilder
.create()
.url(this.url)
.username(this.username)
.password(this.password)
.driverClassName(this.driverClassName)
.build();
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
@Bean
@Autowired
public PlatformTransactionManager localTransactionManager(@Qualifier("localEntityManagerFactory")
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
@Autowired
public LocalContainerEntityManagerFactoryBean localEntityManagerFactory(@Qualifier("localDataSource")DataSource dataSource) {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan("com...");
return factoryBean;
}
@Bean
@Autowired
public JdbcTemplate localJdbcTemplate(@Qualifier("localDataSource")DataSource dataSource) {
return new JdbcTemplate(dataSource);
}}
然后在您需要注入模板的类中:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class })
@EnableTransactionManagement
@Import({ OracleConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}}