Spring框架学习笔记4
程序员文章站
2022-06-04 19:37:43
...
SpringDI
属于SpringIOC的内容
1为什么学习DI?
给创建好的对象的全局成员属性或对象赋值操作;
主要是给jar包里面的属性赋值!!!
2DI方式
A 使用set方法
B 有参构造
C 自动注入
3实现
Clazz.java
package SpringDI;
public class Clazz {
private String classNo;
private String className;
public Clazz(String classNo, String className) {
this.classNo = classNo;
this.className = className;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "Clazz{" +
"classNo='" + classNo + '\'' +
", className='" + className + '\'' +
'}';
}
public Clazz() {
}
}
Student.java
package SpringDI;
public class Student {
private String stuNo;
private String stuName;
private Clazz cla;
public Student(String stuNo, String stuName, Clazz cla) {
this.stuNo = stuNo;
this.stuName = stuName;
this.cla = cla;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Clazz getCla() {
return cla;
}
public void setCla(Clazz cla) {
this.cla = cla;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"stuNo='" + stuNo + '\'' +
", stuName='" + stuName + '\'' +
", cla=" + cla +
'}';
}
}
Test.java
package SpringDI;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)applicationContext.getBean("stu");
System.out.println(stu);
System.out.println("-----------------------------------");
Student stu2 = (Student)applicationContext.getBean("stu2");
System.out.println(stu2);
}
}
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">
<bean id="cla" class="SpringDI.Clazz">
<constructor-arg name="classNo" value="20"></constructor-arg>
<constructor-arg name="className" value="二十"></constructor-arg>
</bean>
<bean id="stu" class="SpringDI.Student">
<!-- 构造器参数 -->
<!-- 位置无所谓 -->
<!-- name 对应构造方法的形参名 value 对应实参名-->
<constructor-arg name="stuNo" value="25"></constructor-arg>
<constructor-arg name="stuName" value="小王"></constructor-arg>
<constructor-arg name="cla" ref="cla"></constructor-arg>
</bean>
<bean id="stu2" class="SpringDI.Student">
<property name="cla" ref="cla"></property>
</bean>
</beans>
解释:
注入方式之一是有参构造方法,上面使用的 constructor-arg就是。name与形参一致。
另一种方式是set方法,下面property就是。name与属性名一致。更加常用!!
给jar包赋值举例
<bean id="beanMetaDataAttribute" class="org.springframework.beans.BeanMetadataAttribute">
<property name="source" ref="a"></property>
</bean>
上一篇: 一、Vue快速入门