Java Spring Boot 在启动时使用构造函数参数初始化 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28060700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:32:57  来源:igfitidea点击:

Spring boot initializing bean at startup with constructor parameters

javaspringspring-boot

提问by Jerry

I need to initialize the following PointQuadTreeclass on startup using Spring Boot with constructor parameters, and make the object available throughout the application. The constructor parameters 'minX, maxX, ...' need to come from the application.properties file.

我需要PointQuadTree在启动时使用带有构造函数参数的 Spring Boot初始化以下类,并使该对象在整个应用程序中可用。构造函数参数 'minX, maxX, ...' 需要来自 application.properties 文件。

PointQuadTree

点四叉树

public class PointQuadTree<T extends PointQuadTree.Item> {

   private final Bounds mBounds;

   public PointQuadTree(double minX, double maxX, double minY, double maxY) {
      this(new Bounds(minX, maxX, minY, maxY));
   }

   ...

}

Bounds

界限

public class Bounds {
   public final double minX;
   public final double minY;

   public final double maxX;
   public final double maxY;

   public final double midX;
   public final double midY;

   public Bounds(double minX, double maxX, double minY, double maxY) {
      this.minX = minX;
      this.minY = minY;
      this.maxX = maxX;
      this.maxY = maxY;

      midX = (minX + maxX) / 2;
      midY = (minY + maxY) / 2;
   }

   ...
}

I've tried annotating PointQuadTreewith @Component, but there is not constructor without parameters. Even if I add a constructor without parameters Boundsis final, so it cannot be set after PointQuadTreeis initialized. Also Boundshas a constructor with parameters only.

我试过标注PointQuadTree@Component,但没有构造函数不带参数。即使我添加了一个没有参数的构造函数Boundsis final,所以它不能在PointQuadTree初始化后设置。也Bounds有一个只有参数的构造函数。

After PointQuadTreeis initialized, I need it to sit in memory and need to be able to autowire it in other components to read/remove/add items. I have no idea how to do this with Spring Boot. Any help greatly appreciated.

之后PointQuadTree被初始化,我需要它在内存中,需要坐下来能够自动装配它的其他组件读/删除/添加项目。我不知道如何用 Spring Boot 做到这一点。非常感谢任何帮助。

采纳答案by Jayaram

This is as simple as creating beans in Spring way...

这就像在 Spring 中创建 bean 一样简单......

@Configuration
public class AppBeans{
@Value("${minx:100}")
private double minX;
...so on ..
 @Bean
   public PointQuadTree pointQuadTree()
   {
      return new PointQuadTree(minX...so on);
   }

}

and inject this bean where you want using @Autowired

并在你想使用的地方注入这个 bean @Autowired

Here ${minx:100}, tries to read from properties file, if not specified takes the default as 100

在这里${minx:100},尝试从属性文件中读取,如果未指定则默认为100

回答by Cuzz

in some configuration file create a spring bean of tree, something like this:

在一些配置文件中创建一个树的 spring bean,如下所示:

@Configuration
public class PointQuadTreeBeans
{

   @Bean(name="theSameTree")
   public PointQuadTree getPointQuadTree(Environment env)
   {
      double minX = env.getProperty("minX");
      double maxX = env.getProperty("maxX");
      double minY = env.getProperty("minY");
      double maxY = env.getProperty("maxY");
      PointQuadTree tree = new PointQuadTree(minX, maxX, minY, maxY);
   }


}

and add this class to spring componentScan

并将此类添加到 spring componentScan

UPD

UPD

another way:

其它的办法:

instead of double minX = env.getProperty("minX");you can create fields with @Value, like @chrylis said in comment:

而不是double minX = env.getProperty("minX");您可以创建字段@Value,就像@chrylis 在评论中所说的那样:

@Value("${minX}")
private double minX;

then use it field to create bean.

然后使用它的字段来创建bean。