Spring Boot没有加载 @ConfigurationProperties。

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

Spring Boot not loading @ConfigurationProperties

问题

我正在开发一个Spring Boot应用程序,尝试配置一个bean的自定义属性,该bean提供了我的文件支持的扩展名。当我运行应用程序时,它成功运行,没有错误,但扩展名没有从application.properties文件加载。

这是我的application.properties文件中的代码:

suppourted.extensions[0]="jpg"
suppourted.extensions[1]="pdf"
suppourted.extensions[2]="png"

这是bean的代码:

package com.MyBoot.FirstBoot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "suppourted")
public class SuppourtedFiles {
	
	private List<String> extensions=new ArrayList<>();

	public List<String> getExtensions() {
		return extensions;
	}

	public void setExtensions(List<String> extensions) {
		this.extensions = extensions;
	}

	@Override
	public String toString() {
		return "SuppourtedFiles [extensions=" + extensions.size() + "]";
	}
}

我的主要SpringBootApplication:

package com.MyBoot.FirstBoot;

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

@SpringBootApplication
public class FirstBoot1Application {

	public static void main(String[] args) {
		SpringApplication.run(FirstBoot1Application.class, args);
	}
}

我不明白问题出在哪里。请帮助我。

更新:

我想将该对象发送到我的JSP页面以使用模型进行显示,而不仅仅在CommandLineRunner中创建该bean。

英文:

I am developing a Spring boot application where I am trying to configure custom properties for a bean which gives the suppourted extensions for my file .
When I run the application it runs sucessfully with 0 errors but the extensions are not loaded from the application.properties file.

Here is my code in application.properties :-----

suppourted.extensions[0]="jpg"
suppourted.extensions[1]="pdf"
suppourted.extensions[2]="png"

Here is the code of the bean:--

package com.MyBoot.FirstBoot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@ConfigurationProperties(prefix = "suppourted")
public class SuppourtedFiles {
	
	private List<String> extensions=new ArrayList<>();

	public List<String>getExtensions() {
		return extensions;
	}

	public void setExtensions(List<String>extensions) {
		this.extensions = extensions;
	}

	@Override
	public String toString() {
		return "SuppourtedFiles [extensions=" + extensions.size() + "]";
	}

}

My main SpringBootApplication:----

package com.MyBoot.FirstBoot;

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

@SpringBootApplication
public class FirstBoot1Application {

	public static void main(String[] args) {
		SpringApplication.run(FirstBoot1Application.class, args);
	}
}

I do not understand where the problem is occuring. Kindly help me.

Update:--

I want to send that object to my jsp page for displaying it using model not just create that bean in CommandLineRunner

答案1

得分: 1

如果我记得正确,你应该在你的SuppourtedFiles类中使用

@Configuration

或者在你的FirstBoot1Application中使用

@EnableConfigurationProperties(SuppourtedFiles.class)

英文:

If I remember correctly you should either use

@Configuration

in your SuppourtedFiles class or

@EnableConfigurationProperties(SuppourtedFiles.class)

in your FirstBoot1Application.

答案2

得分: 1

希望以下内容有效,

应用程序:

package com.MyBoot.FirstBoot;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;


@SpringBootApplication
@EnableConfigurationProperties(SuppourtedFiles.class)
public class FirstBoot1Application implements CommandLineRunner {
    private final SuppourtedFiles suppourtedFiles;

    public FirstBoot1Application(SuppourtedFiles suppourtedFiles) {
        this.suppourtedFiles = suppourtedFiles;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(suppourtedFiles);
    }

    public static void main(String[] args) {
        SpringApplication.run(FirstBoot1Application.class, args);
    }

}

配置类:

package com.MyBoot.FirstBoot;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@ConfigurationProperties(prefix = "suppourted")
public class SuppourtedFiles {

    private List<String> extensions = new ArrayList<>();

    public List<String> getExtensions() {
        return extensions;
    }

    public void setExtensions(List<String> extensions) {
        this.extensions = extensions;
    }

    @Override
    public String toString() {
        return "com.MyBoot.FirstBoot.SuppourtedFiles [extensions=" + Arrays.toString(extensions.toArray()) + "]"; // 更改以检查数组是否由 Spring 填充
    }

}

最小 Maven 依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

注释处理器的可选依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

请确保项目的打包方式为 jar 或 war

控制器:

