@Value in Springboot returns null

前端 未结 5 941
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 00:22

I have the application.properties, which is located in the resources

apllication.properties

hsm.provider=software
hsm.name=TestHsm
h         


        
相关标签:
5条回答
  • 2021-01-18 00:46

    Controller.java

    With Lombok

    package com.example.demo;
    
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/hsm")
    @Data
    public class Controller {
        @Value("${hsm.ip}")
        private String ip;
    
        @Value("${hsm.port}")
        private String port;
    
        @Value("${hsm.name}")
        private String name;
    
        @Value("${hsm.timeout}")
        private String timeout;
    
        @Value("${hsm.provider}")
        private String provider;
    }
    

    Without Lombok ( generated by Intelliji - Refactor DeLombok )

        package com.example.demo;
    
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
    
        @RestController
        @RequestMapping("/hsm")
        public class Controller {
            @Value("${hsm.ip}")
            private String ip;
    
            @Value("${hsm.port}")
            private String port;
    
            @Value("${hsm.name}")
            private String name;
    
            @Value("${hsm.timeout}")
            private String timeout;
    
            @Value("${hsm.provider}")
            private String provider;
    
            public Controller() {
            }
    
            public String getIp() {
                return this.ip;
            }
    
            public String getPort() {
                return this.port;
            }
    
            public String getName() {
                return this.name;
            }
    
            public String getTimeout() {
                return this.timeout;
            }
    
            public String getProvider() {
                return this.provider;
            }
    
            public void setIp(String ip) {
                this.ip = ip;
            }
    
            public void setPort(String port) {
                this.port = port;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public void setTimeout(String timeout) {
                this.timeout = timeout;
            }
    
            public void setProvider(String provider) {
                this.provider = provider;
            }
    
            public boolean equals(final Object o) {
                if (o == this) return true;
                if (!(o instanceof Controller)) return false;
                final Controller other = (Controller) o;
                if (!other.canEqual((Object) this)) return false;
                final Object this$ip = this.getIp();
                final Object other$ip = other.getIp();
                if (this$ip == null ? other$ip != null : !this$ip.equals(other$ip)) return false;
                final Object this$port = this.getPort();
                final Object other$port = other.getPort();
                if (this$port == null ? other$port != null : !this$port.equals(other$port)) return false;
                final Object this$name = this.getName();
                final Object other$name = other.getName();
                if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
                final Object this$timeout = this.getTimeout();
                final Object other$timeout = other.getTimeout();
                if (this$timeout == null ? other$timeout != null : !this$timeout.equals(other$timeout)) return false;
                final Object this$provider = this.getProvider();
                final Object other$provider = other.getProvider();
                if (this$provider == null ? other$provider != null : !this$provider.equals(other$provider)) return false;
                return true;
            }
    
            protected boolean canEqual(final Object other) {
                return other instanceof Controller;
            }
    
            public int hashCode() {
                final int PRIME = 59;
                int result = 1;
                final Object $ip = this.getIp();
                result = result * PRIME + ($ip == null ? 43 : $ip.hashCode());
                final Object $port = this.getPort();
                result = result * PRIME + ($port == null ? 43 : $port.hashCode());
                final Object $name = this.getName();
                result = result * PRIME + ($name == null ? 43 : $name.hashCode());
                final Object $timeout = this.getTimeout();
                result = result * PRIME + ($timeout == null ? 43 : $timeout.hashCode());
                final Object $provider = this.getProvider();
                result = result * PRIME + ($provider == null ? 43 : $provider.hashCode());
                return result;
            }
    
            public String toString() {
                return "Controller(ip=" + this.getIp() + ", port=" + this.getPort() + ", name=" + this.getName() + ", timeout=" + this.getTimeout() + ", provider=" + this.getProvider() + ")";
            }
        }
    

    DemoApplication.java

    package com.example.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class DemoApplication {
        @Autowired
        Controller controller;
    
        public static void main(String[] args) {
            try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
                DemoApplication app = ctx.getBean(DemoApplication.class);
                app.run(args);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void run(String... args) throws Exception {
            System.out.println(controller);
        }
    }
    

    Output: Controller(ip=127.0.0.1, port=3001, name=TestHsm, timeout=1000, provider=software)

    0 讨论(0)
  • 2021-01-18 00:55

    I had the same issue before and @Value was not working for controller but working for component classes so i had used below solution.

    You can @Autowire Environment environment and then environment.getProperty("hsm.provider").

    Note: This is just a workaround solution.

    0 讨论(0)
  • 2021-01-18 01:04

    Judging by your package structure, those properties should definitely be loaded. Only possible option is that you have instantiated your Controller class as new Controller() instead of letting spring injecting the class (using @Autowired or constructor injection).

    0 讨论(0)
  • 2021-01-18 01:09

    Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.

    0 讨论(0)
  • 2021-01-18 01:09

    I've added your code into a Spring boot project and it is in a working condition.

    Checkout this 53482633 repository and follow the instructions to get it up and running.

    Also compare your code against this application to figure out what was going wrong at your end.

    In case if you still face any issues, please post it here.

    0 讨论(0)
提交回复
热议问题