服务器启动时 Glassfish 4.1invoke 方法?
Glassfish 4.1invoke method on the server's startup?
我正在使用 Glassfish 4.1 部署一个使用 jersey 2.23.2 的 RESTful webapp。
我想做的是运行服务器启动时的方法
我的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>XYZ</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>MyApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
和MyApplication.java
import javax.annotation.PostConstruct;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("services");
}
@PostConstruct
public void post_construct () {
//The method I want to run
}
}
我猜你的解决方案不起作用,因为你的 class 不是由容器管理的,因此 @PostConstruct
没有得到评估。
您可以尝试以下方法:
import javax.annotation.PostConstruct;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("services");
post_construct(); // <-- here is the call
}
@PostConstruct
public void post_construct () {
//The method I want to run
}
}
如果这不起作用这里是 "real" 解决方案:
@Singleton
@Startup
public class InitializerBean {
@PostConstruct
public void onStartup() {
// do your method-call here
}
}
另请参阅:
- The Lifecycles of Enterprise Beans
- Seven ways to get things started. Java EE Startup Classes with GlassFish and WebLogic
- @Singleton, @Startup, @PostConstruct doesn't work on EJB3.1 and Glassfishv3.0.1
我正在使用 Glassfish 4.1 部署一个使用 jersey 2.23.2 的 RESTful webapp。
我想做的是运行服务器启动时的方法
我的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>XYZ</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>MyApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
和MyApplication.java
import javax.annotation.PostConstruct;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("services");
}
@PostConstruct
public void post_construct () {
//The method I want to run
}
}
我猜你的解决方案不起作用,因为你的 class 不是由容器管理的,因此 @PostConstruct
没有得到评估。
您可以尝试以下方法:
import javax.annotation.PostConstruct;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("services");
post_construct(); // <-- here is the call
}
@PostConstruct
public void post_construct () {
//The method I want to run
}
}
如果这不起作用这里是 "real" 解决方案:
@Singleton
@Startup
public class InitializerBean {
@PostConstruct
public void onStartup() {
// do your method-call here
}
}
另请参阅:
- The Lifecycles of Enterprise Beans
- Seven ways to get things started. Java EE Startup Classes with GlassFish and WebLogic
- @Singleton, @Startup, @PostConstruct doesn't work on EJB3.1 and Glassfishv3.0.1