Spring注解配置
package com.bookstore.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ComponentScan(value = "com.bookstore", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
@EnableAspectJAutoProxy(proxyTargetClass = false)
@EnableTransactionManagement()
public class SpringConfig {
@Bean
public DruidDataSource getDataSource() {
Properties dataSourceProperties = new Properties();
try {
dataSourceProperties.load(SpringConfig.class.getClassLoader().getResourceAsStream("dataSource.properties"));
return (DruidDataSource) DruidDataSourceFactory.createDataSource(dataSourceProperties);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}