modify
This commit is contained in:
parent
245bc6456c
commit
62f4e296d3
@ -19,14 +19,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||||
public class ServletConfig implements WebMvcConfigurer {
|
public class ServletConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DataSourceConfig sourceConfig;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置数据源
|
* 配置数据源
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DriverManagerDataSource dataSource() {
|
public DriverManagerDataSource dataSource(DataSourceConfig sourceConfig) {
|
||||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
dataSource.setDriverClassName(sourceConfig.getDriverClassName());
|
dataSource.setDriverClassName(sourceConfig.getDriverClassName());
|
||||||
dataSource.setUrl(sourceConfig.getUrl());
|
dataSource.setUrl(sourceConfig.getUrl());
|
||||||
@ -53,9 +50,9 @@ public class ServletConfig implements WebMvcConfigurer {
|
|||||||
* 定义事务管理器
|
* 定义事务管理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DataSourceTransactionManager transactionManager() {
|
public DataSourceTransactionManager transactionManager(DriverManagerDataSource dataSource) {
|
||||||
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
||||||
manager.setDataSource(dataSource());
|
manager.setDataSource(dataSource);
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package com.heibaiying.config;
|
package com.heibaiying.config;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : heibaiying
|
* @author : heibaiying
|
||||||
@ -15,7 +13,6 @@ import org.springframework.core.annotation.Order;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource(value = "classpath:mysql.properties")
|
@PropertySource(value = "classpath:mysql.properties")
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
|
||||||
public class DataSourceConfig {
|
public class DataSourceConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,14 +2,10 @@ package com.heibaiying.config;
|
|||||||
|
|
||||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;;
|
import org.springframework.context.annotation.Configuration;;
|
||||||
import org.springframework.context.annotation.DependsOn;
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
@ -26,19 +22,23 @@ import java.io.IOException;
|
|||||||
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
@ComponentScan(basePackages = {"com.heibaiying.*"})
|
||||||
public class ServletConfig implements WebMvcConfigurer {
|
public class ServletConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Autowired
|
/* @Autowired
|
||||||
private DataSourceConfig sourceConfig;
|
* private DataSourceConfig sourceConfig;
|
||||||
|
* 不要采用这种方式注入DataSourceConfig,由于类的加载顺序影响会报空指针异常
|
||||||
|
* 最好的方式是在DriverManagerDataSource构造中采用参数注入
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置数据源
|
* 配置数据源
|
||||||
|
* sourceConfig
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DriverManagerDataSource dataSource() {
|
public DriverManagerDataSource dataSource(DataSourceConfig sourceConfig) {
|
||||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
dataSource.setDriverClassName(sourceConfig.getDriverClassName());
|
||||||
dataSource.setUrl("jdbc:mysql://localhost:3306/mysql");
|
dataSource.setUrl(sourceConfig.getUrl());
|
||||||
dataSource.setUsername("root");
|
dataSource.setUsername(sourceConfig.getUsername());
|
||||||
dataSource.setPassword("root");
|
dataSource.setPassword(sourceConfig.getPassword());
|
||||||
return dataSource;
|
return dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,9 +74,9 @@ public class ServletConfig implements WebMvcConfigurer {
|
|||||||
* 定义事务管理器
|
* 定义事务管理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public DataSourceTransactionManager transactionManager() {
|
public DataSourceTransactionManager transactionManager(DriverManagerDataSource dataSource) {
|
||||||
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
DataSourceTransactionManager manager = new DataSourceTransactionManager();
|
||||||
manager.setDataSource(dataSource());
|
manager.setDataSource(dataSource);
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user