当给属性赋值的时候,必须提供该属性的set 方法。
Address.java
package com.momo.entity; public class Address { private String address; public Address() { } public Address(String address) { this.address = address; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } @Override public String toString() { return "Address{" + "address='" + address + ''' + '}'; } }
Students.java
package com.momo.entity; import java.util.*; public class Student { private String name; private Address outBean; // 外部bean private Address innerBean;// 内部bean private String[] array; private Listlist; private Map map; private Set set; private String emptyValue; // 注入一个 空字符串 private String nullValue; // 注入一个null 值 private Properties props; @Override public String toString() { return "Student{" + "name='" + name + ''' + ", outBean=" + outBean + ", innerBean=" + innerBean + ", array=" + Arrays.toString(array) + ", list=" + list + ", map=" + map + ", set=" + set + ", emptyValue='" + emptyValue + ''' + ", nullValue='" + nullValue + ''' + ", props=" + props + '}'; } }
beans.xml
Mybatis Spring SpringMVC
Mybatis Spring SpringMVC Mybatis Spring SpringMVC 1001 男 张三
测试
package com.momo.test; import com.momo.entity.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Mytest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student s = (Student) context.getBean("s"); System.out.println(s); } }通过构造方法进行注入
传统的构造方法可以给属性赋值。
this.xxx = xxx; // 手动赋值。需要手动传参。
实体类:
package com.momo.entity; import java.util.Date; public class User { private String name; private Integer age; private Date birthday; public User(){ System.out.println("无参数的构造方法"); } public User(Integer age){ System.out.println("有一个Integer类型的参数的构造"); this.age = age; } public User(String name){ System.out.println("有一个String类型的参数的构造"); this.name = name; } public User(String name,Integer age){ System.out.println("有2个String类型和Integer类型的参数的构造"); this.age = age; this.name = name; } public User(String name, Integer age, Date birthday) { System.out.println("3个参数参数的构造方法"); this.name = name; this.age = age; this.birthday = birthday; } @Override public String toString() { return "User{" + "name='" + name + ''' + ", age=" + age + ", birthday=" + birthday + '}'; } }
1.通过下标
index 属性 : 就是第几个属性。 给对应的属性赋值的时候,会默认的找到适合当前赋值的构造。
2.通过参数类型
可以用,但是不推荐使用。
3.通过参数的名称
通过p命名空间来注入
本质是 通过调用 set 方法和构造方法来进行注入
1.通过p命名空间来注入,需要在beans.xml 中导入 p 命名空间。
在文件的开头加入约束: xmlns:p="http://www.springframework.org/schema/p"
通过c命名空间来注入总结: 1.p命名空间注入的时候,利用类的set方法 2.注入的时候分2种情况 1).基本类型和String--->在bean的标签上加上属性 p:需要注入的属性的属性名="值" 2).注入外部的对象---->在bean的标签上加上属性 p:需要注入的属性的属性名-ref = "外部bean的id"
本质上还是使用构造方法来注入。
导入约束 xmlns:c="http://www.springframework.org/schema/c"
注意: 需要提供 name的构造 public User(String name){ System.out.println("有一个String类型的参数的构造"); this.name = name; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)