use Spring Context
This commit is contained in:
@@ -31,6 +31,8 @@ dependencies {
|
|||||||
exclude group: 'commons-lang', module: 'commons-lang'
|
exclude group: 'commons-lang', module: 'commons-lang'
|
||||||
exclude group: 'com.google.guava', module: 'guava'
|
exclude group: 'com.google.guava', module: 'guava'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
implementation 'org.springframework:spring-context:5.2.5.RELEASE'
|
||||||
}
|
}
|
||||||
|
|
||||||
processResources {
|
processResources {
|
||||||
|
|||||||
@@ -1,15 +1,31 @@
|
|||||||
package ru.dmitriymx.plugin;
|
package ru.dmitriymx.plugin;
|
||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import ru.dmitriymx.plugin.command.BanCommand;
|
import ru.dmitriymx.plugin.command.BanCommand;
|
||||||
import ru.dmitriymx.plugin.command.UnbanCommand;
|
import ru.dmitriymx.plugin.command.UnbanCommand;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class BanHammerPlugin extends JavaPlugin {
|
public class BanHammerPlugin extends JavaPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
this.getCommand("ban").setExecutor(new BanCommand(this.getLogger()));
|
ApplicationContext context = createSpringContext();
|
||||||
this.getCommand("unban").setExecutor(new UnbanCommand(this.getLogger()));
|
|
||||||
|
this.getCommand("ban").setExecutor(context.getBean(BanCommand.class));
|
||||||
|
this.getCommand("unban").setExecutor(context.getBean(UnbanCommand.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApplicationContext createSpringContext() {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
context.setClassLoader(this.getClassLoader());
|
||||||
|
context.registerBean("bukkitLogger", Logger.class, this::getLogger);
|
||||||
|
context.register(SpringConfig.class);
|
||||||
|
context.refresh();
|
||||||
|
|
||||||
|
return context;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/main/java/ru/dmitriymx/plugin/SpringConfig.java
Normal file
10
src/main/java/ru/dmitriymx/plugin/SpringConfig.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.dmitriymx.plugin;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan("ru.dmitriymx.plugin")
|
||||||
|
public class SpringConfig {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,14 +3,20 @@ package ru.dmitriymx.plugin.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class BanCommand implements CommandExecutor {
|
public class BanCommand implements CommandExecutor {
|
||||||
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
public BanCommand(Logger logger) {
|
@Autowired
|
||||||
|
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||||
|
public BanCommand(@Qualifier("bukkitLogger") Logger logger) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,20 @@ package ru.dmitriymx.plugin.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class UnbanCommand implements CommandExecutor {
|
public class UnbanCommand implements CommandExecutor {
|
||||||
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
public UnbanCommand(Logger logger) {
|
@Autowired
|
||||||
|
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
|
||||||
|
public UnbanCommand(@Qualifier("bukkitLogger") Logger logger) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user