Skip to content

Commit 11403af

Browse files
committed
多数据源,logback,发邮件,I18N
1 parent 5f14ca6 commit 11403af

27 files changed

+351
-168
lines changed

pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
<artifactId>mybatisplus-spring-boot-starter</artifactId>
8888
<version>${mybatisplus-spring-boot-starter.version}</version>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.springframework.boot</groupId>
92+
<artifactId>spring-boot-starter-aop</artifactId>
93+
</dependency>
9094
<dependency>
9195
<groupId>com.baomidou</groupId>
9296
<artifactId>mybatis-plus</artifactId>
@@ -158,12 +162,7 @@
158162
</dependency>
159163
<dependency>
160164
<groupId>org.springframework.boot</groupId>
161-
<artifactId>spring-boot-test</artifactId>
162-
</dependency>
163-
<dependency>
164-
<groupId>org.springframework</groupId>
165-
<artifactId>spring-test</artifactId>
166-
<version>4.2.0.RELEASE</version>
165+
<artifactId>spring-boot-starter-test</artifactId>
167166
</dependency>
168167
</dependencies>
169168

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.victor.common.aop;
2+
3+
import java.util.Arrays;
4+
import java.util.Enumeration;
5+
import javax.servlet.http.HttpServletRequest;
6+
import org.aspectj.lang.JoinPoint;
7+
import org.aspectj.lang.annotation.AfterReturning;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.aspectj.lang.annotation.Before;
10+
import org.aspectj.lang.annotation.Pointcut;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.core.annotation.Order;
14+
import org.springframework.stereotype.Component;
15+
import org.springframework.web.context.request.RequestContextHolder;
16+
import org.springframework.web.context.request.ServletRequestAttributes;
17+
18+
/**
19+
* Created by Aaron on 2017/12/20.
20+
* 实现Web层的日志切面
21+
*/
22+
@Aspect
23+
@Component
24+
@Order(-5)
25+
public class WebLogAspect {
26+
27+
private Logger logger = LoggerFactory.getLogger(this.getClass());
28+
29+
/**
30+
* 定义一个切入点.
31+
* 解释下:
32+
*
33+
* ~ 第一个 * 代表任意修饰符及任意返回值.
34+
* ~ 第二个 * 任意包名
35+
* ~ 第三个 * 代表任意方法.
36+
* ~ 第四个 * 定义在web包或者子包
37+
* ~ 第五个 * 任意方法
38+
* ~ .. 匹配任意数量的参数.
39+
*/
40+
@Pointcut("execution(public * com.victor.*.controller..*.*(..))")
41+
public void webLog(){}
42+
43+
@Before("webLog()")
44+
public void doBefore(JoinPoint joinPoint){
45+
46+
// 接收到请求,记录请求内容
47+
logger.info("WebLogAspect.doBefore()");
48+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
49+
HttpServletRequest request = attributes.getRequest();
50+
51+
52+
// 记录下请求内容
53+
logger.info("URL : " + request.getRequestURL().toString());
54+
logger.info("HTTP_METHOD : " + request.getMethod());
55+
logger.info("IP : " + request.getRemoteAddr());
56+
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
57+
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
58+
//获取所有参数方法一:
59+
Enumeration<String> enu=request.getParameterNames();
60+
while(enu.hasMoreElements()){
61+
String paraName=(String)enu.nextElement();
62+
System.out.println(paraName+": "+request.getParameter(paraName));
63+
}
64+
}
65+
66+
@AfterReturning("webLog()")
67+
public void doAfterReturning(JoinPoint joinPoint){
68+
// 处理完请求,返回内容
69+
logger.info("WebLogAspect.doAfterReturning()");
70+
}
71+
}

src/main/java/com/victor/config/datasource/DynamicDataSource.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@
88
*/
99
public class DynamicDataSource extends AbstractRoutingDataSource {
1010

11-
@Override protected Object determineCurrentLookupKey() {
11+
/**
12+
* /*
13+
* 代码中的determineCurrentLookupKey方法取得一个字符串,
14+
* 该字符串将与配置文件中的相应字符串进行匹配以定位数据源,配置文件,即applicationContext.xml文件中需要要如下代码:(non-Javadoc)
15+
* @see org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#determineCurrentLookupKey()
16+
*/
17+
@Override
18+
protected Object determineCurrentLookupKey() {
19+
20+
/**
21+
* DynamicDataSourceContextHolder代码中使用setDataSourceType
22+
* 设置当前的数据源,在路由类中使用getDataSourceType进行获取,
23+
* 交给AbstractRoutingDataSource进行注入使用。
24+
*/
1225
return DynamicDataSourceContextHolder.getDataSourceType();
1326
}
1427
}

src/main/java/com/victor/config/datasource/DynamicDataSourceAspect.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,32 @@ public class DynamicDataSourceAspect {
2020

2121
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);
2222

23+
/**
24+
* @Before("@annotation(ds)")
25+
* 的意思是:
26+
*
27+
* @Before:在方法执行之前进行执行:
28+
* @annotation(targetDataSource):
29+
* 会拦截注解targetDataSource的方法,否则不拦截;
30+
*/
31+
2332
@Before("@annotation(ds)")
2433
public void changeDataSource(JoinPoint point, TargetDataSource ds) throws Throwable {
34+
//获取当前的指定的数据源;
2535
String dsId = ds.name();
36+
//如果不在我们注入的所有的数据源范围之内,那么输出警告信息,系统自动使用默认的数据源。
2637
if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) {
2738
logger.error("数据源[{}]不存在,使用默认数据源 > {}", ds.name(), point.getSignature());
2839
} else {
2940
logger.debug("Use DataSource : {} > {}", ds.name(), point.getSignature());
41+
//找到的话,那么设置到动态数据源上下文中。
3042
DynamicDataSourceContextHolder.setDataSourceType(ds.name());
3143
}
3244
}
3345

