代码之家  ›  专栏  ›  技术社区  ›  Asif Mushtaq

不能自动关联所谓的简单属性,如原语、字符串和类

  •  0
  • Asif Mushtaq  · 技术社区  · 8 年前

    我是春天的新手,希望学习细节。我在读关于自动布线的书 You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties) here .

    1) 我不明白这是什么意思 Classes 在这里

    2) 他们说你不能自动连线原始人但我试过了 Integer 包装器类,它们仍然不工作,为什么?请参见下面的代码。

    豆:

    <bean id="teacher" class="com.climesoft.webapp.model.Teacher"
            autowire="byName">
    </bean>
    <bean id="id" class="java.lang.Integer">
                <constructor-arg value="20" />
    </bean>
    

    Java类:

    public class Teacher {
    
        private String name;
    //  private int id;
        private Integer id; // for autowiring
        private String phone;
        private Course course;
    
    
        public Course getCourse() {
            return course;
        }
    
        public void setCourse(Course course) {
            this.course = course;
        }
    
        public Teacher() {
            System.out.println("Calling Teacher No Param Constructor");
        }
    
        public Teacher(String name, Integer id, String phone) {
            this.name = name;
            this.id = id;
            this.phone = phone;
    
            System.out.println("Calling Teacher Param Constructor");
        }
    
        public String getName() {
            System.out.println("Calling Teacher getName");
            return name;
        }
        public void setName(String name) {
            System.out.println("Calling Teacher setName");
            this.name = name;
        }
        public Integer getId() {
            System.out.println("Calling Teacher getId");
            return id;
        }
        public void setId(Integer id) {
            System.out.println("Calling Teacher setId");
            this.id = id;
        }
        public String getPhone() {
            System.out.println("Calling Teacher getPhone");
            return phone;
        }
        public void setPhone(String phone) {
            System.out.println("Calling Teacher setPhone");
            this.phone = phone;
        }
    
        @Override
        public String toString() {
            return "Teacher [name=" + name + ", id=" + id + ", phone=" + phone + ", course=" + course + "]";
        }
    
    
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Arman    8 年前

    这个句子中有一些并列关系的问题,可能会妨碍你正确理解它。

    可以理解为:

    不能自动关联所谓的简单属性,例如基本体, 字符串,以及此类简单属性的类(和数组)。

    不管怎么说,它实际上意味着任何原语、字符串和原语类(即整数等包装类)、原语/字符串/包装数组都不能自动连接。

    从本质上讲,自动连接可以帮助我们连接用户定义(当然还有框架定义)的类,而由于涉及模糊性,所有上述内容在默认情况下都非常复杂。

        2
  •  0
  •   Swargam    5 年前
    • 不能自动关联(int、boolean、long、short、, 字节)等等。
    • 只能对对象类的子类(直接或间接)进行自动关联。

    因此,基本上,自动连接只是在依赖类中引用类的对象(或Bean)。作为int、double、byte,所有这些都不是对象,而是基本类型,它们不能自动连接。

    但是可以使用这些基本类型的对象计数器部分,比如 一串 , 整数 , 布尔值 你可以自动连线这些。

        3
  •  -1
  •   Pavel Gordon    8 年前

    实际上,您可以创建一个类类型为的bean:

    <bean id="myClass" class="java.lang.Class" factory-method="forName">
       <constructor-arg value="com.MyClass"/>
    </bean>
    

    您可以自动连线int:

    <bean id="autowiredInt" class="java.lang.Integer" factory-method="valueOf">
    <constructor-arg value="100"/></bean>
    

    然后:

    @Autowired @Qualifier("autowiredInt") 
    private int autowiredInt;
    

    或者你可以使用 @Value 注释。