Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springbean在应用程序启动时未初始化_Java_Spring_Hibernate - Fatal编程技术网

Java Springbean在应用程序启动时未初始化

Java Springbean在应用程序启动时未初始化,java,spring,hibernate,Java,Spring,Hibernate,在我的应用程序中,我希望在点击应用程序url之前初始化bean,并为下拉列表存储公共值。这里是bean的声明 <beans:bean id="listService" class="com.system.beans.DropDownList" init-method="populateMasterList" scope="application"/> 豆子: public类DropDownList实现初始化bean { 私有静态最终记录器Logger=Logge

在我的应用程序中,我希望在点击应用程序url之前初始化bean,并为下拉列表存储公共值。这里是bean的声明

<beans:bean id="listService" class="com.system.beans.DropDownList"
        init-method="populateMasterList" scope="application"/>

豆子:

public类DropDownList实现初始化bean
{
私有静态最终记录器Logger=LoggerFactory.getLogger(DropDownList.class);
public static Map listMap=new HashMap();
@自动连线
专用静态系统服务系统服务;
@自动连线(必需=真)
@限定符(value=“systemService”)
公共无效设置系统服务(系统服务系统服务)
{
this.systemService=systemService;
}
@施工后
公共静态映射populateMasterList()
{
logger.debug(“调用研究所信息主机”);
List masterList=systemService.ListInstituteInfo主机();
Map masterMap=newhashmap();
masterMap.put(0“--选择“-”);
主列表。forEach((主)->
{
masterMap.put(master.getListId(),master.getValue());
});
debug(“为列表主控创建的映射”);
listMap.put(“信息列表”,主地图);
返回列表图;
}
公共映射getListMap()
{
返回列表图;
}
公共静态void setListMap()
{
listMap=populateMasterList();
}
@凌驾
public void afterPropertieSet()引发异常
{
populateMasterList();
}
}
我注意到它不会在应用程序启动时初始化。当我试图通过调用
DropDownList.setListMap()来更新主机时它给出了
NullPointerException
。但是,如果我调用jsp页面,在该页面中调用映射为
${listService.listMap['infoList']}
,则如果我尝试保存master,它会在jsp上显示下拉列表,并成功执行


这意味着当我调用显示下拉列表的jsp页面时,只有它在应用程序启动时才初始化bean。

实际的问题是,您不是在静态地访问Springbean,而是在访问类。当您使用bean时,即
listService
实例,Spring将在第一次访问时为您初始化它

您正在调用一个静态方法,但是当这种情况发生时,依赖bean不会被填充。自动连接适用于实例(即在非静态上下文中),因此
systemService
在应用程序中为
null

更新:我刚刚意识到这一点:

@Autowired
private static SystemService systemService;

这是根本错误的。不能自动关联静态字段,这在Spring(或任何类似框架)中都毫无意义。SpringBean是实例,框架将自动连接字段设置为对其他SpringBean的引用。

那么我如何初始化systemService呢?我试图调试它,但我的调试指针从未进入populateMasterList方法。请使用实例方法和字段。只有记录器应保持静态。这会解决你的问题。映射不需要是静态的,因为bean在默认情况下是单态的,所以您不会得到多个映射。
populateMasterList
方法永远不会被调用,因为
@PostConstruct
在静态方法上没有意义。
@Autowired
private static SystemService systemService;