3446
@After("@annotation(ds)")
3547
public void restoreDataSource(JoinPoint point, TargetDataSource ds) {
48+
//方法执行完毕之后,销毁当前数据源信息,进行垃圾回收。
3649
logger.debug("Revert DataSource : {} > {}", ds.name(), point.getSignature());
3750
DynamicDataSourceContextHolder.clearDataSourceType();
3851
}

src/main/java/com/victor/config/datasource/DynamicDataSourceRegister.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,16 @@ public void setEnvironment(Environment env) {
107107

108108
/**
109109
* 初始化主数据源
110-
*
111-
* @author SHANHY
112-
* @create 2016年1月24日
113110
*/
114111
private void initDefaultDataSource(Environment env) {
115112
// 读取主数据源
116113
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
117114
Map<String, Object> dsMap = new HashMap<>();
118115
dsMap.put("type", propertyResolver.getProperty("type"));
119-
dsMap.put("driver-class-name", propertyResolver.getProperty("driver-class-name"));
120-
dsMap.put("url", propertyResolver.getProperty("url"));
121-
dsMap.put("username", propertyResolver.getProperty("username"));
122-
dsMap.put("password", propertyResolver.getProperty("password"));
116+
dsMap.put("driver-class-name", propertyResolver.getProperty("base.driver-class-name"));
117+
dsMap.put("url", propertyResolver.getProperty("base.url"));
118+
dsMap.put("username", propertyResolver.getProperty("base.username"));
119+
dsMap.put("password", propertyResolver.getProperty("base.password"));
123120

124121
defaultDataSource = buildDataSource(dsMap);
125122

@@ -131,8 +128,6 @@ private void initDefaultDataSource(Environment env) {
131128
*
132129
* @param dataSource
133130
* @param env
134-
* @author SHANHY
135-
* @create 2016年1月25日
136131
*/
137132
private void dataBinder(DataSource dataSource, Environment env){
138133
RelaxedDataBinder dataBinder = new RelaxedDataBinder(dataSource);
@@ -157,13 +152,10 @@ private void dataBinder(DataSource dataSource, Environment env){
157152

158153
/**
159154
* 初始化更多数据源
160-
*
161-
* @author SHANHY
162-
* @create 2016年1月24日
163155
*/
164156
private void initCustomDataSources(Environment env) {
165157
// 读取配置文件获取更多数据源,也可以通过defaultDataSource读取数据库获取更多数据源
166-
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "custom.datasource.");
158+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
167159
String dsPrefixs = propertyResolver.getProperty("names");
168160
for (String dsPrefix : dsPrefixs.split(",")) {// 多个数据源
169161
Map<String, Object> dsMap = propertyResolver.getSubProperties(dsPrefix + ".");

src/main/java/com/victor/config/datasource/MultipleDataSourceBeanDefinitionRegistryPostProcessor.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ private void beanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistr
142142
/*
143143
* 获取到子属性,对应一个map;
144144
* 也就是这个map的key就是
145-
*
146145
* type、driver-class-name等;
147-
*
148-
*
149146
*/
150147
Map<String, Object> dsMap = propertyResolver.getSubProperties(dsPrefix + ".");
151148
//存放到一个map集合中,之后在注入进行使用.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.victor.config.i18n;
2+
3+
import java.util.Locale;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.servlet.LocaleResolver;
9+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
12+
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
13+
14+
/**
15+
* Created by Aaron on 2017/12/20.
16+
*/
17+
@Configuration
18+
@EnableAutoConfiguration
19+
@ComponentScan
20+
public class I18NConfig extends WebMvcConfigurerAdapter {
21+
22+
@Bean
23+
public LocaleResolver localeResolver() {
24+
SessionLocaleResolver slr = new SessionLocaleResolver();
25+
// 默认语言
26+
slr.setDefaultLocale(Locale.US);
27+
return slr;
28+
}
29+
30+
@Bean
31+
public LocaleChangeInterceptor localeChangeInterceptor() {
32+
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
33+
// 参数名
34+
lci.setParamName("lang");
35+
return lci;
36+
}
37+
38+
@Override
39+
public void addInterceptors(InterceptorRegistry registry) {
40+
registry.addInterceptor(localeChangeInterceptor());
41+
}
42+
}

src/main/java/com/victor/config/javaCode/MyBeanDefinitionRegistryPostProcessor.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/com/victor/config/shiro/ShiroConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
6161
filterChainDefinitionMap.put("/admin/logout", "logout");
6262

6363
// 过滤链
64-
filterChainDefinitionMap.put("/**", "user");
64+
//filterChainDefinitionMap.put("/**", "user");
6565
filterChainDefinitionMap.put("/css/**", "anon");
6666
filterChainDefinitionMap.put("/fonts/**", "anon");
6767
filterChainDefinitionMap.put("/img/**", "anon");

src/main/java/com/victor/springboot/javacode/service/JavaCodeService.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)