Spring Boot@Autowired为null,但未使用new启动



我有一个实体报告服务Spring Boot框架,可以简单地在JFreeChart中可视化数据。承认这是一个常见的问题,但我花了太多时间阅读和尝试大线程的解决方案为什么我的Spring@Autowired字段为空?寻求帮助可能更有效。我还在学习Spring Boot,所以我一定没有完全理解解决方案,或者忽略了一些关键点。

回购:

package com.tool.logsview.repository;
import ...
@Repository
@ComponentScan(basePackages = {"com.tool.logsview"})
public interface LogRepository extends JpaRepository<Log, Long> {
@Override
List<Log> findAll();
List<Log> findByDept(String dept);
}

服务:

package com.tool.logsview.service;
import ...
@Service
public class LogServiceImp implements LogService{
@Autowired
private LogRepository logRepository;

@Override
public List<Log> findByDept(String dept) {
return logRepository.findByDept(dept);
}
}

我与JFreeChart 集成的控制器

package com.tool.logsview.controller;
import ...
@Component
@Configurable
@ComponentScan(basePackages = {"com.tool.logsview"})
public class GanttChartWithData extends JFrame {
@Autowired
LogServiceImp logServiceImp; // <- null

public GanttChartWithData(final String title) {
super(title);
// ...
}
private @NotNull IntervalCategoryDataset createDataset() {
List<Log> demoDept = logServiceImp.findByDept("Demo"); // <- npe, the only use of logServiceImp in this class
// ...
}
public static void main(final String[] args) {
// ... JFreeChart
}
}

我试过了:

  • 使用注释而不使用new
  • 为类GanttChartWithData添加注释@Component/@Service/@Controller
  • 解决方案更改为@Service,然后使用构造注入
package com.tool.logsview.controller;
import ...
@Service
public class GanttChartWithData extends JFrame {
private static
LogRepository logRepository;
@Autowired
public GanttChartWithData(final String title, LogRepository logRepository) {
super(title);
// ...
this.logRepository = logRepository;
}
// Creates a sample dataset for a Gantt chart.
private @NotNull IntervalCategoryDataset createDataset() {
// ...
List<Log> demoDept = logRepository.findByDeptAndUsernameOrderByUsernameAsc("Demo", "personA");
}
public static void main(final String[] args) {
final GanttChartWithData demo = new GanttChartWithData("Gantt Chart Demo", logRepository);
}
}

注意,这完全可以工作,但我不想给Application带来垃圾代码

@SpringBootApplication
public class LogsviewApplication {
@Autowired
private LogRepository logRepository; // <- not null
...}

在这一点上,我完全感到困惑。甚至到了我不知道为什么我们应该回购的地步->服务->控制器,而我直接使用Repo,它运行良好(在应用程序中,我使用了Repo而不是ServiceImplementation(。

任何启示都值得赞赏!

更新

  • 我按照本指南使用应用程序上下文:
package com.tool.logsview.misc;
import ...
@Controller
public class Dummy {
@RequestMapping("/dummy")
@ResponseBody
public List<Log> dummy() {
LogServiceImp logServiceImp = ApplicationContextHolder.getContext().getBean(LogServiceImp.class);
return logServiceImp.fetchLogList();
}
}

这证明了ApplicationContext是有效的,但我不需要web控制器。我想调用logServiceImp进行业务服务。因此,尝试将其更改为

package com.tool.logsview.misc;
import ...
@Service
public class Dummy {
public Dummy() { }
@Autowired
static LogServiceImp logServiceImp1; // null - the same problem
public static void main(String[] args) {
LogServiceImp logServiceImp = ApplicationContextHolder.getContext().getBean(LogServiceImp.class); // application context but still null
System.out.println(logServiceImp.fetchLogList());
System.out.println(logServiceImp1.fetchLogList());
}
}

很可能误解了整个国际奥委会的概念?

上下文(如果有帮助的话(:起初,它旨在在浏览器上可视化数据,因此使用Controller结构,但我切换到JFreeChart java lib来与Swing一起运行,所以我不需要@Controller和@RequestMapping。正如@Georgy Gobozov所指出的,我可能不需要控制器。但我确实需要@Service来做可视化服务。

您好,您的logService接口应该用@service进行注释,然后您可以验证它是否不再为null,或者是否包含在应用程序上下文中。您可以探索应用程序上下文中加载的所有bean。

最新更新