【发布时间】:2015-02-20 00:19:24
【问题描述】:
没有 IOC 容器的正常方法是:
new User("Names", 22);
这里的参数值是动态的,例如,它们是通过用户提交表单获取的,因此不能存储在文件中。
TextField userNames = new TextField();
names = userNames.getText()
其他参数相同。
地点:
@Component
public class User {
public User(String names, int age) {
.
.
.
}
}
如何初始化User,同时将构造函数的参数(其中用户为Autowired)传递到另一个类:
@Component
public class AnotherClass {
@Autowired
User user(....)????? // How do I do it here
.
.
.
}
【问题讨论】:
-
您只需要 @Component 注释,其余的由 spring 处理。确保您已启用 ComponentScan see here
-
请看看我的更新@sol4me。我认为您的参考文献中没有提到这一点 - 例如,如何从 TextFields 收集值?
-
@user3663765 您正在从表单提交中获取值,在控制器层中您不能使用@ModelAttribute 吗?