解决:SSM框架中普通类调用Service的问题

时间:2022-09-29 11:29:28

最近在写一个项目,需要创建一个数据库初始化的工具类,用到了调用service的部分,不过可惜的是,Spring中的Service不是你想new就能new的,作为老菜鸟,研究了小半天终于搞定了,下面就来说说具体实现的方法。


1.创建一个SpringContextUtil实现ApplicationContextAware接口

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @作者:JackHisen(GWD)
* @项目名:pet-commonTest
* @时间:2017-6-19 下午6:03:01
* @version 1.0
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static Logger logger = Logger.getLogger(SpringContextUtil.class);
private static ApplicationContext applicationContext = null;

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
logger.info("------SpringContextUtil setApplicationContext-------");
SpringContextUtil.applicationContext = applicationContext;
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
}

/**
* 注意 bean name默认 = 类名(首字母小写)
* 例如: A8sClusterDao = getBean("k8sClusterDao")
* @param name
* @return
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}

/**
* 根据类名获取到bean
* @param <T>
* @param clazz
* @return
* @throws BeansException
*/
@SuppressWarnings("unchecked")
public static <T> T getBeanByName(Class<T> clazz) throws BeansException {
try {
char[] cs=clazz.getSimpleName().toCharArray();
cs[0] += 32;//首字母大写到小写
return (T) applicationContext.getBean(String.valueOf(cs));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}

public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}

}



2.在web项目中的web.xml中配置加载Spring容器的Listener

<listener>  
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


3.Service类的注解问题:

该方法实现了ParamInfoService接口,注意上面的注解以及注解内的名称(开头小写)

@Service("paramInfoService")
public class ParamInfoServiceImp implements ParamInfoService {
@Autowired
private ParamInfoMapper paramInfoMapper;

@Override
public List<ParamInfo> getAll() {
// TODO Auto-generated method stub
return paramInfoMapper.findAll();
}

}

4.普通类调用Service的方法

注意:这里面getBean内的名称需要和Service实现类中注解的名称一致

public static void loadAllParamInfo(){
ParamInfoService paramInfoService=(ParamInfoService)SpringContextUtil.getBean("paramInfoService");
List<ParamInfo>datas=paramInfoService.getAll();
if(datas==null)return;
paramMap = new HashMap<String, ParamInfo>(datas.size());
for (ParamInfo bean : datas) {
paramMap.put(bean.getParamNameEn(), bean);
}
System.out.println("加载到参数共[" + paramMap.keySet().size() + "]个");
logger.debug("加载到参数共[" + paramMap.keySet().size() + "]个");
}