工具类中调用数据。但是由于工具类方法一般都写成static,所以直接注入就存在问题。
所以写成了这样:
package com.rm.framework.core;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.PostConstruct;
@Component
public class RivuDataContext {
/** 应用上下文 */
public static String webRoot = "/r3";
/** 应用上下文 */
public static String cookieNameLoginToken = "LoginToken";
/** 当前线程用户名 */
public final static String USER_SESSION_NAME = "curUser";
/** spring上下文 */
@Autowired
private ConfigurableApplicationContext applicationContext;
private static RivuDataContext rivuDataContext;
@PostConstruct
public void init() {
rivuDataContext = this;
rivuDataContext.applicationContext = this.applicationContext;
}
public static void setApplicationContext(ConfigurableApplicationContext webApplicationContext) {
rivuDataContext.applicationContext= webApplicationContext;
}
public static ConfigurableApplicationContext getApplicationContext() {
return rivuDataContext.applicationContext;
}
public static Object getBean(String beanName){
return rivuDataContext.applicationContext.getBean(beanName);
}
}