如何使用Spring访问POJO中的context-param?我想也许通过添加@Component
它将由Spring“管理”然后我可以使用@Value
注释。字符串minutes = null而不是= 5,如上下文中所设置。
@Component
public class process {
@Value("${minutes}")
private static String minutes;
}
的web.xml
<context-param>
<param-name>minutes</param-name>
<param-value>5</param-value>
</context-param>
答案 0 :(得分:0)
检查给定的样本:
@Component
public class process {
private String minutes;
public String getMinutes(){
minutes= context.getInitParameter(startTime);
return minutes;
}
@Autowired
ServletContext context;
答案 1 :(得分:0)
首先,您必须创建一个新的“ServletContextListener”
<div class="nav navbar-nav navbar-left leftSide">
<ul class="nav navbar-nav menu">
<li><a href="#">Browse</a></li>
<li><a href="#">Map</a></li>
<li><a href="#">Interviews</a></li>
<li><a href="#">Places</a></li>
<li class="fav"><a href="#">My Favourites</a></li>
</ul>
</div>
带有静态变量的POJO类:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
public class DataInitionlazationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
ServletContext context = sce.getServletContext();
Process.minutes = context.getInitParameter(startTime);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
通过使用上面的类,它将初始化您的POJO静态变量: