5 changed files with 202 additions and 4 deletions
@ -0,0 +1,26 @@ |
|||||
|
package cn.chjyj.szwh.configure; |
||||
|
|
||||
|
import cn.chjyj.szwh.Interceptor.ChInterceptor; |
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
|
||||
|
/** |
||||
|
* 配置基础信息 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class ChWebMvcConfigurer implements WebMvcConfigurer { |
||||
|
//日志初始化
|
||||
|
private static Log log = LogFactory.getLog(ChWebMvcConfigurer.class); |
||||
|
/** |
||||
|
* 注册拦截器 |
||||
|
* @param registry |
||||
|
*/ |
||||
|
@Override |
||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||
|
registry.addInterceptor(new ChInterceptor()).addPathPatterns("/**"); |
||||
|
log.debug("拦截器注册:"+registry.toString()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,116 @@ |
|||||
|
package cn.chjyj.szwh.configure; |
||||
|
|
||||
|
import cn.chjyj.employee.constant.ChConstant; |
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
|
||||
|
import javax.servlet.Filter; |
||||
|
import javax.servlet.Servlet; |
||||
|
import javax.sql.DataSource; |
||||
|
|
||||
|
import org.apache.ibatis.session.SqlSessionFactory; |
||||
|
import org.mybatis.spring.SqlSessionFactoryBean; |
||||
|
import org.mybatis.spring.SqlSessionTemplate; |
||||
|
import org.mybatis.spring.annotation.MapperScan; |
||||
|
import org.springframework.beans.factory.annotation.Qualifier; |
||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
import com.alibaba.druid.pool.DruidDataSource; |
||||
|
import com.alibaba.druid.support.http.StatViewServlet; |
||||
|
import com.alibaba.druid.support.http.WebStatFilter; |
||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.util.Properties; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* druid 数据库链接配置 |
||||
|
* 涉及多数据源,此处需要明确指定扫描包的位置 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@MapperScan(basePackages = {"cn.chjyj.employee.mapper"},sqlSessionFactoryRef = "chSqlSessionFactory") |
||||
|
public class DruidConfig { |
||||
|
private Log log = LogFactory.getLog(DruidConfig.class); //初始化日志
|
||||
|
static final String CHMAPPER_LOCATION = "classpath:mapper/employee/*.xml"; |
||||
|
|
||||
|
/** |
||||
|
* 配置为主要数据源 |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
|
||||
|
@Bean(name = "chDataSource") |
||||
|
@Qualifier("chDataSource") |
||||
|
public DataSource druidDataSource() throws Exception{ |
||||
|
//配置文件的真实路径
|
||||
|
String dbconf = ChConstant.WORK_DIR + ChConstant.DB_CONF; |
||||
|
Properties prop = new Properties(); |
||||
|
prop.load(new FileReader(dbconf)); |
||||
|
log.info("master druid:"+dbconf); |
||||
|
//使用datasource方式
|
||||
|
DruidDataSource dataSource = new DruidDataSource(); |
||||
|
String dburl = prop.getProperty("druid.url"); |
||||
|
dataSource.setUrl(dburl); |
||||
|
dataSource.setUsername(prop.getProperty("druid.username")); |
||||
|
dataSource.setPassword(prop.getProperty("druid.password")); |
||||
|
|
||||
|
return dataSource; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Bean |
||||
|
public ServletRegistrationBean<Servlet> druidServlet() { |
||||
|
// 进行 druid 监控的配置处理
|
||||
|
ServletRegistrationBean<Servlet> srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/**"); |
||||
|
// 白名单
|
||||
|
srb.addInitParameter("allow", "127.0.0.1"); |
||||
|
// 黑名单
|
||||
|
//srb.addInitParameter("deny", "192.168.31.253");
|
||||
|
// 用户名
|
||||
|
//srb.addInitParameter("loginUsername", "root");
|
||||
|
// 密码
|
||||
|
//srb.addInitParameter("loginPassword", "root");
|
||||
|
// 是否可以重置数据源
|
||||
|
srb.addInitParameter("resetEnable", "false"); |
||||
|
log.info("druid监控配置:" + srb); |
||||
|
return srb; |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public FilterRegistrationBean<Filter> filterRegistrationBean() { |
||||
|
FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>(); |
||||
|
frb.setFilter(new WebStatFilter()); |
||||
|
// 所有请求进行监控处理
|
||||
|
frb.addUrlPatterns("/*"); |
||||
|
// 排除名单
|
||||
|
frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*"); |
||||
|
return frb; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Bean(name="chSqlSessionFactory") |
||||
|
public SqlSessionFactory aisSqlSessionFactory(@Qualifier("chDataSource") DataSource dataSource) throws Exception { |
||||
|
SqlSessionFactoryBean bean=new SqlSessionFactoryBean(); |
||||
|
bean.setDataSource(dataSource); |
||||
|
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DruidConfig.CHMAPPER_LOCATION)); |
||||
|
log.info("chSqlSessionFactory"+bean.toString()); |
||||
|
return bean.getObject(); |
||||
|
} |
||||
|
|
||||
|
//配置事务
|
||||
|
@Bean(name="transactionManager") |
||||
|
public DataSourceTransactionManager testTransactionManager(@Qualifier("chDataSource") DataSource dataSource) { |
||||
|
return new DataSourceTransactionManager(dataSource); |
||||
|
} |
||||
|
|
||||
|
//
|
||||
|
@Bean(name="sqlSessionTemplate") |
||||
|
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("chSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { |
||||
|
return new SqlSessionTemplate(sqlSessionFactory); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package cn.chjyj.szwh.configure; |
||||
|
|
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.core.env.Environment; |
||||
|
|
||||
|
@Configuration |
||||
|
public class EnvConfig { |
||||
|
private Log log = LogFactory.getLog(EnvConfig.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private Environment env; |
||||
|
|
||||
|
/** |
||||
|
* 获取大数据链接信息 |
||||
|
* 项目初始化的时候完成操作 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Bean |
||||
|
public static String yxDbConf(){ |
||||
|
//读取当前运行位置
|
||||
|
String curPath=System.getProperty("user.dir"); |
||||
|
return curPath; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue