解决@Value 注解自动注入 配置文件属性 为null 的问题
2016-10-20 17:07
417 查看
1. 不能直接给静态属性注入值
如果必须是静态属性注入 可以使用set方法来注入值
@Component public class Test{ public static String VALUE; @Value("${propeties.string}") public void setVALUE(String s) { VALUE = s; }
2。被注入的属性必须是spring的托管类
3.配置文件问题 springMvc 和 spring 是两个容器
如果加载配置文件在spring容器中 springMvc注入属性 会找不到配置文件
不使用spring 也可使用如下方法注入 属性
private static Properties props; public static final String channelUser ; static{ props = PropertyUtil.getInstance().createProperties("/function.properties"); channelUser = props.getProperty("function.channelUser").trim(); }
相关文章推荐
- Spring框架 注解annotation和配置文件混合使用(生成对象与属性注入) xml配置文件没有提示解决
- Spring配置文件使用@Value注解注入值的方法,以及AOP切面在Spring+SpringMVC中的不生效解决
- Spring注解配置注入失败属性null解决
- Spring 中注入bean的properties配置文件位置问题解决
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- 解决maven3和myeclipse8.6结合后配置文件无法自动拷贝到对应目录问题
- spring-自动加载配置文件\使用属性文件注入
- 解决 SpringMvc 非controller类使用@Autowired注解 service注入为null的问题
- spring-自动加载配置文件\使用属性文件注入
- 解决intellij idea git 自动提交配置文件问题
- XML配置文件导入dtd文件仍然没有自动提示问题的解决
- spring加载属性文件影响SqlSessionFactoryBean的bean和MapperScannerConfigurerbean扫描器自动注入问题
- JSON通过配置文件格式化时间属性(解决[object Object]问题)
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- 解决maven3和myeclipse8.6结合后配置文件无法自动拷贝到对应目录问题
- 解决非controller使用@Autowired注解注入为null问题
- 自定义类型转换器解决Spring配置文件注入类型错误的问题 PropertyEditorSupport
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
- Spring 使用注解注入,调用类实例出现 java.lang.NullPointException 问题的解决方法
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解