简体   繁体   English

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

[英]Spring boot initializing bean at startup with constructor parameters

I need to initialize the following PointQuadTree class on startup using Spring Boot with constructor parameters, and make the object available throughout the application. 我需要在启动时使用带有构造函数参数的Spring Boot初始化以下PointQuadTree类,并使该对象在整个应用程序中可用。 The constructor parameters 'minX, maxX, ...' need to come from the application.properties file. 构造函数参数'minX,maxX,...'需要来自application.properties文件。

PointQuadTree 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 PointQuadTree with @Component , but there is not constructor without parameters. 我尝试用@Component注释PointQuadTree ,但是没有没有参数的构造函数。 Even if I add a constructor without parameters Bounds is final , so it cannot be set after PointQuadTree is initialized. 即使我添加一个没有参数的构造函数, Bounds也是final ,所以在PointQuadTree初始化之后无法设置它。 Also Bounds has a constructor with parameters only. Bounds也有一个只带参数的构造函数。

After PointQuadTree is initialized, I need it to sit in memory and need to be able to autowire it in other components to read/remove/add items. PointQuadTree初始化之后,我需要它在内存中,并且需要能够在其他组件中自动装配它以读取/删除/添加项目。 I have no idea how to do this with Spring Boot. 我不知道如何使用Spring Boot执行此操作。 Any help greatly appreciated. 任何帮助非常感谢。

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 并使用@Autowired将此bean注入所需的位置

Here ${minx:100} , tries to read from properties file, if not specified takes the default as 100 这里${minx:100} ,尝试从属性文件中读取,如果未指定,则默认为100

in some configuration file create a spring bean of tree, something like this: 在一些配置文件中创建一个tree bean的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"); 而不是double minX = env.getProperty("minX"); you can create fields with @Value , like @chrylis said in comment: 你可以使用@Value创建字段,就像@chrylis在评论中所说:

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

then use it field to create bean. 然后使用它来创建bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM