Spring 应用程序
1、创建 Maven 工程。
2、pom.xml 中引入 Spring 依赖。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
Spring 的两大核心机制:IoC(控制反转) 和 AOP(面向切面编程)
IoC 对象创建不再由开发者完成,而是容器自动创建,开发者直接取出来用即可。
3、创建 spring.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="stu" class="com.southwind.entity.Student" >
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
</bean>
</beans>
4、加载 IoC 容器,获取创建好的 bean。
public class IoCTest {
public static void main(String[] args) {
//1.加载 IoC 容器,spring.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean("stu");
System.out.println(student);
}
}
特殊字符的处理方式。
<?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="stu" class="com.southwind.entity.Student" >
<property name="id" value="1"></property>
<!-- <property name="name" value="<张三>"></property>-->
<property name="name">
<value><![CDATA[<张三>]]></value>
</property>
<property name="age" value="22"></property>
</bean>
</beans>
使用 IoC 容器创建对象,实体类的注意事项:
- 必须有无参构造函数。
- 成员变量必须有 setter 方法。
IoC 实例化对象的过程,通过反射+XML解析的方式对 spring.xml 进行处理,反射拿到无参构造函数,调用创建对象,同时获取 setter 方法,调用完成成员变量的赋值。
获取 IoC bean 的方式
- 通过 id 获取
Student student = (Student) applicationContext.getBean("stu");
- 通过运行时类获取
Student student = applicationContext.getBean(Student.class);
通过运行时类获取 bean 存在一个弊端,当 spring.xml 中配置两个 Student 的 bean 时会抛出异常。
创建 IoC bean 的方式
- 无参构造
<bean id="stu2" class="com.southwind.entity.Student">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="23"></property>
</bean>
- 有参构造
1、在实体类中创建有参构造。
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
2、在 spring.xml 中进行配置。
<bean id="stu3" class="com.southwind.entity.Student">
<constructor-arg index="1" value="王五"></constructor-arg>
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="2" value="20"></constructor-arg>
</bean>
<bean id="stu3" class="com.southwind.entity.Student">
<constructor-arg name="name" value="王五"></constructor-arg>
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
</bean>
<bean id="stu3" class="com.southwind.entity.Student">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="王五"></constructor-arg>
<constructor-arg value="20"></constructor-arg>
</bean>
两个 bean 的级联
package com.southwind.entity;
public class Student {
private Integer id;
private String name;
private Integer age;
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", classes=" + classes +
'}';
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
}
}
package com.southwind.entity;
public class Classes {
private Integer id;
private String name;
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;
}
@Override
public String toString() {
return "Classes{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
<bean id="stu2" class="com.southwind.entity.Student">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="23"></property>
<property name="classes" ref="classes1"></property>
</bean>
<bean id="classes1" class="com.southwind.entity.Classes">
<property name="id" value="1"></property>
<property name="name" value="Java班"></property>
</bean>