我们知道,如果我们在一个不加@Configuration 或者 @Service的类中,直接使用
@Autowired注入,比如:
@Autowired
private XXXService xxxService;
这样子调试的时候,实际上收到的xxxService对象是null,同时,如果我们使用了
@Value 比如
@Value("${data-collector.basepath}")
这种方式获取yml中 配置信息的时候也是获取不到的,获取出来是个null怎么弄?
-------------------->>>>>>>>开始解决>>>>>>>--------------
1.首先我们需要一个springUtil工具类,用来从spring容器中获取我们想要的Bean
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@Slf4j
public class SpringUtil implements BeanFactoryPostProcessor {
/** Spring应用上下文环境 */
private static ConfigurableListableBeanFactory beanFactory;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
SpringUtil.beanFactory = beanFactory;
}
/**
* 获取对象
*
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws org.springframework.beans.BeansException
*
*/
@SuppressWarnings("unchecked")
public static T getBean(String name) throws BeansException
{
return (T) beanFactory.getBean(name);
}
/**
* 获取类型为requiredType的对象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*
*/
public static T getBean(Class clz) throws BeansException
{
T result = (T) beanFactory.getBean(clz);
return result;
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name)
{
return beanFactory.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
{
return beanFactory.getAliases(name);
}
/**
* 获取aop代理对象
*
* @param invoker
* @return
*/
@SuppressWarnings("unchecked")
public static T getAopProxy(T invoker)
{
return (T) AopContext.currentProxy();
}
}
2.然后再写一个注入类,把我们要注入的信息,首先注入到这个注入类中,然后我们在使用对应,
bean,或者配置信息的类中,再去通过静态代码块,利用这个注入类获取出信息来.
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Getter
@Setter
@ConfigurationProperties(prefix = "data-collector")
@Configuration
public class SpringPropertiesHelper {
/**
* 基本路径
*/
private String basePath;
/**
* 操作某个数据库表的类
*/
private IstudentService studentService;
}
2.然后在要使用这些配置信息,或者是某个类的 类中写入如下代码,在顶部
@Slf4j
public class FileWriterCsvUtil {
/**
* 基础路径
*/
private static String basePath;
/**
* 某个数据库表的操作类
*
*/
private static StudentServiceImpl studentService;
static {
SpringPropertiesHelper springPropertiesHelper =
SpringUtil.getBean(SpringPropertiesHelper.class);
basePath = springPropertiesHelper.getBasePath();
studentService = (StudentServiceImpl) springPropertiesHelper.getStudentService();
}
......其他代码
}
可以看到通过上面的方法就可以在普通的类中,获取到yml的配置文件信息,以及,对应的注入到
spring容器中的类对象了.
亲测可用~希望可以帮到大家.
编辑
留言与评论(共有 0 条评论) “” |