【问题标题】:Constructor Injection in Spring / JPA / HibernateSpring / JPA / Hibernate中的构造函数注入
【发布时间】: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


【解决方案1】:

首先关注 Roland Illig 的评论!

Spring(和其他一些 DI 框架)允许 3 种类型的依赖注入:

1.属性注入

@Autowired
private AnyBeanClazz anyBean;

2.方法注入

@Autowired
public void injectedMethod(AnyBeanClazz anyBean){
    // Do anything here
}

3.构造函数注入

class OtherBeanClazz{
    @Autowired
    public OtherBeanClazz(AnyBeanClazz anyBean){}
}

【讨论】:

    【解决方案2】:

    查看您的示例后,我可以假设 StudentInfo 是一个值对象,而 RegisterStudentHelper 是服务。 由于值对象包含状态,因此它们不是依赖注入的最佳候选者。您应该将其创建为新的并将其作为参数传递给 doRegisterStudentProcess() 方法。

    为了您的理解,我找到了这篇文章: https://www.bennadel.com/blog/2377-creating-service-objects-and-value-objects-in-a-dependency-injection-di-framework.htm

    【讨论】:

      【解决方案3】:

      好吧,您的代码对我来说没有多大意义,因为在某些情况下您在 StudentInfo 上设置了值,如果您将其用作 @Component 来注入,默认情况下这将是一个单例类春天。

      但是,这里是使用构造函数注入的代码:

      @Controller
      public class RegisterStudent {
      
          private RegisterStudentHelper studentHelper;
          private StudentInfo studentInfo;
      
          @Autowired
          RegisterStudent(StudentInfo studentInfo ) {
              this.studentInfo = studentInfo;
              this.studentHelper = studentHelper;
          }
      
      }
      

      如果你愿意,你可以省略 @Autowired,因为 Spring 足够聪明,知道如果 Controller(或任何 @Component)只有一个构造函数,他会自动使用注入依赖项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 2021-08-14
        相关资源
        最近更新 更多