Spring MVC 找不到“javax.servlet.ServletContext”

nwsw7zdq  于 2023-08-06  发布在  Spring
关注(0)|答案(1)|浏览(194)

我有一个Springboot应用程序如下:

  1. @SpringBootApplication
  2. @ImportResource("classpath:/config/applicationContext.xml")
  3. public class TaxBatchMain {
  4. @Autowired
  5. TaxIdService taxIdService;
  6. private static final Logger LOGGER = LogManager.getLogger(TaxBatchMain.class);
  7. public static void main(String[] args) {
  8. new SpringApplicationBuilder(TaxBatchMain.class).web(false).run(args);
  9. TaxBatchMain taxBatchMain = new TaxBatchMain();
  10. }
  11. public TaxBatchMain() {
  12. SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  13. }
  14. @PostConstruct
  15. public void checkForTransactions() {
  16. try {
  17. ////
  18. String tab = "someother content";
  19. String footer = taxIdService.formatFooter();
  20. ////
  21. ////
  22. } catch() {
  23. //////////
  24. }
  25. }
  26. }

字符串
TaxIdServiceImpl类如下:

  1. @Service
  2. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  3. public class TaxIdServiceImpl implements TaxIdService {
  4. @Autowired
  5. private ServletContext servletContext;
  6. private String formatFooter(String footer) {
  7. String[] searchList = {"<ENVIRONMENT_NAME>", "<MS_ENV_NAME>"};
  8. String[] replacementList = {(String) servletContext.getAttribute(ServletContextKey.EMAIL_HOST_NAME.name()),
  9. (String) servletContext.getAttribute(ServletContextKey.MS_EMAIL_HOST_NAME.name())};
  10. return StringUtils.replaceEach(footer, searchList, replacementList);
  11. }
  12. }


应用程序上下文如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"....................///
  3. <context:annotation-config />
  4. <context:property-placeholder location="classpath:/config.properties" />
  5. <util:properties id="configProperties" location="classpath:/config.properties" />
  6. <!-- <context:property-placeholder location="classpath:data/application.properties"/> -->
  7. <context:component-scan base-package="com.tax.main" />
  8. <context:component-scan base-package="com.tax.service" />
  9. <context:component-scan base-package="com.tax.model" />
  10. <context:component-scan base-package="com.tax.mapper" />
  11. <context:component-scan base-package="com.tax.util" />
  12. /////


当我运行主类时,我得到foll。误差,误差
应用程序启动失败
产品描述:
TaxIdServiceImpl中的字段servletContext需要一个javax.servlet.ServletContext类型的bean,但找不到。
行动:
考虑在配置中定义javax.servlet.ServletContext类型的bean。

slmsl1lt

slmsl1lt1#

尝试启用webEnvironment。您的SpringApplicationBuilder似乎未启用Web环境。
第一个月
因为你使用的是spring-boot,你可以考虑使用基于Annotation的方法,而不是基于xml的方法。

相关问题