Spring boot Value的值取不到

               

boot中@Value读取配置信息,1.声明为Bean,可以被spring管理 2.要通过构造方法注入,通过new 对象的方式,@Value的值读取不到。

例如如下图:

配置Bean的配置

@Data@NoArgsConstructor@Componentpublic class OSSConfig{// oss服务器地址,北上广深等 @Value("${live.oss.endpoint.video.gaokao}"private String endpoint; @Value("${live.oss.accessKeyId}"private String accessKeyId; @Value("${live.oss.accessKeySecret}"private String accessKeySecret; @Value("${live.oss.bucketName.gaokao}"private String bucketName;}
control中的引用

public class LiveOssController {    private LiveOssService liveOssService ;    private OSSConfig ossConfig ;    public LiveOssController(LiveOssService liveOssService,OSSConfig ossConfig){        this.liveOssService = liveOssService ;        this.ossConfig = ossConfig ;    }}



@Value的两种方式

基于@Value进行注入时有两种方式,占位符和spel表达式 
//占位符方式
    @Value("${jdbc.url}")
    private String url;
    //SpEL表达方式,其中代表xml配置文件中的id值configProperties
    @Value("#{configProperties['jdbc.username']}")
    private String userName;
这两种方式需要在xml中配置时也是不一样的
<!--基于占位符方式 配置单个properties -->
    <!--<context:property-placeholder location="conf/jdbc.properties"/>-->
    <!--基于占位符方式 配置多个properties -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="location" value="conf/jdbc.properties"/>
    </bean>

    <!--基于SpEL表达式 配置多个properties id值为configProperties 提供java代码中使用 -->
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/conf/jdbc.properties</value>
            </list>
        </property>
    </bean>

    <!--基于SpEL表达式 配置单个properties -->
    <!--<util:properties id="configProperties" location="classpath:conf/jdbc.properties"/>-->

           

猜你喜欢

转载自blog.csdn.net/qq_44906668/article/details/89244220