简体   繁体   English

在Spring中,如何从配置中创建具有默认值的POJO?

[英]In Spring, how do I create a POJO with a default value from configuration?

I am using the Spring framework to with a properties file that has the key: 我正在使用Spring框架来处理具有关键字的属性文件:

empty.value = -

I would like to be able to create a POJO that would have that empty value as its default value for the default constructor of the POJO. 我希望能够创建一个POJO,该POJO会将空值作为POJO的默认构造函数的默认值。 Something like this: 像这样:

public class Dog {

    private String name;

    public Dog(){
    }

    public Dog(String name){
        this.name = name;
    }
}

So that if Dog d = new Dog() is called, d.name will be - from the empty.value key. 所以,如果Dog d = new Dog()被调用时, d.name-empty.value关键。

Autowiring the name variable will just be null because Spring isn't aware of the POJO as well as using @Value("${empty.value}") . 自动装配name变量将为null因为Spring不了解POJO,也不使用@Value("${empty.value}")

What would be the best way of achieving this? 实现这一目标的最佳方法是什么?

Edit : To clarify, there are meant to be many Dog s throughout the system and I need to be able to initialize them on request. 编辑 :澄清一下,整个系统中有很多Dog ,我需要能够根据要求初始化它们。

Edit 2 : Consider the following use case- I have an interface containing a method: 编辑2 :考虑以下用例-我有一个包含方法的接口:

Dog transform(List<String> animals);

I would like to be able to know that whatever Dog is returned from the transform method has either - (configurable) as a name or a different value passed in to the constructor when created (or setter, later). 我希望能够知道,从transform方法返回的任何Dog都具有- (可配置)作为name或者在创建时(或稍后使用setter)传递给构造函数的其他值。 I would like to ensure that for whatever transform does in any implementation, even if it fails, I will have a valid Dog with a name as mentioned. 我想确保无论在任何实现中进行任何transform ,即使transform失败,我都会拥有一个有效的Dog ,其name如上所述。

If you are using the Spring framework, you have to create all your Spring beans using also Spring. 如果使用Spring框架,则必须同时使用Spring创建所有Spring Bean。 For example: 例如:

Dog d = (Dog)context.getBean("dog");

Doing Dog d = new Dog() is a valid instance, but that way it's not processed by Spring. Dog d = new Dog()是有效的实例,但是那样Spring不会对其进行处理。

Update about the Edit 2 有关编辑2的更新

If you want to ensure you always have a valid dog instance when calling the transform method, you can use a try catch block in the caller code. 如果要确保在调用transform方法时始终具有有效的dog实例,则可以在调用者代码中使用try catch块。 For example: 例如:

// caller class
Dog dog = null;
try{
   dog = yourInterface.transform(animals);
   if (dog == null || !validName(dog.name)){
       throw new RuntimeException("Not a valid dog/name");   
   }
}catch(Exception ex){
   dog = (Dog)context.getBean("dog");
}

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

相关问题 如何使用Spring / Thymeleaf表单将POJO添加到列表中? - How do I add a POJO to a list using Spring/Thymeleaf form? 如何根据 POJO 中的另一个值更改变量的值? - How do I change the value of a variable based on another value in POJO? 我如何使用动态密钥为 json 创建 POJO 类以进行改造? - how do i create POJO class for json with dynamic keys for retrofit? 如何从Java中的xml实体字符串值创建不同的pojo - How to create a different pojo from xml entity string value in java 如何从此示例JSON创建POJO? - How can I create a POJO from this sample JSON? 以编程方式将POJO转换为@Configuration Spring - Turn POJO to @Configuration programmatically Spring 如何利用 json 映射文件从一个 pojo 转换为另一个 pojo? - How do I leverage a json mapping file to convert from one pojo to another pojo? PropertiesLoaderUtils.loadProperties 如何在运行时使用 Spring 映射到配置 POJO? - How PropertiesLoaderUtils.loadProperties mapped to Configuration POJO using Spring at Runtime? 从pojo生成JsonSchema:如何自动添加“description”? - Generating JsonSchema from pojo: how do I add “description” automatically? 如何从Android的POJO类中获取项目? - How do I get an item from POJO classes in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM