全部章节 >>>>
本章目录
1.4 BeanFactory与ApplicationContext的区别
1.1 Spring框架基础
1.1.1 Spring特点及体系结构
Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。
Spring的核心是依赖注入、控制反转和面向切面编程。
Spring认为Java EE的开发应该更容易、更简单。
Spring的优势有以下几点:
- 方便解耦,简化开发。
- AOP编程的支持。
- 声明式事务的支持。
- 方便程序的测试。
- 方便集成各种优秀的框架。
- 降低Java EE API的使用难度。
1.1.1 Spring的体系结构
Spring的整个框架按其所属功能可以划分为七个主要组件。
1.1.2 使用Eclipse搭建Spring开发环境
打开Eclipse工具,创建Dynamic Web Project项目
在工程中,引入Spring的核心jar包
创建Spring核心配置文件applicationContext.xml
打开applicationContext.xml文件,引入beans约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 此空白处输入内容 -->
</beans>
1.1.3 简单的Spring依赖注入
在没有Spring框架之前,Java应用程序都是由多个类组成,它们之间相互协作来完成特定的业务逻辑。每个对象之间相互联系,导致了高度耦合的代码,不便于维护。而Spring框架的依赖注入使代码解耦合,便于维护。
在applicationContext.xml文件中使用bean元素注入对象:
在com.mhys.bean包下,创建一个Student.java类
public class Student {
private String stuId;
private String name;
// set()方法和get()方法省略
}
在applicationContext.xml文件中,注册Student类 在com.mhys.test包下,编写测试类,在控制台输出结果。
// 获取上下文
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取配置中Student实例
Student student = (Student) context.getBean("student");
// 输出结果
System.out.println(student);
运行程序,在控制台中,查看输出结果
<!-- id:代表实例化名 class:选择要实例化的类 -->
<bean id="student" class="com.mhys.bean.Student"></bean>
1.1.4 set注入
Spring框架提供了4种常用的注入方式,set注入、构造方法注入、p命名空间注入以及表达式spel方式注入,本节只讲解set注入的实现方法,其他注入方式后续会陆续讲到。
在applicationContext.xml配置文件中,不仅能完成对象的初始化。还可以动态配置属性信息,这也是Spring功能强大之所在。
该案例讲解的是通过set注入方式,使用property元素对类属性实现静态注入。
示例:在applicationContext.xml文件中使用bean元素注入对象
在Student类中注入name属性的属性值。
<bean id="student" class="com.mhys.bean.Student">
<!-- name:属性名 value:属性值 -->
<property name="name" value="Jack"></property>
</bean>
在测试类中调用getName()方法。
System.out.println(student.getName());
在控制台中,查看输出结果。
1.1.5 ref属性的作用及使用
有时在类与类之间存在依赖关系。例如,在一个类中对另一个类的引用。
Employee类引入Car类
public class Car {
private String brand;
private String carNo;
// set()方法和get()方法省略
}
public class Employee {
private String name;
private Car car;
// set()方法和get()方法省略
}
示例:在applicationContext.xml文件中使用bean元素注入对象
在com.mhys.bean包下,创建Phone类,并添加call()方法。
public class Phone {
private String message;
public void call(){
System.out.println(message);
}
// set()方法和get()方法省略
}
修改Student类,添加对Phone的依赖。
public class Student {
private String stuId;
private String name;
private Phone phone;
// set()方法和get()方法省略
}
在applicationContext.xml配置文件中,注册Phone类到容器,并在Student Bean中使用ref属性引用Phone Bean。
<bean id="phone" class="com.mhys.bean.Phone">
<property name="message" value="Good morning,Jack"></property>
</bean>
<bean id="student" class="com.mhys.bean.Student">
<property name="phone" ref="phone"></property>
</bean>
在TestMain测试类中,调用call()方法
System.out.println(student.getPhone().call());
在控制台中,查看输出结果
1.1.6 实践练习
1.2 通过工厂方式创建Bean对象
在Spring框架中,对Bean的创建方式有三种方式
- 静态工厂方法创建Bean
- 实例工厂方法创建Bean
- 构造方法创建Bean
通过简单的配置来完成Bean对象的实例化,其本质上是通过构造方法创建Bean实例,接下来学习使用静态、动态创建Bean对象的两种方法。
1.2.1 静态工厂创建Bean对象
示例:在applicationContext.xml文件中使用bean元素注入对象
在com.mhys.bean包下,创建User类,添加userName和password属性。
public class User {
private String userName;
private String password;
// set()和get()方法省略
}
在com.mhys.bean包下,创建UserFactory类,声明getInstance()方法。
public class UserFactory {
public static User getInstance(){
System.out.println("静态工厂方法创建对象!");
return new User();
}
}
在applicationContext.xml配置文件中,注册UserFactory类到容器。
<!-- 定义User Bean 由UserFactory工厂的getInstance()方法创建 -->
<bean id="user" class="com.mhys.bean.UserFactory" factory-method="getInstance"></bean>
在com.mhys.test包下,创建测试类 在控制台中,查看输出结果
public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
}
示例:在applicationContext.xml文件中使用bean元素注入对象
在UserFactory类,增加getUser()方法。
public User getUser(){
System.out.println("动态工厂方法创建对象!");
return new User();
}
在applicationContext.xml配置文件中,注入UserFactory类。
<!-- 配置工厂Bean -->
<bean id="userFactory" class="com.mhys.bean.UserFactory"></bean>
<!-- 声明由实例工厂userFactory的getUser()方法,创建User Bean对象-->
<bean id="user1" factory-bean="userFactory" factory-method="getUser" ></bean>
编写测试类。 在控制台中,查看输出结果
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) context.getBean("user1");
System.out.println(user1);
}
}
1.2.3 实践练习
1.3 Spring属性注入
在Spring框架中,属性的注入我们有多种方式,前面我们学过使用set方式注入,接下来我们学习构造方法注入属性、p命名空间注入属性和spel方式注入属性这三种属性注入方式。
1.3.1 构造方法注入属性
示例:使用constructor-arg元素来完成构造方法属性注入
在com.mhys.bean包下创建User类添加username和password属性并添加有参构造方法。
public class User {
private String username;
private String password;
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
// 有参构造方法
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User() {
super();
}
// set()方法和get()方法省略
}
在applicationContext.xml配置文件中,注册User类到容器。
<bean id="user" class="com.mhys.bean.User">
<!-- 构造方法注入属性 -->
<constructor-arg name="username" value="刘强"></constructor-arg>
<constructor-arg name="password" value="1q2w3e"></constructor-arg>
</bean>
在com.mhys.test包下,创建测试类 在控制台中,查看输出结果
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
}
1.3.2 p命名空间注入属性
示例:在applicationContext.xml配置文件中,通过p:属性名=属性值方式注入属性
在com.mhys.bean包下,创建Car类,首先,添加brand和carNo两个属性。
public class Car {
private String brand;
private String carNo;
@Override
public String toString() {
return "Car [brand=" + brand + ", carNo=" + carNo + "]";
}
// set()方法和get()方法省略
}
在User类中,新增Car类型属性。
public class User {
private String username;
private String password;
private Car car;
@Override
public String toString() {
return "User [username=" + username + ", password=" + password+ ", car=" + car + "]";
}
// set()方法和get()方法省略
}
在applicationContext.xml配置文件中,注册User类和Car类到容器。
<!-- p命名空间注入属性值 -->
<bean id="car" class="com.mhys.bean.Car" p:brand="奔驰" p:carNo="D4562"></bean>
<bean id="user" class="com.mhys.bean.User" p:username="李瑞" p:password="asd123" p:car-ref="car">
</bean>
编写测试类。 在控制台中,查看输出结果。
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
}
p命名空间注入属性之前需要引入对应约束,xmlns:p="http://www.springframework.org/schema/p",p命名空间没有对应的Schema文件。
1.3.3 表达式spel方式注入属性
示例:在applicationContext.xml配置文件中,注册User和Car类到容器
在com.mhys.bean包下,创建Car类,首先,添加brand和carNo两个属性。
<bean id="car" class="com.mhys.bean.Car">
<!-- 表达式spel方式注入属性 -->
<property name="brand" value="#{'宝马'}"></property>
<property name="carNo" value="#{'A2496'}"></property>
</bean>
<bean id="user" class="com.mhys.bean.User">
<property name="username" value="#{'杨强'}"></property>
<property name="password" value="#{'zxc456'}"></property>
<property name="car" ref="car"></property>
</bean>
在控制台中,查看输出结果。
1.3.4 实践练习
1.4 BeanFactory与ApplicationContext的区别
BeanFactory和AppliactionContext创建Spring容器的区别主要体现在Bean创建时间的不同
ApplicationContext采用的是创建上下文的方式创建Spring容器
Bean是在创建容器时即被加载。BeanFactory采用的是延时加载,即容器需要Bean的时候才加载实例。
1.4.1 BeanFactory接口
BeanFactory是IoC容器的顶级接口,是IoC容器的最基础实现,也是访问Spring容器的根接口,负责对Bean的创建和访问等工作。
XmlBeanFactory的演示案例:
编写测试类。
public class Test {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
User user = (User) beanFactory.getBean("user");
System.out.println(user);
}
}
在控制台中,查看输出结果
1.4.2 ApplicationContext接口
ApplicationContext即应用上下文,它是建立在BeanFactory基础之上的。
ApplicationContext有四个主要的实现类:
- ClassPathXmlApplicationContext:默认从类路径(classes目录下)加载配置文件。
- FileSystemXmlApplicationContext:默认从文件系统中加载配置文件。
- XmlWebApplicationContext
- AnnotationConfigWebApplicationContext。
XmlWebApplicationContext与AnnotationConfigWebApplicationContext是专门为Web应用提供的。
示例:通过FileSystemXmlApplicationContext获取上下文容器对象
在com.mhys.pojo包下,创建Book类,首先,添加bookName和pages两个属性。
public class Book {
private String bookName; // 书名
private int pages; // 页数
// set()方法和get()方法省略
}
在applicationContext.xml配置文件中,注入Book类。
<!-- p命名空间注入属性 -->
<bean id="book" class="com.mhys.pojo.Book" p:bookName="西游记" p:pages="360">
</bean>
在com.mhys.test包下,编写测试类。 在控制台中,查看输出结果。
public class Test {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Book book = (Book) context.getBean("book");
System.out.println(book.getBookName() + "总共" + book.getPages() + "页");
}
}
1.4.3 实践练习
总结
- Spring是一个轻量级的IoC和AOP容器框架,是为Java应用程序提供基础性服务的一套框架,目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求。
- IoC就是控制反转,是指创建对象的控制权的转移
- BeanFactory和AppliactionContext创建Spring容器的区别主要体现在Bean创建时间的不同,ApplicationContext采用的是创建上下文的方式创建Spring容器,Bean是在创建容器时即被加载。BeanFactory采用的是延时加载,即容器需要Bean的时候才加载实例。