![](/img/trans.png)
[英]Difference between javax.servlet.ServletContext and javax.naming.Context
[英]'javax.servlet.ServletContext' that could not be found
我有一個Springboot應用程序如下:
@SpringBootApplication
@ImportResource("classpath:/config/applicationContext.xml")
public class TaxBatchMain {
@Autowired
TaxIdService taxIdService;
private static final Logger LOGGER = LogManager.getLogger(TaxBatchMain.class);
public static void main(String[] args) {
new SpringApplicationBuilder(TaxBatchMain.class).web(false).run(args);
TaxBatchMain taxBatchMain = new TaxBatchMain();
}
public TaxBatchMain() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
@PostConstruct
public void checkForTransactions() {
try {
////
String tab = "someother content";
String footer = taxIdService.formatFooter();
////
////
}catch(){
//////////
}
}
}
TaxIdServiceImpl類如下:
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TaxIdServiceImpl implements TaxIdService {
@Autowired
private ServletContext servletContext;
private String formatFooter(String footer) {
String[] searchList = {"<ENVIRONMENT_NAME>", "<MS_ENV_NAME>"};
String[] replacementList = {(String) servletContext.getAttribute(ServletContextKey.EMAIL_HOST_NAME.name()),
(String) servletContext.getAttribute(ServletContextKey.MS_EMAIL_HOST_NAME.name())};
return StringUtils.replaceEach(footer, searchList, replacementList);
}
}
應用程序上下文如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"....................///
<context:annotation-config />
<context:property-placeholder location="classpath:/config.properties" />
<util:properties id="configProperties" location="classpath:/config.properties" />
<!-- <context:property-placeholder location="classpath:data/application.properties"/> -->
<context:component-scan base-package="com.tax.main" />
<context:component-scan base-package="com.tax.service" />
<context:component-scan base-package="com.tax.model" />
<context:component-scan base-package="com.tax.mapper" />
<context:component-scan base-package="com.tax.util" />
/////
當我運行主課時,我得到了foll。 錯誤
應用程序未能啟動
描述:
com.tax.service.TaxIdServiceImpl中的字段servletContext需要一個無法找到的類型為'javax.servlet.ServletContext'的bean。
行動:
考慮在配置中定義類型為'javax.servlet.ServletContext'的bean。
嘗試啟用webEnvironment。 您的SpringApplicationBuilder似乎未啟用Web環境。
new SpringApplicationBuilder(TaxBatchMain.class).web(true).run(args);
由於您使用的是spring-boot,因此可以考慮使用基於注釋的方法而不是基於xml的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.