feat(0): [java]-[wcferry-mvn]-控制层接口测试
This commit is contained in:
parent
3c39723468
commit
12029ebba0
17
clients/java/wcferry-mvn/src/main/java/com/iamteer/BussinessContext.java
vendored
Normal file
17
clients/java/wcferry-mvn/src/main/java/com/iamteer/BussinessContext.java
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.iamteer;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
@Order(100)
|
||||||
|
public class BussinessContext {
|
||||||
|
|
||||||
|
private Client client;
|
||||||
|
|
||||||
|
}
|
@ -1,11 +1,13 @@
|
|||||||
package com.iamteer.controller;
|
package com.iamteer.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.iamteer.entity.TResponse;
|
import com.iamteer.entity.TResponse;
|
||||||
import com.iamteer.enums.ResponseCodeEnum;
|
import com.iamteer.enums.ResponseCodeEnum;
|
||||||
|
import com.iamteer.service.TestService;
|
||||||
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
@ -15,10 +17,18 @@ import io.swagger.annotations.ApiOperation;
|
|||||||
@Api(tags = "测试-接口")
|
@Api(tags = "测试-接口")
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
@ApiOperation(value = "测试", notes = "index")
|
private TestService testService;
|
||||||
@PostMapping(value = "/index")
|
|
||||||
public TResponse<Object> index() {
|
@Autowired
|
||||||
return TResponse.ok(ResponseCodeEnum.SUCCESS, "");
|
public void setTestService(TestService testService) {
|
||||||
|
this.testService = testService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "测试", notes = "login")
|
||||||
|
@PostMapping(value = "/login")
|
||||||
|
public TResponse<Object> login() {
|
||||||
|
Boolean flag = testService.isLogin();
|
||||||
|
return TResponse.ok(ResponseCodeEnum.SUCCESS, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@ import javax.annotation.Resource;
|
|||||||
|
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.iamteer.BussinessContext;
|
||||||
import com.iamteer.Client;
|
import com.iamteer.Client;
|
||||||
import com.iamteer.config.WcferryProperties;
|
import com.iamteer.config.WcferryProperties;
|
||||||
|
|
||||||
@ -19,11 +21,19 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
@Order(101)
|
||||||
public class WechatRunner implements ApplicationRunner {
|
public class WechatRunner implements ApplicationRunner {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private WcferryProperties properties;
|
private WcferryProperties properties;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private BussinessContext bussinessContext;
|
||||||
|
|
||||||
|
public WechatRunner(BussinessContext bussinessContext) {
|
||||||
|
this.bussinessContext = bussinessContext;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
System.out.println(">>>服务启动第一个开始执行的任务<<<<");
|
System.out.println(">>>服务启动第一个开始执行的任务<<<<");
|
||||||
@ -38,8 +48,8 @@ public class WechatRunner implements ApplicationRunner {
|
|||||||
// 本地启动 RPC
|
// 本地启动 RPC
|
||||||
// Client client = new Client(); // 默认 10086 端口
|
// Client client = new Client(); // 默认 10086 端口
|
||||||
// Client client = new Client(10088,true); // 也可以指定端口
|
// Client client = new Client(10088,true); // 也可以指定端口
|
||||||
|
Client client = new Client(properties.getSocketPort(), properties.getDllPath());
|
||||||
Client client = new Client(properties.getSocketPort(), properties.getDllPath()); // 默认 10086 端口
|
bussinessContext.setClient(client); // 默认 10086 端口
|
||||||
|
|
||||||
// 是否已登录
|
// 是否已登录
|
||||||
// log.info("isLogin: {}", client.isLogin());
|
// log.info("isLogin: {}", client.isLogin());
|
||||||
|
13
clients/java/wcferry-mvn/src/main/java/com/iamteer/service/TestService.java
vendored
Normal file
13
clients/java/wcferry-mvn/src/main/java/com/iamteer/service/TestService.java
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.iamteer.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务接口-注册
|
||||||
|
*
|
||||||
|
* @author chandler
|
||||||
|
* @date 2024-09-29 20:58
|
||||||
|
*/
|
||||||
|
public interface TestService {
|
||||||
|
|
||||||
|
Boolean isLogin();
|
||||||
|
|
||||||
|
}
|
35
clients/java/wcferry-mvn/src/main/java/com/iamteer/service/impl/TestServiceImpl.java
vendored
Normal file
35
clients/java/wcferry-mvn/src/main/java/com/iamteer/service/impl/TestServiceImpl.java
vendored
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.iamteer.service.impl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.iamteer.BussinessContext;
|
||||||
|
import com.iamteer.service.TestService;
|
||||||
|
import com.iamteer.utils.SpringContextHolderUtil;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务实现层-注册
|
||||||
|
*
|
||||||
|
* @author chandler
|
||||||
|
* @date 2024-09-29 20:58
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class TestServiceImpl implements TestService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean isLogin() {
|
||||||
|
|
||||||
|
BussinessContext bussinessContext = SpringContextHolderUtil.getBean(BussinessContext.class);
|
||||||
|
|
||||||
|
boolean flag = bussinessContext.getClient().isLogin();
|
||||||
|
log.info("flag:{}", flag);
|
||||||
|
List<String> list = bussinessContext.getClient().getDbNames();
|
||||||
|
log.info("list:{}", list);
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
173
clients/java/wcferry-mvn/src/main/java/com/iamteer/utils/SpringContextHolderUtil.java
vendored
Normal file
173
clients/java/wcferry-mvn/src/main/java/com/iamteer/utils/SpringContextHolderUtil.java
vendored
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
package com.iamteer.utils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring 工具类
|
||||||
|
*
|
||||||
|
* @author chandler
|
||||||
|
* @date 2023-03-30 11:05:49
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@Lazy(false)
|
||||||
|
public class SpringContextHolderUtil implements ApplicationContextAware, DisposableBean {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上下文对象实例
|
||||||
|
*/
|
||||||
|
private static ApplicationContext applicationContext = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取applicationContext-取得存储在静态变量中的ApplicationContext.
|
||||||
|
*/
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
checkApplicationContext();
|
||||||
|
return applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||||
|
SpringContextHolderUtil.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过name获取Bean-从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> T getBean(String name) {
|
||||||
|
checkApplicationContext();
|
||||||
|
if (applicationContext.containsBean(name)) {
|
||||||
|
return (T)applicationContext.getBean(name);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过class获取Bean-从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||||
|
*/
|
||||||
|
public static <T> T getBean(Class<T> requiredType) {
|
||||||
|
checkApplicationContext();
|
||||||
|
return applicationContext.getBean(requiredType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过name,以及Clazz返回指定的Bean
|
||||||
|
*/
|
||||||
|
public static <T> T getBean(String name, Class<T> requiredType) {
|
||||||
|
return getApplicationContext().getBean(name, requiredType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||||
|
*/
|
||||||
|
public static <T> T getBeanOfType(Class<T> clazz) {
|
||||||
|
checkApplicationContext();
|
||||||
|
return (T)applicationContext.getBeansOfType(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除SpringContextHolder中的ApplicationContext为Null.
|
||||||
|
*/
|
||||||
|
public static void clearHolder() {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
|
||||||
|
}
|
||||||
|
applicationContext = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发布事件
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
*/
|
||||||
|
public static void publishEvent(ApplicationEvent event) {
|
||||||
|
if (applicationContext == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
applicationContext.publishEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public void destroy() {
|
||||||
|
SpringContextHolderUtil.clearHolder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void registerSingletonBean(String beanName, Class clzz, Map<String, Object> original) {
|
||||||
|
checkApplicationContext();
|
||||||
|
DefaultListableBeanFactory beanFactory =
|
||||||
|
(DefaultListableBeanFactory)SpringContextHolderUtil.getApplicationContext().getAutowireCapableBeanFactory();
|
||||||
|
if (beanFactory.containsBean(beanName)) {
|
||||||
|
removeBean(beanName);
|
||||||
|
}
|
||||||
|
GenericBeanDefinition definition = new GenericBeanDefinition();
|
||||||
|
// 类class
|
||||||
|
definition.setBeanClass(clzz);
|
||||||
|
// 属性赋值
|
||||||
|
definition.setPropertyValues(new MutablePropertyValues(original));
|
||||||
|
// 注册到spring上下文
|
||||||
|
beanFactory.registerBeanDefinition(beanName, definition);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void registerSingletonBean(String beanName, Object obj, Map<String, Object> original) {
|
||||||
|
checkApplicationContext();
|
||||||
|
DefaultListableBeanFactory beanFactory =
|
||||||
|
(DefaultListableBeanFactory)SpringContextHolderUtil.getApplicationContext().getAutowireCapableBeanFactory();
|
||||||
|
if (beanFactory.containsBean(beanName)) {
|
||||||
|
removeBean(beanName);
|
||||||
|
}
|
||||||
|
GenericBeanDefinition definition = new GenericBeanDefinition();
|
||||||
|
// 类class
|
||||||
|
definition.setBeanClass(obj.getClass());
|
||||||
|
// 属性赋值
|
||||||
|
definition.setPropertyValues(new MutablePropertyValues(original));
|
||||||
|
// 注册到spring上下文
|
||||||
|
beanFactory.registerBeanDefinition(beanName, definition);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized void registerSingletonBean(String beanName, Object obj) {
|
||||||
|
registerSingletonBean(beanName, obj, JSONObject.parseObject(JSON.toJSONString(obj), Map.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除spring中管理的bean
|
||||||
|
*
|
||||||
|
* @param beanName
|
||||||
|
*/
|
||||||
|
public static void removeBean(String beanName) {
|
||||||
|
ApplicationContext ctx = SpringContextHolderUtil.getApplicationContext();
|
||||||
|
DefaultListableBeanFactory acf = (DefaultListableBeanFactory)ctx.getAutowireCapableBeanFactory();
|
||||||
|
if (acf.containsBean(beanName)) {
|
||||||
|
acf.removeBeanDefinition(beanName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkApplicationContext() {
|
||||||
|
if (applicationContext == null) {
|
||||||
|
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user