配置文件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>
自己在學習,說得不對的地方希望大家指出來,相互學習。