简体   繁体   English

如何在 spring 中使用依赖注入将类属性传递值分配给类构造函数?

[英]How to assign a class property passing value to class constructor using Dependency injection in spring?

I am kind of super new to spring.我对春天有点陌生。 I need to assign a value to my class property using the constructor.我需要使用构造函数为我的类属性分配一个值。 I am using the class constructor method in spring dependency injection to do that.我在 spring 依赖注入中使用类构造函数方法来做到这一点。 How to configure bean XML to do that.如何配置 bean XML 来做到这一点。

MyBean1.class: MyBean1.类:

  public class MyBean1{
      private MyBean2 bean2;
      public MyBean1(MyBean2 bean2){
         this.bean2=bean2;
      }
  }

XML: XML:

  <!-- Definition for bean1 -->
   <bean id = "bean1" class = "com.MyBean1">
      <constructor-arg ref = "bean2"/>
   </bean>

   <!-- Definition for bean2 -->
   <bean id = "bean2" class = "com.MyBean2"></bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring依赖注入到具有构造函数的类 - Spring dependency injection to a class with a constructor Spring依赖注入:如何基于系统属性实例化类? - Spring Dependency Injection: How to Instantiate Class based on system property? 在具有参数化构造函数的类上使用依赖注入 - Using dependency injection on class having parameterized constructor 如何在抽象 class 中使用构造函数注入注入特定依赖项? - How to inject specific dependency using constructor injection in an abstract class? 使用Spring依赖注入时如何检查类是否在构建路径上 - How to check that a Class is on Build Path when using Spring dependency injection 如何使用依赖注入在Spring中注入资源类实例 - How to inject resource Class instance in Spring using Dependency injection Spring依赖项注入如何在带有@Component注释但没有构造函数注释的类中工作? - How does Spring dependency injection work inside a class that is annotated @Component but any without constructor annotation? 使用Spring框架的构造方法依赖注入,无法保留该值 - Constructor Dependency Injection using Spring framework, unable to retain the value 使用构造函数注入模拟 spring 类 - Mocking a spring class with constructor injection JUnit:如何通过构造函数创建具有依赖注入的测试类 - JUnit: How to create a test class with dependency injection via the constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM