我正在使用 Jax-RS 2.0 和 Jersey 2.22.1 和 Java SE 8,部署在 Tomcat 8.0.30 中。我使用适当的 JAX-RS 注释对 POJO 进行了注释,并且它按预期工作。我还用@Singleton
. 该类被懒惰地实例化为单例,这是@Singleton
. 但是我想在应用程序启动时急切地实例化这个类。有没有办法做到这一点?我查看了@Startup
注释,但不幸的是,这是 EJB 包的一部分,我没有使用 EJB(我也不想导入 EJB jar 文件)。
我还在使用 Springframework 4.2.4,默认情况下,它会在使用@Service
or@Component
注释时急切地实例化单例,但不幸的是,我不能在 JAX-RS POJO 上使用这些注释(这就是类从SpringBeanAutowiringSupport
.
我附上了java代码,但由于它按预期工作,我不确定它会添加任何有用的东西。
import javax.inject.Singleton;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;
import org.xxx.profile.util.GeneralUtil;
import org.xxx.profile.util.WebServiceLogger;
import org.xxx.profile.util.datatransfer.AccountDTO;
import org.xxx.profile.web.exception.RestException;
import org.xxx.profile.web.exception.FailureResponse;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
@Singleton
@Path( "account" )
public class AccountWebService extends SpringBeanAutowiringSupport{
@Autowired
private AccountLogic accountLogic;
@Autowired
protected SimpleParser simplerParser;
@GET
@Path("get/{id: [a-zA-Z][a-zA-Z_0-9]*}")
@Produces( MediaType.APPLICATION_JSON )
@Consumes( MediaType.APPLICATION_JSON )
public AccountDTO retrieveAccount(@PathParam("id") String customerId) throws RestException {
try {
return accountLogic.retrieveAccount(customerId);
}
catch(Exception e){
String transactionID = GeneralUtil.getUniqueID();
WebServiceLogger.severe( transactionID, "Unable to retrieve an account for the customerId: " + customerId, e, this.getClass() );
throw new RestException( new FailureResponse( FailureResponse.Status.FAIL, "add user friendly message here", transactionID ), e );
}
}
}