[go: nahoru, domu]

Skip to content

Commit

Permalink
代码修改 => 1.去除SpringGetter类,将ApplicationContext放入Vessel中管理,修改Vessel相关部分…
Browse files Browse the repository at this point in the history
…代码 2.默认不扫描Common包
  • Loading branch information
Joezeo committed Aug 1, 2021
1 parent 616e1d2 commit 66ad35a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
18 changes: 13 additions & 5 deletions common/src/main/java/com/toocol/common/BasisInitAfter.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package com.toocol.common;

import com.toocol.common.utils.SpringGetter;
import com.toocol.common.vessel.AbstractVessel;
import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* @author Joezeo
* @date 2021/8/1 0:28
*/
@Component
public class BasisInitAfter implements CommandLineRunner {
@AllArgsConstructor
public abstract class BasisInitAfter implements CommandLineRunner {

public final AbstractVessel vessel;

@Override
public void run(String... args) throws Exception {
SpringGetter.getBean(AbstractVessel.class).ifPresent(AbstractVessel::init);
vessel.init();
init();
}

/**
* initial the server
*/
public abstract void init();
}
35 changes: 0 additions & 35 deletions common/src/main/java/com/toocol/common/utils/SpringGetter.java

This file was deleted.

34 changes: 30 additions & 4 deletions common/src/main/java/com/toocol/common/vessel/AbstractVessel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.toocol.common.vessel;

import com.toocol.common.utils.SpringGetter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand All @@ -13,7 +17,14 @@
* @author Joezeo
* @date 2021/7/31 23:41
*/
public abstract class AbstractVessel {
@Slf4j
public abstract class AbstractVessel implements ApplicationContextAware {

public static ApplicationContext applicationContext;

public static AbstractVessel get() {
return applicationContext.getBean(AbstractVessel.class);
}

public void init() {
Class<?> superClass = this.getClass();
Expand All @@ -25,14 +36,29 @@ public void init() {

fields.forEach(field -> {
try {
if (field.getType().equals(ApplicationContext.class)) {
return;
}
if (field.getType().equals(Logger.class)) {
return;
}

field.setAccessible(true);
field.set(this, SpringGetter.getBean(field.getType()).orElse(null));
field.set(this, applicationContext.getBean(field.getType()));
} catch (BeansException e) {
System.out.println("Bean: " + field.getType().getName() + " not exist, skip");
log.warn("Bean: {} not exist, skip", field.getType().getName());
} catch (IllegalAccessException e) {
e.printStackTrace();
System.out.println("inject field:" + field.getName() + " into vessel failed, stop the server.");
System.out.println();
log.error("inject field:{} into vessel failed, stop the server.", field.getName());
System.exit(-1);
}
});
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
AbstractVessel.applicationContext = applicationContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.stereotype.Component;

/**
* every son server should has its own Vessel and annotated {@link org.springframework.context.annotation.Primary},
* every sub server should has its own Vessel and annotated {@link org.springframework.context.annotation.Primary},
* if it doesnt, we just use this DefaultVessel
*
* @author Joezeo
Expand All @@ -13,6 +13,11 @@
@Component
public class DefaultVessel extends AbstractVessel{

/**
* this field default is null,
* if the sub project need use akka system, please use annotation {@link org.springframework.context.annotation.Import} to
* import the {@link com.toocol.common.akka.AkkaSystemConfig} manual.
*/
public ActorSystem actorSystem;

}

0 comments on commit 66ad35a

Please sign in to comment.