简体   繁体   English

嵌入 Tomcat 9 执行@PostConstruct

[英]Embeded Tomcat 9 to execute @PostConstruct

I am creating an executable jar where using Jsf myfaces 2.3 with Tomcat 9 embedded, so when i execute the jar it will read a war file and deploy it on the embedded server, Its reading the bean and displaying contents correctly. I am creating an executable jar where using Jsf myfaces 2.3 with Tomcat 9 embedded, so when i execute the jar it will read a war file and deploy it on the embedded server, Its reading the bean and displaying contents correctly. However tomcat seems not executing @PostConstruct annotation.但是 tomcat 似乎没有执行 @PostConstruct 注释。

For example: I have the following in a bean:例如:我在 bean 中有以下内容:

    @PostConstruct
    public void init() {
        System.out.println("init bean called..........");
    }

But its not executing the void bean the bean is called.但它不执行调用 bean 的 void bean。

I have included javax.annotation-api-1.3.2 jar in the WEB-INF\Lib folder.我已将 javax.annotation-api-1.3.2 jar 包含在 WEB-INF\Lib 文件夹中。

String contextPath = "/Test";     
        String warFilePath = "D:\\Test\\embedded\\Test.war";
StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, warFilePath);
((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true);
((StandardJarScanner) ctx.getJarScanner()).setScanAllFiles(true);
((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(true);

tomcat.start();
tomcat.getServer().await();

No error messages... on the console.没有错误消息...在控制台上。 Simply not executing @PostConstruct init void.根本不执行@PostConstruct init void。 Any help will be appreciated.任何帮助将不胜感激。

Got it.. simply need to enable naming.明白了..只需要启用命名。

tomcat.enableNaming();

in my main void:在我的主要空白中:

Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("temp");
tomcat.setPort(8080);
tomcat.enableNaming();
tomcat.getConnector(); // Tomcat 9 we need to get the connector. it not by default.

String contextPath = "/Test";     
String warFilePath = "D:\\Test\\embedded\\Test.war";

tomcat.getHost().setAppBase(".");
StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, warFilePath);
((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true);
((StandardJarScanner) ctx.getJarScanner()).setScanAllFiles(true);
((StandardJarScanner) ctx.getJarScanner()).setScanClassPath(true);

tomcat.start();
tomcat.getServer().await();

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

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