Skip to content

Commit

Permalink
Merge pull request #18 from AwakenCN/springboot
Browse files Browse the repository at this point in the history
修改1.1.3版本bean注入失败问题
  • Loading branch information
UncleCatMySelf authored Aug 20, 2019
2 parents fe9f1ca + 15b9279 commit 5580bee
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Front-End-Testing/chat90.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
window.WebSocket = window.MozWebSocket;
}
if (window.WebSocket) {
socket = new WebSocket("ws://192.168.56.1:8090/ws");
socket = new WebSocket("ws://192.168.1.121:8090/ws");
socket.onmessage = function(event) {
var ta = document.getElementById('responseText');
ta.value = ta.value + '\n' + event.data
Expand Down
42 changes: 36 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.github.UncleCatMySelf</groupId>
<artifactId>InChat</artifactId>
<version>1.1.3</version>
Expand Down Expand Up @@ -75,11 +80,11 @@
<artifactId>jedis</artifactId>
<version>${redis.clients.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-log4j12</artifactId>-->
<!--<version>${slf4j.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand All @@ -100,6 +105,31 @@
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
<!-- springboot 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!-- 常规测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/github/unclecatmyself/application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

import com.github.unclecatmyself.auto.ConfigFactory;
import com.github.unclecatmyself.auto.InitServer;
import com.github.unclecatmyself.users.DataBaseServiceImpl;
import com.github.unclecatmyself.users.FromServerServiceImpl;
import com.github.unclecatmyself.users.MyInit;
import com.github.unclecatmyself.users.VerifyServiceImpl;
import com.github.unclecatmyself.users.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Create by UncleCatMySelf in 22:49 2019\1\4 0004
*/
@SpringBootApplication
public class application {

public static void main(String[] args) {
SpringApplication.run(application.class, args);
ConfigFactory.initNetty = new MyInit();
ConfigFactory.inChatVerifyService = new VerifyServiceImpl();
ConfigFactory.inChatToDataBaseService = new DataBaseServiceImpl();
ConfigFactory.fromServerService = FromServerServiceImpl.TYPE2;
ConfigFactory.textData = new UserTextData();
// ConfigFactory.RedisIP = "192.168.0.101";
// ConfigFactory.RedisIP = "192.168.192.132";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.github.unclecatmyself.bootstrap.data.InChatToDataBaseService;
import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService;
import com.github.unclecatmyself.common.bean.InitNetty;
import com.github.unclecatmyself.task.TextData;

/**
* 默认配置工厂
* Created by MySelf on 2018/12/21.
*/
public class ConfigFactory {

/** Redis的ip地址,这你不会不知道吧? */
/** Redis的ip地址 */
public static String RedisIP;

/** 用户校验伪接口 */
Expand All @@ -26,4 +27,6 @@ public class ConfigFactory {
/** InChat项目配置 */
public static InitNetty initNetty;

public static TextData textData;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.unclecatmyself.common.ssl.SecureSocketSslContextFactory;
import com.github.unclecatmyself.common.utils.SslUtil;
import com.github.unclecatmyself.task.DataAsynchronousTask;
import com.github.unclecatmyself.users.UserTextData;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
Expand Down Expand Up @@ -60,7 +61,7 @@ protected void initHandler(ChannelPipeline channelPipeline, InitNetty serverBea
}
intProtocolHandler(channelPipeline,serverBean);
channelPipeline.addLast(new IdleStateHandler(serverBean.getHeart(),0,0));
channelPipeline.addLast(new DefaultHandler(new HandlerServiceImpl(new DataAsynchronousTask(ConfigFactory.inChatToDataBaseService),ConfigFactory.inChatVerifyService)));
channelPipeline.addLast(new DefaultHandler(new HandlerServiceImpl(new DataAsynchronousTask(ConfigFactory.inChatToDataBaseService),ConfigFactory.inChatVerifyService,ConfigFactory.textData)));
}

private void intProtocolHandler(ChannelPipeline channelPipeline,InitNetty serverBean){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import com.github.unclecatmyself.common.bean.SendInChat;
import com.github.unclecatmyself.common.bean.vo.SendServerVO;
import com.github.unclecatmyself.common.constant.Constans;
import com.github.unclecatmyself.task.TextData;
import com.google.gson.Gson;
import com.github.unclecatmyself.bootstrap.channel.ws.WsChannelService;
import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService;
import com.github.unclecatmyself.task.DataAsynchronousTask;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.CharsetUtil;
Expand All @@ -39,11 +42,14 @@ public class HandlerServiceImpl extends HandlerService {

private final WsChannelService websocketChannelService = new WebSocketChannelService();

private final DataAsynchronousTask dataAsynchronousTask;
private DataAsynchronousTask dataAsynchronousTask;

public HandlerServiceImpl(DataAsynchronousTask dataAsynchronousTask,InChatVerifyService inChatVerifyService) {
private TextData textData;

public HandlerServiceImpl(DataAsynchronousTask dataAsynchronousTask,InChatVerifyService inChatVerifyService,TextData textData) {
this.dataAsynchronousTask = dataAsynchronousTask;
this.inChatVerifyService = inChatVerifyService;
this.textData = textData;
}


Expand Down Expand Up @@ -86,12 +92,25 @@ public boolean login(Channel channel, Map<String,Object> maps) {
public void sendMeText(Channel channel, Map<String,Object> maps) {
Gson gson = new Gson();
channel.writeAndFlush(new TextWebSocketFrame(
gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE)))));
try {
dataAsynchronousTask.writeData(maps);
} catch (Exception e) {
e.printStackTrace();
}
gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE))))).addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()){
dataAsynchronousTask.writeData(maps);
textData.writeData(maps);
} else {
future.cause().printStackTrace();
future.channel().close();
}
}
}
);
// try {
// dataAsynchronousTask.writeData(maps);
// } catch (Exception e) {
// e.printStackTrace();
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) {
httpHandlerService.notFindUri(channel);
break;
default:
System.out.println("为匹配"+msg);
System.out.println("未匹配"+msg);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.unclecatmyself.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* Created by MySelf on 2019/8/20.
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {

/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}

/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}

