mirror of
https://github.com/wxd-gaming/test-all.git
synced 2026-07-02 17:44:28 +08:00
【新增】测试。调试代码
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
package scripts;
|
||||
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletRegistration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
@Configuration
|
||||
public class MyWebAppInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) {
|
||||
System.out.println("------------111111--------");
|
||||
// 创建DispatcherServlet的实例
|
||||
DispatcherServlet dispatcherServlet = new DispatcherServlet();
|
||||
|
||||
// 设置servlet映射
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", dispatcherServlet);
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping("/");
|
||||
}
|
||||
}
|
||||
// package scripts;
|
||||
//
|
||||
// import jakarta.servlet.ServletContext;
|
||||
// import jakarta.servlet.ServletRegistration;
|
||||
// import org.springframework.context.annotation.Configuration;
|
||||
// import org.springframework.web.WebApplicationInitializer;
|
||||
// import org.springframework.web.servlet.DispatcherServlet;
|
||||
//
|
||||
// @Configuration
|
||||
// public class MyWebAppInitializer implements WebApplicationInitializer {
|
||||
//
|
||||
// @Override
|
||||
// public void onStartup(ServletContext servletContext) {
|
||||
// System.out.println("------------111111--------");
|
||||
// // 创建DispatcherServlet的实例
|
||||
// DispatcherServlet dispatcherServlet = new DispatcherServlet();
|
||||
//
|
||||
// // 设置servlet映射
|
||||
// ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", dispatcherServlet);
|
||||
// registration.setLoadOnStartup(1);
|
||||
// registration.addMapping("/");
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package scripts;
|
||||
|
||||
import jakarta.servlet.ServletRegistration;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
@Configuration
|
||||
public class ScriptConfig {
|
||||
|
||||
|
||||
|
||||
//@Bean
|
||||
//public DispatcherServlet resourceServlet(ApplicationContext applicationContext) {
|
||||
// public DispatcherServlet resourceServlet(ApplicationContext applicationContext) {
|
||||
// System.out.println("init resourceServlet");
|
||||
// System.out.println("------------111111--------");
|
||||
// // 创建DispatcherServlet的实例
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package scripts;
|
||||
|
||||
import org.example.HelloController;
|
||||
import org.example.SpringAnnotationUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.wxd.boot.agent.loader.ClassDirLoader;
|
||||
import org.wxd.boot.agent.system.ReflectContext;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class ScriptMain {
|
||||
|
||||
static AnnotationConfigApplicationContext childContainer;
|
||||
|
||||
public static void scriptInit(ConfigurableApplicationContext context, ClassDirLoader classDirLoader) {
|
||||
//子容器
|
||||
childContainer = new AnnotationConfigApplicationContext();
|
||||
childContainer.setParent(context);
|
||||
childContainer.setClassLoader(classDirLoader);
|
||||
|
||||
childContainer.scan(ScriptMain.class.getPackageName());
|
||||
childContainer.refresh();
|
||||
childContainer.start();
|
||||
// scriptInit0(context, classDirLoader);
|
||||
|
||||
//从子容器中获取父容器中的Bean
|
||||
HelloController parentService = childContainer.getBean(HelloController.class);
|
||||
LoggerFactory.getLogger(ScriptMain.class).info("{}", parentService);
|
||||
//getBeansOfType无法获取到父容器中的Bean
|
||||
childContainer.getBeansOfType(TestApi.class).forEach((k, v) -> {
|
||||
LoggerFactory.getLogger(ScriptMain.class).info("{}", v);
|
||||
});
|
||||
System.out.println("http://127.0.0.1:18081/test");
|
||||
}
|
||||
|
||||
public static void scriptInit0(ConfigurableApplicationContext context, ClassDirLoader classDirLoader) {
|
||||
|
||||
Set<Class<?>> initBeanClass = new HashSet<>();
|
||||
ReflectContext reflectContext = ReflectContext.Builder.of(classDirLoader, ScriptMain.class.getPackageName()).build();
|
||||
// 获取beanFactory
|
||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
|
||||
reflectContext.classStream().forEach(clazz -> {
|
||||
final String className = clazz.getName();
|
||||
// 2. 将有@spring注解的类交给spring管理
|
||||
// 2.1 判断是否注入spring
|
||||
boolean flag = SpringAnnotationUtils.hasSpringAnnotation(clazz);
|
||||
if (flag) {
|
||||
// 2.2交给spring管理
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
|
||||
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
|
||||
// 此处beanName使用全路径名是为了防止beanName重复
|
||||
String packageName = className.substring(0, className.lastIndexOf(".") + 1);
|
||||
String beanName = className.substring(className.lastIndexOf(".") + 1);
|
||||
beanName = packageName + beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
|
||||
// 2.3注册到spring的beanFactory中
|
||||
beanFactory.registerBeanDefinition(beanName, beanDefinition);
|
||||
// 2.4允许注入和反向注入
|
||||
beanFactory.autowireBean(clazz);
|
||||
beanFactory.initializeBean(clazz, beanName);
|
||||
/*if(Arrays.stream(clazz.getInterfaces()).collect(Collectors.toSet()).contains(InitializingBean.class)){
|
||||
initBeanClass.add(clazz);
|
||||
}*/
|
||||
initBeanClass.add(clazz);
|
||||
System.out.println(clazz.getName());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package scripts;
|
||||
|
||||
import org.example.HelloController;
|
||||
import org.example.Main;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -11,8 +12,9 @@ public class TestApi {
|
||||
@Autowired HelloController helloController;
|
||||
|
||||
@RequestMapping(value = "/test")
|
||||
public String test() {
|
||||
return "ok";
|
||||
public String test() throws Exception {
|
||||
Main.reload();
|
||||
return "ok4";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
spring-test/scripts/src/main/java/scripts/TestApi2.java
Normal file
22
spring-test/scripts/src/main/java/scripts/TestApi2.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package scripts;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.example.HelloController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class TestApi2 {
|
||||
|
||||
@Autowired HelloController helloController;
|
||||
@Autowired TestApi testApi;
|
||||
|
||||
@RequestMapping(value = "/test2")
|
||||
public String test2() throws Exception {
|
||||
log.debug("{}", testApi);
|
||||
return "test2";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user