我有码头服务器和弹簧靴。
我上课了:
@WebListener
@ManagedBean
@Configuration
@PropertySource("file:/etc/koshka/newlps.properties")
public class IasContextListener implements ServletContextListener
{
private final Logger logger =
LogManager.getLogger(IasContextListener.class.getName());
@Value("${mongo.host}")
private String host;
@Value("${mongo.port}")
private Integer port;
@Override
public void contextDestroyed(ServletContextEvent sce)
{
MongoClient mongo =
(MongoClient) sce.getServletContext()
.getAttribute(Constants.COMMON.MONGO_CLIENT);
mongo.close();
logger.info("MongoClient closed successfully");
}
@Override
public void contextInitialized(ServletContextEvent sce)
{
logger.info(String.format("Mongo host: %s\tport: %d", host, port));
MongoClient mongo = new MongoClient(host, port);
logger.info("MongoClient initialized successfully");
sce
.getServletContext()
.setAttribute(Constants.COMMON.MONGO_CLIENT, mongo);
}
}
当我通过spring-boot调试运行时,contextInitialized
函数中的主机和端口不为空,但是当我部署到Jetty9服务器时,由于空值,该函数在MongoClient mongo = new MongoClient(host, port);
上失败了。