/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}

/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}

}
12 changes: 12 additions & 0 deletions src/main/java/com/github/unclecatmyself/task/TextData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.unclecatmyself.task;

import java.util.Map;

/**
* Created by MySelf on 2019/8/20.
*/
public abstract class TextData {

public abstract void writeData(Map<String,Object> maps);

}
30 changes: 30 additions & 0 deletions src/main/java/com/github/unclecatmyself/users/UserTextData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.unclecatmyself.users;

import com.github.unclecatmyself.common.utils.SpringContextUtils;
import com.github.unclecatmyself.task.TextData;
import com.github.unclecatmyself.users.pojo.Test;
import com.github.unclecatmyself.users.repository.TestRepository;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* Created by MySelf on 2019/8/20.
*/
public class UserTextData extends TextData {

private TestRepository repository = (TestRepository) SpringContextUtils.getBean(TestRepository.class);

@Override
public void writeData(Map<String, Object> maps) {
Test test = new Test();
test.setId(1);
test.setMsg("1111");
repository.save(test);
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/github/unclecatmyself/users/pojo/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.unclecatmyself.users.pojo;

import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

/**
* @ClassName TestUser
* @Description 框架测试用户,测试密码加密,不允许生产使用
* @Author MySelf
* @Date 2019/7/27 11:06
* @Version 1.0
**/
@Entity
@Data
@DynamicUpdate
public class Test implements Serializable {

/**id*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

/**信息*/
private String msg;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.unclecatmyself.users.repository;

import com.github.unclecatmyself.users.pojo.Test;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* @InterfaceName TestUserRepository
* @Description Jpa映射层
* @Author MySelf
* @Date 2019/7/27 11:05
* @Version 1.0
**/
public interface TestRepository extends JpaRepository<Test,Integer> {

}
8 changes: 8 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.192.133:3306/inchat
username: root
password: password
jpa:
show-sql: true
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
profiles:
active: dev
File renamed without changes.

0 comments on commit 5580bee

Please sign in to comment.