如何使用Spring Boot访问属性@value

huangapple go评论132阅读模式
英文:

How to access properties @value using spring boot

问题

我想使用 @Value 来读取 application.properties 文件。

app.properties

JDBC_DRIVER=com.mysql.cj.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/vetobooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
USER=root
PASS=a

main

package com.example.java_spring_java_example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class main {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(main.class, args);
        context.getBean(JDBCMysql.class);
    }
}

JDBCMysql

package com.example.java_spring_java_example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class JDBCMysql {
    @Value("${JDBC_DRIVER}")
    public String JDBC_DRIVER;

    @Value("${USER}")
    public String USER;

    @Value("${PASS}")
    public String PASS;

    @Value("${DB_URL}")
    public String DB_URL;

    public void loadValue() {
        System.out.println(DB_URL + "-" + USER + "-" + PASS + "...");
    }
}

我得到了 null-null-null 作为结果。如何才能获得真实的值?

英文:

I would to read application.properties using @Value.

app.properties

JDBC_DRIVER =com.mysql.cj.jdbc.Driver
DB_URL =jdbc:mysql://127.0.0.1:3306/vetobooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
USER =root
PASS =a

main

package com.example.java_spring_java_example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;


@SpringBootApplication
	public class main {
			
		public static void main(String[] args) {
		
			
			ConfigurableApplicationContext context = SpringApplication.run(main.class, args);
			context.getBean(JDBCMysql.class);
			
		}

	}

JDBCMysql

package com.example.java_spring_java_example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class JDBCMysql {
	  @Value("${JDBC_DRIVER}")
		public String JDBC_DRIVE;
	   
	    @Value("${USER}")
		public String USER;

		@Value("${PASS}")
		public String PASS;

		@Value("${DB_URL}")
		public String DB_URL;
	public void loadValue() {
	  System.out.println(DB_URL+"-"+ USER+"-"+ PASS+"...");
	}
	   }

I get null-null-null as a result. How can I get the real values?

答案1

得分: 0

你不能像你所做的那样静态地访问 Spring Bean。请在 main 方法中按以下方式访问该 Bean。并且将 JDBCMysql 类中的方法改为非静态。

ConfigurableApplicationContext context = SpringApplication.run(main.class, args);
JDBCMysql jdbcmysql = context.getBean(JDBCMysql.class);
jdbcmysql.loadValue();

此外,我看到每个属性后面都有空格。移除空格后应该可以正常运行。

JDBC_DRIVER=com.mysql.cj.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/vetobooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
USER=root
PASS=a
英文:

You can not access spring bean statically as you have done. Please access the bean as below in main method. And make method in JDBCMysql class non-static.

ConfigurableApplicationContext context = SpringApplication.run(main.class, args);
JDBCMysql jdbcmysql = context.getBean(JDBCMysql.class);
jdbcmysql.loadValue();

Moreover, I see space with every property. Remove space and it should work.

JDBC_DRIVER=com.mysql.cj.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/vetobooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
USER=root
PASS=a

答案2

得分: 0

我认为这里的问题在于你正在使用static关键字。你可以这样做,但接着必须有非静态的setter方法。

即尝试改成这样:

public static String USER;

@Value("${USER}")
public void setUSER(String user) {
    .....
}
英文:

I think the issue here is that you are using the static keyword.. You can do this but you must then have non static setters ..

i.e. Try this instead ..

public static String USER;
	
	@Value("${USER}")
    public void setUSER(String user) {
        .....
    }

huangapple
  • 本文由 发表于 2020年10月19日 22:16:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定