SpringBoot利用注解@Value获取properties属性为null

参考:https://www.cnblogs.com/zacky31/p/8609990.html

今天在项目中想使用@Value来获取Springboot中properties中属性值。

场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到。

如何解决:在使用这个工具类的时候是new的,要想使用@value来获取,必须将这个工具类交由容器来注入,

SeaWeedFs seaWeedFs = new SeaWeedFs();

改为

@Autowired
private SeaWeedFs seaWeedFs;

尝试了还发现一个问题,一开始工具类中的方法是静态的,当Springboot启动时后报一个错误

把static去掉以后,终于可以用@value来获取properties中的属性值

原文地址:https://www.cnblogs.com/huanghongbo/p/9098069.html

时间: 2024-08-01 23:28:33

SpringBoot利用注解@Value获取properties属性为null的相关文章

spring利用注解@Value获取properties属性为null

原因可能有好几种 1.spring与spring mvc是两个容器,调用时候要分清哪个容器 2.@Component 并且 context:component-scan 3.获取配置文件http://www.henryxi.com/read-values-from-properties-file-in-spring

springMvc中获取通过注解获取properties配置文件

springMvc的项目中,通过注解@Value获取properties配置文件中的配置,使用该注解必须引入的包: spring-beans-4.1.4.RELEASE.jar 下面是需要在spring的配置文件中配置的内容 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns

Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/resources/conf/boot.properties com.ieen.boot.name="123" com.ieen.boot.value="456" 调用方法@Value 注解调用application.properties属性值: @Value("

SpringBoot利用自定义注解实现AOP

SpringBoot利用自定义注解实现AOP java 本文主要讲解利用SpringBoot的自定义注解来实现AOP思想. 在开始所有的操作之前,需要导入aop坐标: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 如何自定义注解? 实际上注解本

@Value 注解获取properties值

转自:使用Spring 3的@value简化配置文件的读取 Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码. 1.在applicationContext.xml文件中配置properties文件 <bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&g

C#利用反射获取对象属性值

public static string GetObjectPropertyValue<T>(T t, string propertyname){     Type type = typeof(T); PropertyInfo property = type.GetProperty(propertyname); if (property == null) return string.Empty; object o = property.GetValue(t, null); if (o == n

spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别

spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给DispatcherServlet 管理的 springMVC.xml才能用? 要交给springMVC的DispatcherServlet去扫描,而不是spring的监听器ContextLoaderListener去扫描,就可以比较方便的使用"${xxx}"去注入. 1.使用 $ 获取属性 @Value(

Java读取利用java.util类Properties读取resource下的properties属性文件

说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.ResourceBundle; public class Test { private static Properties pro ; static{ InputStream inputStream = Test.class.ge

二、Springboot 常用注解

@SpringBootApplication: ??包含@Configuration.@EnableAutoConfiguration.@ComponentScan通常用在主类上.??很多SpringBoot开发者总是使用@Configuration,@EnableAutoConfiguration和 @ComponentScan注解他们的main类.由于这些注解被如此频繁地一块使用,SpringBoot提供一个方便的@SpringBootApplication选择.该??@SpringBoot