一个java对象肯定有属性方法
怎么给一个对象的属性给属性赋值
在spring配置文件中,给java对象的属性赋值
**di:依赖注入**,表示创建对象给属性赋值
di的实现语法有俩种
di的语法分类:
- set注入(设置注入): spring调用类的set方法,在set方法中可以实现属性的赋值
- 80%左右都是使用set注入
- 构造注入,spring调用类的有参数构造方法,创建对象。在构造方法中完成赋值
public class Student {private Integer id;private String name;private int age;public Student() {}public Student(Integer id, String name, int age) {this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}}
基于XML的DI(理解,用的比较少)
我们在spring的配置开发中有默认的配置名称(选择的是spring config创建):applicationContext.xml
<?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"><!--声明student对象--><bean id="myStudent" class="org.chentianyu.users.Student" /></beans>
xml文件修改(applicationContext.xml)
<?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">
<!--
声明student对象
注入: 就是赋值的意思
简单类型: spring中规定java的基本类型和String字符串都是简单类型
di: 给属性赋值
1.set注入(设置注入): spring调用类的set方法,你可以在set方法中完成属性赋值
1)简单类型的set注入
<bean id="xx" class="yyy">
<property name="属性名字" value="属性的值" />
一个property只能给一个属性赋值
<property name="属性名字" value="属性的值"
</bean>
-->
<bean id="myStudent" class="org.chentianyu.users.Student" >
<property name="name" value="李四" />
<property name="age" value="20" />
<property name="id" value="1" />
</bean>
</beans>
若是把applicationContext.xml放在resources目录下的一个名叫chen的目录下,config路径得是String config = "chen/applicationContext.xml";
这个是基于对象的set方法来传参的如果我们去除了setName方法的话
Bean property 'name' is not writable or has an invalid setter method. Did you mean 'age'?
User中的name属性没有setter方法
设值注入只有使用set方法
如果写一个不存在参数的setter方法
public void setEmail(String email){
System.out.println("setEmail="+email);
}
<bean id="myStudent" class="org.chentianyu.users.Student" >
<property name="name" value="李四" /><!--setName("李四")-->
<property name="age" value="20" /><!--setAge(20)-->
<property name="id" value="1" /><!--setId(1)-->
<property name="email" value="123@qq.com" />
</bean>
@Test
public void test05(){
String config = "chen/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
//从容器中获取Student对象
Student myStudent = (Student) applicationContext.getBean("myStudent");
System.out.println("Student对象=" + myStudent);
}
setEmail=123@qq.com
Student对象=Student{id=1, name='李四', age=20}
引用类型的设值注入
编写一个School.java
package org.chentianyu.ba02;public class School { private String name; private String address; public School() { } public School(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + '\'' + ", address='" + address + '\'' + '}'; }}
在Student.java类中引用
package org.chentianyu.users;import org.chentianyu.ba02.School;public class Student { private Integer id; private String name; private int age; private School school; public Student() { } public Student(Integer id, String name, int age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setEmail(String email){ System.out.println("setEmail="+email); } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; }}
那我要创建Student类怎么做?
先新建一个测试类
package org.chentianyu;import org.chentianyu.ba02.School;import org.chentianyu.users.Student;import org.junit.Test;public class AppTest02 { @Test public void testSpringStudent(){ //手动创建 Student student = new Student(); student.setName("李四"); student.setAge(20); School school = new School(); school.setName("杭州职业技术学院"); school.setAddress("杭州下沙"); student.setSchool(school); System.out.println(student); }}
运行结果
Student{id=null, name='李四', age=20, school=School{name='杭州职业技术学院', address='杭州下沙'}}
beans.xml(Spring Config)
<?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"> .... <!-- 引用类型的注入: set调用类的set方法 <bean id="xxx" class="yyy" > <property name="属性名称" ref="bean的id(bean对象的名称)" /> </bean> --> <bean id="myStudent" class="org.chentianyu.ba02.Student"> <property name="name" value="李四" /> <property name="age" value="22" /> <!--引用类型--> <property name="school" ref="mySchool" /> </bean> <!--声明School对象--> <bean id="mySchool" class="org.chentianyu.ba02.School"> <property name="name" value="杭州职业技术学院" /> <property name="address" value="杭州下沙" /> </bean></beans>
@Testpublic void testSpringStudent(){ String config = "chen/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config); Student myStudent = (Student) applicationContext.getBean("myStudent"); System.out.println(myStudent);}
构造注入
package org.chentianyu.ba02;public class Student { private Integer id; private String name; private int age; private School school; public Student() { System.out.println("无参构造方法执行了"); } public Student(Integer id, String name, int age) { System.out.println("有参构造方法执行了"); this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setEmail(String email){ System.out.println("setEmail="+email); } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; }}
bean01>applicationContext.xml
<?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"> <!-- 构造注入: spring调用类有参数的构造方法,在创建的同时,在构造方法中给属性赋值 构造注入使用的是:<constructor-arg>标签 <constructor-arg>标签: 一个<constructor-arg>表示一个构造方法一个参数 name: 表示构造方法的形参名 index: 表示构造方法的参数位置,参数从左到右位置是0,1,2的顺序 value: 如果构造方法的形参类型是简单类型,使用value; ref: 构造方法的形参类型是引用数据类型,使用ref --> <bean id="myStudent" class="org.chentianyu.ba02.Student"> <constructor-arg name="id" index="0" value="3" /> <constructor-arg name="name" index="1" value="我" /> <constructor-arg name="age" index="2" value="20" /> <constructor-arg name="school" index="3" ref="mySchool" /> </bean> <bean id="mySchool" class="org.chentianyu.ba02.School"> <property name="name" value="杭州职业技术学院" /> <property name="address" value="杭州下沙" /> </bean></beans>
AppTest02.java
@Testpublic void testGZ(){ String config = "bean01/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config); Student myStudent = (Student) applicationContext.getBean("myStudent"); System.out.println(myStudent);}
基于注解的DI(掌握)
请看**注解**栏
