我有Employee类,其中包含实用程序方法。
@Service
public class Employee {
@Value("${Employee.name}")
private String firstName;
List<String> employees = Arrays.asList(firstName);
public List<String> allEmplyees() {
System.out.println("First Name ::" + firstName);
return employees;
}
public int numberOfEmployees() {
return employees.size();
}
}
我正在读取Employee类中的属性文件并在方法中使用值。
我有第二类消费者,该类自动装配Employee类并调用其方法。
问题:当我从Consumer类调用allEmployees方法时,我
得到了[null]。例:
消费阶层
@RestController
public class StudentController {
@Autowired
private Employee employee;
@RequestMapping(method = RequestMethod.GET, value = "/test")
public String testMe(){
return employee.allEmplyees().toString();
}
}
我做错了什么,请帮忙!
List<String> employees = Arrays.asList(firstName);
Arrays.asList()将返回一个ArrayList,它是Arrays内部的私有静态类,而不是java.util.ArrayList类。要添加更多内容,@Value
一旦创建了bean ,便会处理注释。因此您可以@PostConstruct
在spring注入@Value
字段或将其作为构造函数的参数后,用于设置实例变量。
private List<String> employees = new ArrayList<>();
@PostConstruct
public void init() {
this.employees.add(firstName);
}
要么
private String firstName;
List<String> employees = null;
public Employee(@Value("${Employee.name}") String name) {
this.firstName = name;
this.employees = Arrays.asList(firstName);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句