简体   繁体   English

Spring Bean未在应用程序启动时初始化

[英]Spring Bean is not initializing on Application startup

In my application i want to initialize the bean before hitting application url, and store the common values for drop-down. 在我的应用程序中,我想在访问应用程序URL之前初始化bean,并存储下拉菜单的常用值。 here it is declaration for bean 这是bean的声明

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

Bean: 豆角,扁豆:

public class DropDownList implements InitializingBean
{
    private static final Logger                     logger  = LoggerFactory.getLogger(DropDownList.class);
    public static Map<String, Map<Integer, String>> listMap = new HashMap<String, Map<Integer, String>>();
    @Autowired
    private static SystemService                    systemService;

    @Autowired(required = true)
    @Qualifier(value = "systemService")
    public void setSystemService(SystemService systemService)
    {
        this.systemService = systemService;
    }

    @PostConstruct
    public static Map<String, Map<Integer, String>> populateMasterList()
    {
        logger.debug("Calling Institute Info Masters");
        List<InstituteInfoMaster> masterList = systemService.listInstituteInfoMasters();
        Map<Integer, String> masterMap = new HashMap<Integer, String>();
        masterMap.put(0, "---Select---");
        masterList.forEach((master) ->
        {
            masterMap.put(master.getListId(), master.getValue());
        });
        logger.debug("Created Map for List Masters");
        listMap.put("infoList", masterMap);
        return listMap;
    }

    public Map<String, Map<Integer, String>> getListMap()
    {
        return listMap;
    }

    public static void setListMap()
    {
        listMap = populateMasterList();
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        populateMasterList();

    }
}

I observed that it does not initializes on Application startup. 我观察到它不会在应用程序启动时初始化。 when i try to update the master by calling DropDownList.setListMap(); 当我尝试通过调用DropDownList.setListMap();更新母版时DropDownList.setListMap(); it gives NullPointerException . 它给出了NullPointerException but if i calls the jsp page where i am calling the Map as ${listService.listMap['infoList']} it displays the Drop-down on jsp after it if i tries to save master it executes successfully. 但是,如果我在jsp页面中调用地图,将其称为${listService.listMap['infoList']} ,则在我尝试保存母版后,它将在jsp上显示下拉列表,如果它成功执行。

it means when i calls the jsp page where i am showing drop-down that time only it initializing the bean not on Application startup. 这意味着当我调用那时候我显示下拉菜单的jsp页面时,它只会在应用程序启动时初始化bean。

The actual problem is that you are not accessing the Spring bean , but the class, staticly. 实际的问题是您不是静态访问Spring bean而是类。 When you use the bean, ie the listService instance, Spring will initalize it for you on first access. 当您使用bean(即listService实例)时,Spring会在首次访问时为您listService它。

You are calling a static method, but when this happens, the dependant beans are not populated. 您正在调用静态方法,但是发生这种情况时,不会填充从属bean。 Autowiring works for instances (ie in non-static context), so systemService is null in your application. 自动装配适用于实例(即在非静态上下文中),因此systemService在您的应用程序中为null

Update: I have just realized this line: 更新:我刚刚意识到这一行:

@Autowired
private static SystemService systemService;

This is fundamentally wrong. 这根本是错误的。 You cannot autowire static fields, it makes absolutely no sense in Spring (or in any similar framework). 无法自动装配静态字段,在Spring(或任何类似框架)中这绝对没有意义。 Spring beans are instances, and the framework set the autowired fields to references to other spring beans. Spring Bean是实例,并且框架将自动装配的字段设置为对其他Spring Bean的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM