5 changed files with 190 additions and 0 deletions
@ -0,0 +1,115 @@ |
|||||
|
package app.bcms.jchat.config; |
||||
|
|
||||
|
|
||||
|
import app.bcms.jchat.constant.ChConstant; |
||||
|
import com.alibaba.druid.pool.DruidDataSource; |
||||
|
import com.alibaba.druid.support.http.StatViewServlet; |
||||
|
import com.alibaba.druid.support.http.WebStatFilter; |
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
||||
|
|
||||
|
import javax.servlet.Filter; |
||||
|
import javax.servlet.Servlet; |
||||
|
import javax.sql.DataSource; |
||||
|
import java.io.FileReader; |
||||
|
import java.util.Properties; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* druid 数据库链接配置 |
||||
|
* 涉及多数据源,此处需要明确指定扫描包的位置 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@MapperScan(basePackages = {"app.bcms.*.mapper"},sqlSessionFactoryRef = "sqlSessionFactory") |
||||
|
public class DruidConfig { |
||||
|
private Log log = LogFactory.getLog(DruidConfig.class); //初始化日志
|
||||
|
static final String CHMAPPER_LOCATION = "classpath:mappers/*xml"; |
||||
|
|
||||
|
/** |
||||
|
* 配置为主要数据源 |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
|
||||
|
@Bean(name = "dataSource") |
||||
|
@Qualifier("dataSource") |
||||
|
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.setDriverClassName(prop.getProperty("druid.driver-class")); |
||||
|
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", "xc1123"); |
||||
|
// 是否可以重置数据源
|
||||
|
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="sqlSessionFactory") |
||||
|
public SqlSessionFactory chSqlSessionFactory(@Qualifier("dataSource") 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("dataSource") DataSource dataSource) { |
||||
|
return new DataSourceTransactionManager(dataSource); |
||||
|
} |
||||
|
|
||||
|
//
|
||||
|
@Bean(name="sqlSessionTemplate") |
||||
|
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { |
||||
|
return new SqlSessionTemplate(sqlSessionFactory); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package app.bcms.jchat.config; |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
package app.bcms.jchat.config; |
||||
|
|
||||
|
import com.xtmis.suu.constant.ChConstant; |
||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; |
||||
|
|
||||
|
/** |
||||
|
* web mvc 的控制类 |
||||
|
*/ |
||||
|
|
||||
|
public class WebMvcConfig extends WebMvcConfigurationSupport { |
||||
|
/** |
||||
|
* 使用该类会使 application中的配置失效 |
||||
|
* 静态资源映射 |
||||
|
* @param registry |
||||
|
*/ |
||||
|
protected void addResourceHandlers(ResourceHandlerRegistry registry) { |
||||
|
String resDir = ChConstant.WORK_DIR+ChConstant.UPLOAD_PATH; |
||||
|
registry.addResourceHandler("/uploads/**").addResourceLocations("file:"+resDir); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
package app.bcms.jchat.constant; |
||||
|
|
||||
|
|
||||
|
import com.xtmis.suu.config.EnvConfig; |
||||
|
|
||||
|
/** |
||||
|
* 系统常量 |
||||
|
*/ |
||||
|
public class ChConstant { |
||||
|
//项目运行目录
|
||||
|
public static final String WORK_DIR = EnvConfig.yxDbConf(); |
||||
|
|
||||
|
//数据库链接文件路径
|
||||
|
public static final String DB_CONF = "/conf/db.properties"; |
||||
|
// 文件上传目录
|
||||
|
public static final String UPLOAD_PATH="/uploads"; |
||||
|
|
||||
|
//分页大小
|
||||
|
public static final int PAGESIZE = 12; |
||||
|
} |
||||
|
|
||||
Loading…
Reference in new issue