@RestController
public class SampleController {
    private final SuppourtedFiles suppourtedFiles;

    public SampleController(SuppourtedFiles suppourtedFiles) {
        this.suppourtedFiles = suppourtedFiles;
    }

    @GetMapping("/extensions")
    public ResponseEntity<List<String>> extensions(){
        return ResponseEntity.ok(suppourtedFiles.getExtensions());
    }
}

控制器的依赖项:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
英文:

Hope below will work,

Applications:

package com.MyBoot.FirstBoot;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;


@SpringBootApplication
@EnableConfigurationProperties(SuppourtedFiles.class)
public class FirstBoot1Application implements CommandLineRunner {
    private final SuppourtedFiles suppourtedFiles;

    public FirstBoot1Application(SuppourtedFiles suppourtedFiles) {
        this.suppourtedFiles = suppourtedFiles;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(suppourtedFiles);
    }

    public static void main(String[] args) {
        SpringApplication.run(FirstBoot1Application.class, args);
    }

}

Configuration Class:

package com.MyBoot.FirstBoot;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@ConfigurationProperties(prefix = &quot;suppourted&quot;)
public class SuppourtedFiles {

    private List&lt;String&gt; extensions = new ArrayList&lt;&gt;();

    public List&lt;String&gt; getExtensions() {
        return extensions;
    }

    public void setExtensions(List&lt;String&gt; extensions) {
        this.extensions = extensions;
    }

    @Override
    public String toString() {
        return &quot;com.MyBoot.FirstBoot.SuppourtedFiles [extensions=&quot; + Arrays.toString(extensions.toArray()) + &quot;]&quot;; // Changed to check if array is filled from spring or not.
    }

}

minimum maven dependencies

&lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

Optional Dependency for annotation processor:

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
  &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;

> Please make sure your project packaging is jar or war

Controller:

@RestController
public class SampleController {
    private final SuppourtedFiles suppourtedFiles;

    public SampleController(SuppourtedFiles suppourtedFiles) {
        this.suppourtedFiles = suppourtedFiles;
    }

    @GetMapping(&quot;/extensions&quot;)
    public ResponseEntity&lt;List&lt;String&gt;&gt; extensions(){
        return ResponseEntity.ok(suppourtedFiles.getExtensions());
    }
}

Controller Dependency:

&lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

答案3

得分: 0

你应该在SuppourtedFiles类上使用@Configuration注解,同时在FirstBoot1Application类上使用@EnableConfigurationProperties注解,并添加org.springframework.boot:spring-boot-configuration-processor依赖。

package com.MyBoot.FirstBoot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "suppourted")
public class SuppourtedFiles {
    // 不需要创建 extensions 对象,只需声明它
    private List<String> extensions;

    public List<String> getExtensions() {
        return extensions;
    }
    public void setExtensions(List<String> extensions) {
        this.extensions = extensions;
    }
}

package com.MyBoot.FirstBoot;

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

@SpringBootApplication
@EnableConfigurationProperties
public class FirstBoot1Application {

    public static void main(String[] args) {
        SpringApplication.run(FirstBoot1Application.class, args);
    }

}

而且在属性文件中不需要双引号。你可以这样写:

suppourted.extensions[0]=jpg
suppourted.extensions[1]=pdf
suppourted.extensions[2]=png
英文:

You should have the @Configuration on the SuppourtedFiles class and also the @EnableConfigurationProperties on the FirstBoot1Application class and add the org.springframework.boot:spring-boot-configuration-processor dependency.

package com.MyBoot.FirstBoot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = &quot;suppourted&quot;)
public class SuppourtedFiles {
    //You should not create the extensions object just declare it
    private List&lt;String&gt; extensions;

    public List&lt;String&gt;getExtensions() {
        return extensions;
    }
    public void setExtensions(List&lt;String&gt;extensions) {
        this.extensions = extensions;
    }
}

package com.MyBoot.FirstBoot;

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

@SpringBootApplication
@EnableConfigurationProperties
public class FirstBoot1Application {

    public static void main(String[] args) {
        SpringApplication.run(FirstBoot1Application.class, args);
    }

}

And also in the properties you do not need double quotes. You can just have,

suppourted.extensions[0]=jpg
suppourted.extensions[1]=pdf
suppourted.extensions[2]=png

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

发表评论

匿名网友

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

确定