赞
踩
实体类:
public class User { private String name; public User() { System.out.println("无参构造方法"); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
bean配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.lmy.pojo.User">
<property name="name" value="zhangSan"/>
</bean>
</beans>
测试:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
}
}
输出:
无参构造方法
Process finished with exit code 0
当没有无参构造方法时:
public class User2 { private String name; public User2(String name) { this.name = name; System.out.println("有参构造方法"); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
配置报错:
总结: 默认的创建方式即通过无参构造方法创建,在getBean获取容器对象时对象已经创建了
实体类:
public class User2 {
private String name;
public User2(String name) {
this.name = name;
System.out.println("有参构造方法");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
bean配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 第一种根据index参数下标设置 --> <bean id="user2" class="com.lmy.pojo.User2"> <!-- index指构造方法 , 下标从0开始 --> <constructor-arg index="0" value="zhangSan2"/> </bean> <!-- 第二种根据参数名字设置 --> <bean id="user3" class="com.lmy.pojo.User2"> <!-- name指参数名 --> <constructor-arg name="name" value="zhangSan3"/> </bean> <!-- 第三种根据参数类型设置 --> <bean id="user4" class="com.lmy.pojo.User2"> <constructor-arg type="java.lang.String" value="zhangSan4"/> </bean> </beans>
测试:
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
User2 user2 = (User2) context.getBean("user2");
User2 user3 = (User2) context.getBean("user3");
User2 user4 = (User2) context.getBean("user4");
System.out.println(user2);
System.out.println(user3);
System.out.println(user4);
}
}
结果:
有参构造方法
有参构造方法
有参构造方法
User{name='zhangSan2'}
User{name='zhangSan3'}
User{name='zhangSan4'}
Process finished with exit code 0
结论:
在配置文件加载的时候。其中管理的对象都已经初始化了!
注意:
注意:要求被注入的属性 , 必须有set方法, set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is 。
实体类:
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbies() { return hobbies; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbies=" + hobbies + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
配置类:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.lmy.pojo.Address"> <property name="address" value="重庆"/> </bean> <bean id="student" class="com.lmy.pojo.Student"> <!--常量注入--> <property name="name" value="zhangSan"/> <!--Bean注入,这里的值是一个引用,ref--> <property name="address" ref="address"/> <!--数组注入--> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!--List注入--> <property name="hobbies"> <list> <value>唱歌</value> <value>跳舞</value> </list> </property> <!--Map注入--> <property name="card"> <map> <entry key="手机卡" value="12345678912"/> <entry key="身份证" value="123456789123456789"/> </map> </property> <!--set注入--> <property name="games"> <set> <value>GTA5</value> <value>王者荣耀</value> </set> </property> <!--Null注入--> <property name="wife"> <null/> </property> <!--Properties注入--> <property name="info"> <props> <prop key="学号">123</prop> <prop key="性别">男</prop> </props> </property> </bean> </beans>
测试:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
结果:
Student{name='zhangSan', address=Address{address='重庆'}, books=[红楼梦, 西游记, 水浒传, 三国演义], hobbies=[唱歌, 跳舞], card={手机卡=12345678912, 身份证=123456789123456789}, games=[GTA5, 王者荣耀], wife='null', info={学号=123, 性别=男}}
Process finished with exit code 0
实体类:
public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 需要导入约束 : xmlns:p="http://www.springframework.org/schema/p" P(属性: properties)命名空间 , 属性依然要设置set方法 --> <bean id="user" class="com.lmy.pojo.User" p:name="zhangShan" p:age="21"/> <!-- 导入约束 : xmlns:c="http://www.springframework.org/schema/c" C(构造: Constructor)命名空间 , 属性依然要设置有参构造方法 --> <bean id="user1" class="com.lmy.pojo.User" c:name="lisi" c:age="21"/> </beans>
测试:
public class MyUserTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user1");
System.out.println(user.toString());
}
}
输出:
User{name='lisi', age=21}
Process finished with exit code 0
结论:
P(属性: properties)命名空间注入即类似于set注入
C(构造: Constructor)命名空间注入即类似于有参构造器注入
注意:
有参构造器注入与set注入不能同时用于同一个实体类。因为实体类中有有参构造器后只能使用有参构造器注入,不能使用set注入
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。