Skip to content

Commit d29cc98

Browse files
committed
初步实现一个使用Spring框架的Demo Web模块
1 parent 29cfa77 commit d29cc98

35 files changed

Lines changed: 1243 additions & 33 deletions

‎spring-demo/pom.xml‎

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pmd
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#### 本Demo使用Spring MVC 框架实现微信公众号后台管理功能,支持多公众号,欢迎帮忙维护添加新功能,或提供更好的实现。
2+
### 1. 配置
3+
复制/src/main/resources/wx-gzh1.properties.template 生成wx-gzh1.properties 文件,填写相关配置;
4+
复制/src/main/resources/wx-gzh2.properties.template 生成wx-gzh2.properties 文件,填写相关配置.
5+
6+
### 2. 使用maven运行demo程序
7+
mvn jetty:run
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.github.binarywang</groupId>
8+
<artifactId>weixin-java-parent</artifactId>
9+
<version>1.3.6-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>weixin-java-demo-with-spring</artifactId>
12+
<packaging>war</packaging>
13+
<name>WeiXin Java Tools - DemoWithSpring</name>
14+
<description>spring demo</description>
15+
<url>https://github.com/binarywang/weixin-java-tools</url>
16+
<properties>
17+
<spring.version>4.3.0.RELEASE</spring.version>
18+
<spring-security.version>4.1.0.RELEASE</spring-security.version>
19+
<guava.version>19.0</guava.version>
20+
<slf4j.version>1.7.2</slf4j.version>
21+
<aspectj.version>1.8.9</aspectj.version>
22+
<fastjson.version>1.2.6</fastjson.version>
23+
<commons-lang3.version>3.1</commons-lang3.version>
24+
<jetty-maven-plugin.version>9.3.10.v20160621</jetty-maven-plugin.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.github.binarywang</groupId>
30+
<artifactId>weixin-java-mp</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>com.alibaba</groupId>
36+
<artifactId>fastjson</artifactId>
37+
<version>${fastjson.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.apache.commons</groupId>
41+
<artifactId>commons-lang3</artifactId>
42+
<version>${commons-lang3.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.google.guava</groupId>
46+
<artifactId>guava</artifactId>
47+
<version>${guava.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-log4j12</artifactId>
52+
<version>${slf4j.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-webmvc</artifactId>
57+
<version>${spring.version}</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-web</artifactId>
62+
<version>${spring.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework.security</groupId>
66+
<artifactId>spring-security-core</artifactId>
67+
<version>${spring-security.version}</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.aspectj</groupId>
72+
<artifactId>aspectjrt</artifactId>
73+
<version>${aspectj.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.aspectj</groupId>
77+
<artifactId>aspectjweaver</artifactId>
78+
<version>${aspectj.version}</version>
79+
</dependency>
80+
</dependencies>
81+
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.eclipse.jetty</groupId>
86+
<artifactId>jetty-maven-plugin</artifactId>
87+
<version>${jetty-maven-plugin.version}</version>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.github.binarywang.demo.spring.aop;
2+
3+
import org.apache.commons.lang3.ArrayUtils;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.After;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Before;
8+
import org.aspectj.lang.annotation.Pointcut;
9+
import org.aspectj.lang.reflect.CodeSignature;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.security.core.Authentication;
13+
import org.springframework.security.core.context.SecurityContextHolder;
14+
import org.springframework.stereotype.Component;
15+
16+
/**
17+
*
18+
* @author Binary Wang
19+
*
20+
*/
21+
@Aspect
22+
@Component
23+
public class ControllerLogAspect {
24+
private Logger logger = LoggerFactory.getLogger(getClass());
25+
26+
@Pointcut("within(com.github.binarywang.demo.spring..*.controller..*)")
27+
public void inController() {
28+
}
29+
30+
@Pointcut("execution(public * com.github.binarywang.demo.spring..*.controller..*.*(..))")
31+
public void controller() {
32+
}
33+
34+
@Before("inController()")
35+
public void writeBeforeLog(JoinPoint jp) {
36+
this.debugInController(jp, "Start");
37+
}
38+
39+
@After("inController()")
40+
public void writeAfterLog(JoinPoint jp) {
41+
this.debugInController(jp, "End");
42+
}
43+
44+
private void debugInController(JoinPoint jp, String msg) {
45+
String userName = getLoginUserName();
46+
47+
this.logger.debug("\n【{}】{}.{}() {} ", userName,
48+
jp.getTarget()
49+
.getClass().getSimpleName(), jp.getSignature().getName(), msg);
50+
}
51+
52+
private static String getLoginUserName() {
53+
Authentication authentication = SecurityContextHolder.getContext()
54+
.getAuthentication();
55+
if (authentication != null) {
56+
return authentication.getName();
57+
}
58+
59+
return "Anonymous";
60+
}
61+
62+
@Before("controller()")
63+
public void writeParams(JoinPoint jp) {
64+
String[] names = ((CodeSignature) jp.getSignature())
65+
.getParameterNames();
66+
Object[] args = jp.getArgs();
67+
68+
if (ArrayUtils.isEmpty(names)) {
69+
return;
70+
}
71+
72+
StringBuilder sb = new StringBuilder("Arguments: ");
73+
for (int i = 0; i < names.length; i++) {
74+
sb.append(names[i] + " = " + args[i] + ",");
75+
}
76+
77+
debugInController(jp, sb.toString());
78+
}
79+
80+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.binarywang.demo.spring.builder;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.github.binarywang.demo.spring.service.BaseWxService;
7+
8+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
9+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
10+
11+
/**
12+
*
13+
* @author Binary Wang
14+
*
15+
*/
16+
public abstract class AbstractBuilder {
17+
protected final Logger logger = LoggerFactory.getLogger(getClass());
18+
19+
public abstract WxMpXmlOutMessage build(String content,
20+
WxMpXmlMessage wxMessage, BaseWxService service) ;
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.binarywang.demo.spring.builder;
2+
3+
import com.github.binarywang.demo.spring.service.BaseWxService;
4+
5+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
6+
import me.chanjar.weixin.mp.bean.WxMpXmlOutImageMessage;
7+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
8+
9+
/**
10+
*
11+
* @author Binary Wang
12+
*
13+
*/
14+
public class ImageBuilder extends AbstractBuilder {
15+
16+
@Override
17+
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
18+
BaseWxService service) {
19+
20+
WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId(content)
21+
.fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName())
22+
.build();
23+
24+
return m;
25+
}
26+
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.binarywang.demo.spring.builder;
2+
3+
import com.github.binarywang.demo.spring.service.BaseWxService;
4+
5+
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
6+
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
7+
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
8+
9+
/**
10+
*
11+
* @author Binary Wang
12+
*
13+
*/
14+
public class TextBuilder extends AbstractBuilder {
15+
16+
@Override
17+
public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
18+
BaseWxService service) {
19+
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
20+
.fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName())
21+
.build();
22+
return m;
23+
}
24+
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.binarywang.demo.spring.config;
2+
3+
/**
4+
* 公众号标识的枚举类
5+
* @author Binary Wang
6+
*
7+
*/
8+
public enum WxAccountEnum {
9+
GZH1(1, "公众号1"),
10+
GZH2(2, "公众号2");
11+
12+
private int pubid;
13+
private String name;
14+
15+
private WxAccountEnum(int pubid, String name) {
16+
this.name = name;
17+
this.pubid = pubid;
18+
}
19+
20+
public int getPubid() {
21+
return this.pubid;
22+
}
23+
24+
public String getName() {
25+
return this.name;
26+
}
27+
28+
public static int queryPubid(String wxCode) {
29+
return WxAccountEnum.valueOf(wxCode.toUpperCase()).getPubid();
30+
}
31+
32+
public static String queryWxCode(int pubid) {
33+
for (WxAccountEnum e : values()) {
34+
if (e.getPubid() == pubid) {
35+
return e.name().toLowerCase();
36+
}
37+
}
38+
39+
return null;
40+
}
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.binarywang.demo.spring.config;
2+
3+
/**
4+
* 微信配置的抽象实现类
5+
* @author Binary Wang
6+
*
7+
*/
8+
public abstract class WxConfig {
9+
public abstract String getToken();
10+
11+
public abstract String getAppid();
12+
13+
public abstract String getAppsecret();
14+
15+
public abstract WxAccountEnum getWxAccountEnum();
16+
17+
public int getPubId() {
18+
return getWxAccountEnum().getPubid();
19+
}
20+
21+
}

0 commit comments

Comments
 (0)