1. AppConfig
@Configuration @ComponentScan public class AppConfig { public static ApplicationContext context; @Autowired private DBService dbService; @Bean DataSource createDataSource() throws IOException { HikariConfig config = new HikariConfig(); Properties props= dbService.getDbProperty(); config.setJdbcUrl(props.getProperty("jdbcUrl")); config.setUsername(props.getProperty("username")); config.setPassword(props.getProperty("password")); config.addDataSourceProperty("connectionTimeout", props.getProperty("connectionTimeout")); // 连接超时 config.addDataSourceProperty("idleTimeout", props.getProperty("idleTimeout")); // 空闲超时 config.addDataSourceProperty("maximumPoolSize", props.getProperty("maximumPoolSize")); // 最大连接数 config.addDataSourceProperty("autoCommit", props.getProperty("autoCommit")); // 自动提交 DataSource ds = new HikariDataSource(config); return ds; } }
2. DBService
@Component public class DBService { @Value("classpath:/jdbc.property") private Resource dbProperty;
public Properties getDbProperty() throws IOException { Properties props = new Properties(); try( BufferedReader reader = new BufferedReader( new InputStreamReader( dbProperty.getInputStream(), StandardCharsets.UTF_8)) ){ props.load(reader); } return props; } }
3. resources/jdbc.property
jdbcUrl=jdbc:mysql://localhost:3306/learnjdbc?useUnicode=true&characterEncoding=utf-8 username=root password=123456 # 连接超时:1秒 connectionTimeout=10000 # 空闲超时:60秒 idleTimeout=60000 # 最大连接数:10 maximumPoolSize=10 # 自动提交 autoCommit=true
4. AppListener
@WebListener public class AppListener implements ServletContextListener { // 在此初始化WebApp,例如打开数据库连接池等: public void contextInitialized(ServletContextEvent sce) { //创建bean AppConfig.context = new AnnotationConfigApplicationContext(AppConfig.class); System.out.println("WebApp initialized: ServletContext = " + sce.getServletContext()); }
Sign in to make a reply
净净一隅
1. AppConfig
@Configuration
@ComponentScan
public class AppConfig {
public static ApplicationContext context;
@Autowired
private DBService dbService;
@Bean
DataSource createDataSource() throws IOException {
HikariConfig config = new HikariConfig();
Properties props= dbService.getDbProperty();
config.setJdbcUrl(props.getProperty("jdbcUrl"));
config.setUsername(props.getProperty("username"));
config.setPassword(props.getProperty("password"));
config.addDataSourceProperty("connectionTimeout", props.getProperty("connectionTimeout")); // 连接超时
config.addDataSourceProperty("idleTimeout", props.getProperty("idleTimeout")); // 空闲超时
config.addDataSourceProperty("maximumPoolSize", props.getProperty("maximumPoolSize")); // 最大连接数
config.addDataSourceProperty("autoCommit", props.getProperty("autoCommit")); // 自动提交
DataSource ds = new HikariDataSource(config);
return ds;
}
}
2. DBService
@Component
public class DBService {
@Value("classpath:/jdbc.property")
private Resource dbProperty;
public Properties getDbProperty() throws IOException {
Properties props = new Properties();
try(
BufferedReader reader = new BufferedReader(
new InputStreamReader(
dbProperty.getInputStream(),
StandardCharsets.UTF_8))
){
props.load(reader);
}
return props;
}
}
3. resources/jdbc.property
jdbcUrl=jdbc:mysql://localhost:3306/learnjdbc?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
# 连接超时:1秒
connectionTimeout=10000
# 空闲超时:60秒
idleTimeout=60000
# 最大连接数:10
maximumPoolSize=10
# 自动提交
autoCommit=true
4. AppListener
@WebListener
public class AppListener implements ServletContextListener {
// 在此初始化WebApp,例如打开数据库连接池等:
public void contextInitialized(ServletContextEvent sce) {
//创建bean
AppConfig.context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("WebApp initialized: ServletContext = " + sce.getServletContext());
}