项目启动时读取一次数据库参数,以后直接使用
新建一个InitDataListener类,实现InitializingBean, ServletContextAware,变量声明为静态全局的,直接使用就可以
import javax.annotation.Resource;
import javax.servlet.ServletContext;
@Service
public class InitDataListener implements InitializingBean, ServletContextAware {
@Resource
private VipConfigService vipConfigService;
public static VipConfig vipj1;
public static VipConfig vipj2;
public static VipConfig vipj3;
public static VipConfig vipj4;
@Override
public void afterPropertiesSet() throws Exception {
}
@Override
public void setServletContext(ServletContext servletContext) {
vipj1= vipConfigService.queryVipConfig(Utils.VIPJ1);
vipj2= vipConfigService.queryVipConfig(Utils.VIPJ2);
vipj3= vipConfigService.queryVipConfig(Utils.VIPJ3);
vipj4= vipConfigService.queryVipConfig(Utils.VIPJ4);
}
}
其他的按照ssm照常写就可以了,看一下实现层
@Slf4j
@Service
public class VipConfigServiceImpl implements VipConfigService {
@Autowired
VipConfigDao vipConfigDao;
@Override
public VipConfig queryVipConfig(String vipname) {
try {
return vipConfigDao.queryVipConfig(vipname);
}catch (Exception e){
log.error("{}找不到相关的配置信息",vipname);
throw new RuntimeException(vipname+"找不到相关的配置信息");
}
}
}
最后用的时候直接用类名就可以调用了