简体   繁体   English

Spring-如何将ServletContext转换为@Service(+如何获取WebApps清单)

[英]Spring - how to get ServletContext into a @Service (+ how to get WebApps Manifest)

In a Web App's @Controllers you can autowire your Servlet Context so you can (in my case) get the Manifest from the web-app (see https://stackoverflow.com/a/615545/1019307 ). 在Web应用程序的@Controllers您可以自动连接Servlet上下文,以便(在我的情况下)可以从Web应用程序获取清单(请参阅https://stackoverflow.com/a/615545/1019307 )。

@Autowired
ServletContext servletContext;

How do you get this into the service? 您如何将其纳入服务?

I implemented this simple pattern and thought I'd share. 我实现了这种简单的模式,并认为自己会分享。

Update: This is a poor solution as it makes the service depend on the client. 更新:这是一个较差的解决方案,因为它使服务依赖于客户端。 See below for updated solution. 请参阅下面的更新解决方案。

Simply with a @PostConstruct so that the Service has the ServletContext set before it isRunning. 只需使用@PostConstruct以便Service在运行之前设置ServletContext。

@Controller
@RequestMapping("/manifests")
public class ManifestEndpoint {
    @Autowired
    private ManifestService manifestService;

    @Autowired
    ServletContext servletContext;

    @PostConstruct
    public void initService() {
        manifestService.setServletContext(servletContext);
    }

Then in the service be sure to check it is used, since that can't be guaranteed. 然后,请确保在服务中检查它的使用情况,因为不能保证这样做。

@Component
public class ManifestService {
    ....
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    private void buildManifestCurrentWebApp() {
        if (servletContext == null) {
            throw new RuntimeException("ServletContext not set");
        }
        # Here's how to complete my example on how to get WebApp Manifest
        try {
            URL thisAppsManifestURL = servletContext.getResource("/META-INF/MANIFEST.MF");
            System.out.println("buildManifestCurrentWebApp - url: "+thisAppsManifestURL);
            buildManifest(thisAppsManifestURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

Updated solution that doesn't make the service depend on the client. 更新了解决方案,使服务不依赖于客户端。

@Controller
@RequestMapping("/manifests")
public class ManifestEndpoint {
    private static final Logger logger = LoggerFactory.logger(ManifestEndpoint.class);
    @Autowired
    private ManifestService manifestService;

    @Autowired
    private ServletContext servletContext;

    @PostConstruct
    public void initService() {
        // We need to use the Manifest from this web app.
        URL thisAppsManifestURL;
        try {
            thisAppsManifestURL = servletContext.getResource("/META-INF/MANIFEST.MF");
        } catch (MalformedURLException e) {
            throw new GeodesyRuntimeException("Error retrieving META-INF/MANIFEST.MF resource from webapp", e);
        }
        manifestService.buildManifest(thisAppsManifestURL);
    }

The ManifestService doesn't change (that is, there is no need for buildManifestCurrentWebApp() now). ManifestService不变(也就是说,现在不需要buildManifestCurrentWebApp())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM