Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring引导配置属性不工作_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

spring引导配置属性不工作

spring引导配置属性不工作,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,为了让spring boot@configurationproperties注释正常工作,这让我非常恼火。所以希望有人能为我解释一下我做错了什么。 我有一个spring启动应用程序,它在类路径上包含application.properties。它在那里有一个价值 server.contextPath=/test/v1 server.port=8080 spring.profiles.active=dev vendors=me 我有一个application.class,它有spring引导注

为了让spring boot@configurationproperties注释正常工作,这让我非常恼火。所以希望有人能为我解释一下我做错了什么。 我有一个spring启动应用程序,它在类路径上包含application.properties。它在那里有一个价值

server.contextPath=/test/v1
server.port=8080

spring.profiles.active=dev
vendors=me
我有一个application.class,它有spring引导注释,位于包层次结构的顶部

package com.test;

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

@SpringBootApplication
@EnableConfigurationProperties
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
我试图将属性供应商映射到configurationproperties bean中,如下所示

package com.test.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties
public class GlobalProperties {

    private String vendors;

    public String getVendors() {
        return vendors;
    }

    public void setVendors(String vendors) {
        this.vendors = vendors;
    }
}
然后从我的rest控制器调用这个bean。我知道它解析属性,因为当我重命名它时,服务器无法启动。在下面的代码中,props bean没有自动连接,为null//为简洁起见而编写的代码

package com.test.controller;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.test.config.GlobalProperties;

@RestController
@Component
public class MController {


    //TODO should be wired in from properties file 
    @Autowired
    GlobalProperties props;


    private boolean vendorUnknown(String vendor) {
        if(props.getAllowedVendor().equalsIgnoreCase(vendor)) {
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = { "application/json" })
    public ResponseEntity<?> getStatus(
            @PathVariable String id) {
        //@RequestBody Bookmark input
        if(vendorUnknown("me")) {
        System.out.println("found");
    };
        return ResponseEntity.noContent().build();
    }

}

将@EnableConfigurationProperties添加到应用程序类以启用扫描ConfigurationProperties bean

  • 删除
    @PropertySource
    。您可以保留
    @Component
    ,如果不保留,请指定
    @EnableConfigurationProperties(GlobalProperties.class)
  • 不需要
    testController
    上的
    @组件
    。但我认为您的问题在于从何处调用
    vendorUnknown
    方法。它没有显示在您的代码中。如果从构造函数调用,则bean初始化尚未完成,
    GlobalProperties props
    实际上为空
  • 编辑

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
    根据OP的编辑,这里有一个完全有效的解决方案

    DemoApplication.java

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
    DemoController.java

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
    应用程序属性

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
    DemoControllerTest.java

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
    OP代码出现问题

    @SpringBootApplication
    @EnableConfigurationProperties
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    public class DemoController {
        @Autowired
        private DemoProperties props;
    
        @GetMapping(value = "/", produces = APPLICATION_JSON_VALUE)
        public ResponseEntity<?> getVendor() {
            return ResponseEntity.ok(props.getVendors());
        }
    }
    
    @ConfigurationProperties
    @Component
    public class DemoProperties {
        private String vendors;
    
        public String getVendors() {
            return vendors;
        }
    
        public void setVendors(String vendors) {
            this.vendors = vendors;
        }
    }
    
    vendors=me
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class DemoControllerTest {
        @Autowired
        TestRestTemplate restTemplate;
    
        @Test
        public void testGenVendor() throws Exception {
            String vendor = restTemplate.getForObject("/", String.class);
    
            assertThat(vendor).isEqualTo("me");
        }
    }
    
  • MockMvc
    不使用主类,因此不会处理
    EnableConfigurationProperties
    MockMvc
    WebMvcTest
    旨在测试web层(duh!),而不是整个应用程序。使用这两种方法中的任何一种。属性bean应该由测试设置

  • InjectMock
    以静默方式失败
    。看


  • @PropertySource
    @Component
    也可以从
    GlobalProperties
    中删除。添加了@EnableConfigurationProperties到我的application.class中,但我的props bean在控制器中仍然为空。我不是从构造函数调用,而是从controller@user1107753,创建一个复制您的问题的示例。我不确定从上面发布的代码中可以得到多少最小值。@user1107753独立运行并复制您的问题的东西。用一个简单的示例和测试类更新了问题。测试将失败,但它表明属性未设置