【发布时间】:2017-03-26 01:19:45
【问题描述】:
Spring 和 JPA 新手
我有一个以下控制器类
public class RegisterStudent{
private StudentInfo studentInfo;
private RegisterStudentHelper studentHelper;
public void registerStudentProcess(String name,String regNo,String course)
{
// // Creating an Instance of StudentInfo Class
studentInfo = new StudentInfo();
studentInfo.setName(name);
studentInfo.setRegNo(regNo);
studentInfo.setCourse(course);
// set the StudentInfo to the RegisterStudentHelper constructor
studentHelper = new RegisterStudentHelper(studentInfo);
studentHelper.doRegisterStudentProcess();
}
}
我正在尝试在使用 Spring 、 JPA 和 hibernate 时转换相同的模型,看起来像这样
@Controller
public class RegisterStudent{
@Autowired
private StudentInfo studentInfo;
@Autowired
private RegisterStudentHelper studentHelper;
public void registerStudentProcess(String name,String regNo,String course)
{
studentInfo.setName(name);
studentInfo.setRegNo(regNo);
studentInfo.setCourse(course);
studentHelper = new RegisterStudentHelper(studentInfo);
studentHelper.doRegisterStudentProcess();
}
}
如何使用 Annotations 将 studentInfo 设置为 registerStudentHelper 构造函数?
感谢任何想法或建议。
【问题讨论】:
-
请先修正错别字和代码缩进。然后坚持 Java 命名约定(类名大写,方法名小写),然后再次发布问题。请注意,根据定义,控制器是无状态类。
-
一般来说,Spring 不需要任何特殊的东西——只需创建一个将依赖项作为参数的构造函数即可。
-
@Roland illig 我的道歉。我现在已经更正了。你能详细说明控制器无状态类吗?
-
按这些类的名称,我认为 StudentInfo 不应该是一个 bean,也不认为它应该是自动装配的。也不要实例化 RegisterStudentHelper 是你已经在注入它
标签: java spring hibernate spring-mvc jpa