Spring笔记
项目的类列表和xml
- 基于xml注入
SpringTest.java
- 注意: 在第二步获取Bean时的两种方法,一种是通过配置文件中的Bean的id,另一种是通过全类名。通过全类名获取Bean时,需要注意配置文件中只允许有一个与之类型相匹配的Bean。
package Spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
//1,创建Spring的IOC容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//2,从IOC容器中获取Bean实例
//根据配置文件的Bean的id获取
Student student = (Student)context.getBean("student2");
//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常.
//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个.
Student student = context.getBean(Student.class);
//3,调用方法
student.intruduction();
Teacher teacher1 = (Teacher)context.getBean("teacher1");
Teacher teacher2 = (Teacher)context.getBean("teacher2");
Teacher teacher3 = (Teacher)context.getBean("teacher3");
System.out.println(teacher1);
System.out.println(teacher2);
System.out.println(teacher3);
}
}
Student.java
package Spring;
public class Student {
private String name;
public Student() {
}
public Student(String name) {
super();
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void intruduction() {
System.out.println("My name is " + name);
}
}
Teacher.java
- 设置了不同的构造器方便后面测试。
package Spring;
public class Teacher {
private String name;
private int age;
private double salary;
private String sex;
public Teacher(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Teacher(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + ", salary=" + salary + ", sex=" + sex + "]";
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
ApplicationContext.xml
- 注入方式: 分别有setter注入和构造注入。
- setter注入: setter注入要求所注入的类中必须要有空的构造器
- 构造注入: 在构造注入时,如果有多个构造器,可以使用value、index(标记顺序)、type(标记类型)来准确定位所使用的构造器。
<?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 -->
<!-- 通过setter注入(必须要用空构造器) -->
<bean id="student" class="Spring.Student">
<property name="name" value="HelloWorld"></property>
</bean>
<!-- 通过构造注入 -->
<bean id="student2" class="Spring.Student">
<constructor-arg value="HelloWorld2"></constructor-arg>
</bean>
<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
<!-- 可以根据 index、value的类型、type指定类型进行更加精确的定位. -->
<bean id="teacher1" class="Spring.Teacher">
<constructor-arg value="Mike"></constructor-arg>
<constructor-arg value="18"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<bean id="teacher2" class="Spring.Teacher">
<constructor-arg value="18" index="1"></constructor-arg>
<constructor-arg value="Mike" index="0"></constructor-arg>
<constructor-arg value="男" index="2"></constructor-arg>
</bean>
<bean id="teacher3" class="Spring.Teacher">
<constructor-arg value="18" type="int"></constructor-arg>
<constructor-arg value="Mike" type="java.lang.String"></constructor-arg>
<constructor-arg value="10000" type="double"></constructor-arg>
</bean>
</beans>
结果
My name is HelloWorld2
Teacher [name=Mike, age=18, salary=0.0, sex=男]
Teacher [name=Mike, age=18, salary=0.0, sex=男]
Teacher [name=Mike, age=18, salary=10000.0, sex=null]