简体   繁体   中英

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. However tomcat seems not executing @PostConstruct annotation.

For example: I have the following in a bean:

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

But its not executing the void bean the bean is called.

I have included javax.annotation-api-1.3.2 jar in the WEB-INF\Lib folder.

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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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