gpt4 book ai didi

java - Spring Autowiring 提供 NPE。不手动创建bean

转载 作者:行者123 更新时间:2023-11-30 05:34:57 26 4
gpt4 key购买 nike

我的 Spring MVC 程序中的服务类在运行应用程序时仍然为空。这两个类都是 @Service 并在 Controller 类中具有 @Autowired 但仍然为 null。我已经浏览了几天,我发现的只有两个原因,(我相信)这两个原因都不适合我的情况。

Spring boot 应用程序尝试创建一个不和谐的机器人, Autowiring 在 Controller 或 Junit 测试中不起作用(执行时出现 NPE,调试时变量显示 null)。

驱动程序类别:

package com.deth;

//imports
@SpringBootApplication
public class DethBotApplication {


private static Logger logger = Logger.getLogger(DethBotApplication.class);

@Autowired
private static BotCommandListener botListener;

public static void main(String[] args) {
SpringApplication.run(DethBotApplication.class, args);


try {
JDA jda = new JDABuilder(AccountType.BOT)
.setToken(TOKEN)
//.addEventListener(command controller)
.addEventListener(botListener)
.build(); //starts listening in discord server.

相关 Controller 代码:

package com.deth.controller;
//imports
@Component
public class BotCommandListener extends ListenerAdapter {


private static Logger logger = Logger.getLogger(BotCommandListener.class);

@Autowired
@Qualifier("raidAdminService")
private RaidAdminService raidAdminService;

@Autowired
private RaidRosterServiceImpl raidRosterService;

@Autowired
private RaidAttendanceService raidAttendanceService;

@Override
public void onMessageReceived(MessageReceivedEvent event) {
JDA jda = event.getJDA();

String msg = event.getMessage().getContentDisplay();
if(msg.startsWith("!")) {
String command = "";
if(!msg.contains(" ")) {
command = msg;
} else {
command = msg.subSequence(0, msg.indexOf(" ")).toString();
logger.trace("possible command: " + command);
}
try {
switch (command) {
//raid leader commands
case "!open":
raidAdminService.createRaid(event); //NPE here
logger.trace("!open detected");
break;

raidAdminService:

package com.deth.service;
//imports
@Service("raidAdminService")
public class RaidAdminServiceImpl extends CommandInfoService implements RaidAdminService {

String intRegex = "[0-9]+";

@Override
public void createRaid(MessageReceivedEvent event) {
// TODO Auto-generated method stub

包结构:

  • com
    • 死亡
      • DethBot应用程序
      • Controller
        • DethBotCommandListner
      • 服务
        • RaidAdminService(接口(interface))
        • RaidAdminServiceImpl(类)....

当程序启动并运行时,在discord服务器中发送“!open”,正确命中switch语句并尝试调用createRaid方法,但RaidAdminService没有 Autowiring ,因此它在null上调用该方法。

最佳答案

我认为问题出在您的 DethBotApplication 类中。你不能在那里自动接线。主类需要先执行。之后该应用程序将查找 @Componet、@Service、@Controller... 注释。下面的代码可能会解决您的问题。

package com.deth;

//imports
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DethBotApplication extends SpringBootServletInitializer {

private static Logger logger = Logger.getLogger(DethBotApplication.class);

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DethBotApplication.class, args);
BotCommandListener botListener = context.getBean(BotCommandListener.class);
try {
JDA jda = new JDABuilder(AccountType.BOT)
.setToken(TOKEN)
//.addEventListener(command controller)
.addEventListener(botListener)
.build(); //starts listening in discord server.

关于java - Spring Autowiring 提供 NPE。不手动创建bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56843329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com