Spring 为外部对象内部的对象集合添加默认值

Spring 为外部对象内部的对象集合添加默认值,spring,spring-boot,Spring,Spring Boot,我正在尝试向属性属性添加默认值。我有一个类,其中有另一个类类型作为列表注入。 我能够获得所有属性的默认值,即使是依赖类。我想知道是否有任何方法可以使用@value添加自定义对象的默认值列表。 我的模型课是- package com.example.test.Model; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org

我正在尝试向属性属性添加默认值。我有一个类,其中有另一个类类型作为列表注入。 我能够获得所有属性的默认值,即使是依赖类。我想知道是否有任何方法可以使用@value添加自定义对象的默认值列表。 我的模型课是-

package com.example.test.Model;

    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Employee {
     
        @Value("1")
        private Integer id;
        @Value("Anubham")
        private String name;
        
        @Autowired
        private List<Departments>departments;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Departments> getDepartments() {
            return departments;
        }
    
        public void setDepartments(List<Departments> departments) {
            this.departments = departments;
        }
        
    
        public Employee() {
            super();
        }
        
        
    
        public Employee(Integer id, String name, List<Departments> departments) {
            super();
            this.id = id;
            this.name = name;
            this.departments = departments;
        }
    
        @Override
        public String toString() {
            return "Employee [id=" + id + ", name=" + name + ", departments=" + departments + "]";
        }
        }
Another one is:
package com.example.test.Model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Departments {
    
    @Value("1")
    private int id;
    
    @Value("computer")
    String subject;

    public Departments() {
        super();
    }

    public Departments(int id, String subject) {
        super();
        this.id = id;
        this.subject = subject;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return "Departments [id=" + id + ", subject=" + subject + "]";
    }
}
package com.example.test.Model;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.stereotype.Component;
@组成部分
公营雇员{
@价值(“1”)
私有整数id;
@价值(“Anubham”)
私有字符串名称;
@自动连线
私营部门;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共部门名单({
返回部门;
}
公共部门(列出部门){
这个.部门=部门;
}
公职人员(){
超级();
}
公共员工(整数id、字符串名称、列出部门){
超级();
this.id=id;
this.name=名称;
这个.部门=部门;
}
@凌驾
公共字符串toString(){
返回“Employee[id=“+id+”,name=“+name+”,departments=“+departments+””;
}
}
另一个是:
包com.example.test.Model;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.stereotype.Component;
@组成部分
公共课系{
@价值(“1”)
私有int-id;
@价值(“计算机”)
字符串主题;
公共部门(){
超级();
}
公共部门(int-id,字符串主题){
超级();
this.id=id;
this.subject=主语;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getSubject(){
返回主题;
}
public void setSubject(字符串主题){
this.subject=主语;
}
@凌驾
公共字符串toString(){
返回“部门[id=“+id+”,主题=“+subject+”]”;
}
}
我作为员工[id=1,name=Anubham,departments=[departments[id=1,subject=computer]]获得输出。 我想再多留一张部门记录。 我想知道是否可以在不使用任何其他方法的情况下使用@value。

在您的示例中,“部门”是一个注入员工bean的bean。如果您想要有多个部门,您必须创建接口/抽象“部门”,并使用您想要的具体值(部门A、部门B)在bean中实现它

但值注释并不意味着注入静态内容,而是从属性文件中注入值。我不知道你想通过这种方式实现什么