配置文件db.properties
url=jdbc\:mysql\://localhost\:3306/seller_admin?useUnicode\=true&characterEncoding\=utf8 driver=com.mysql.jdbc.Driver username=root password=root maxActive=100 maxIdle=25 maxWait=28000 defaultAutoCommit=false
继承PropertyPlaceholderConfigurer类,读取.properties文件中的参数
public class AthenaPropertyConfigurer extends PropertyPlaceholderConfigurer { public void init(Properties props) throws IOException { String confPath = System.getProperties().getProperty("conf.path"); if (confPath == null) { confPath = "/data/admin/conf";//文件路径 } File file = new File(confPath); String[] urls = file.list((dir, name) -> name.endsWith(".properties")); for (int i = 0; i < urls.length; i++) { PropertiesLoaderUtils.fillProperties(props, new FileSystemResource(confPath + File.separator + urls[i])); } } @Override protected void loadProperties(Properties props) throws IOException { init(props); super.loadProperties(props); } }
在配置文件spring-config-file.xml和servlet.xml中添加配置:
<bean id="propertyConfigurer" class="AthenaPropertyConfigurer"><!--AthenaPropertyConfigurer的路径--> <property name="ignoreResourceNotFound" value="true"/> </bean>
做好上面两步之后就可以通过注解获取到.properties中的值了,例如数据库的配置。
第一种:通java类配置
@Configuration public class DaoConfig { @Bean public BasicDataSource dataSource( @Value("${driver}") String driver, @Value("${url}") String url, @Value("${username}") String username, @Value("${password}") String password, @Value("${defaultAutoCommit}") Boolean defaultAutoCommit, @Value("${maxActive}") Integer maxActive, @Value("${maxIdle}") Integer maxIdle, @Value("${maxWait}") Long maxWait ) throws IOException { //获取配置文件中的参数 BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver);//数据库驱动 dataSource.setUrl(url);//数据库地址 dataSource.setUsername(username);//用户名称 dataSource.setPassword(password);//密码 dataSource.setDefaultAutoCommit(defaultAutoCommit);//是否默认自动提交回滚 dataSource.setMaxActive(maxActive);//最大连接数,0表示没有限制 dataSource.setMaxIdle(maxIdle);//最大等待链接数,0没有限制 dataSource.setMaxWait(maxWait);//最大等待时间,单位:ms dataSource.setValidationQuery("select 1");//设置常用,MySQL默认select 1 dataSource.setConnectionInitSqls(Collections.singletonList("set names utf8mb4"));//设置编码 dataSource.setTestWhileIdle(true);//申请连接时检测 dataSource.setTestOnBorrow(true); dataSource.setTimeBetweenEvictionRunsMillis(3600000L);//检测空闲连接 dataSource.setMinEvictableIdleTimeMillis(18000000L);//断开空闲连接,知道最小连接数 return dataSource; } @Bean(name = "sqlSessionFactoryBeanName") @Autowired public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) { SqlSessionFactoryBean sqlFactory = new SqlSessionFactoryBean(); sqlFactory.setDataSource(dataSource); sqlFactory.setTypeAliasesPackage("com.admin.server.dao.dataobject"); sqlFactory.setConfigLocation(new ClassPathResource("com/admin/server/dao/sqlmap/sqlmap.xml")); ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor(); editor.setValue(null); editor.setAsText("classpath:com/admin/server/dao/sqlmap/mapping/*-mapping.xml"); sqlFactory.setMapperLocations((Resource[]) editor.getValue()); return sqlFactory; } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBeanName"); mapperScannerConfigurer.setBasePackage("com.admin.server.dao"); return mapperScannerConfigurer; } @Bean @Autowired public DataSourceTransactionManager txManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean @Autowired public TransactionTemplate transactionTemplate(DataSourceTransactionManager txManager) { return new TransactionTemplate(txManager); } }
第二种,直接在配置文件spring-config-file.xml中添加配置
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}"/> <property name="password" value="${password}" /> <property name="defaultAutoCommit" value="${defaultAutoCommit}" /> <property name="maxActive" value="${maxActive}" /> <property name="maxIdle" value="${maxIdle}" /> <property name="maxWait" value="${maxWait}" /> <property name="validationQuery" value="select 1" /> </bean>
自己在学习,说得不对的地方希望大家指出来,相互学习。