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;

/**
* @Author BaiYZ
* @Program BookStoreFrameWork
* @Description Spring 配置类
* @Date 2022-01-02 23:00:13
*/
@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;
}

//创建JDBCTemplate
@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;
}
}