Spring框架学习笔记
经过了两个多月的学习,今天终于进入了Spring框架的学习,Spring作为JavaEE的灵魂框架,其重要性毋庸置疑,所以在此对学习Spring的过程做一些记录。
Spring简介
Spring是分层的Java SE/EOE应用full-stack轻量级开源框架,以IOC和AOP为内核。
- full-stack:这里的全栈并不是我们平常所理解的意思,它是指Spring在各层上都有对应的解决方案,web层对应有SpringMVC,service层对应有SpringIOC,dao层对应有SpringJdbcTemplate。
- IOC:Inversion Of Controller 控制反转 将创建Bean的控制权反转给Spring
- AOP:Aspect Oriented Programming
Spring提供了展现层SpringMVC和持久层SpringJDBCTemplate以及业务层事务管理等众多的企业级开发技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的JavaEE企业应用开源框架。
Spring的优势
- 方便解耦,简化开发
- AOP编程的支持
- 声明式事务的支持
- 方便程序的测试
- 方便集成各种优秀的框架
- 降低了Java EE API 的使用难度
- Spring 的Java源码是经典学习范例
Spring快速入门的开发步骤
- 导入坐标
在项目的pom.xml中添加如下依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2.创建Bean
Bean的成员变量可以分为三类:普通数据类型、引用数据类型、集合数据类型
/**
* Teacher类
*/
public class Teacher {
private String name;
private int age;
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;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
//定义StudentDao接口
public interface StudentDao {
public void study();
}
import com.fxq.dao.StudentDao;
import com.fxq.domain.Teacher;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class StudentDaoImpl implements StudentDao {
//定义普通数据类型的成员变量
private String name;
//定义存储普通数据类型的List集合
private List<String> strList;
//定义存储引用数据类型的List集合
private List<Teacher> teacherList;
//定义Map集合,键为String 值为Teacher
private Map<String,Teacher> teacherMap;
//定义Properties集合,键值都为String
private Properties properties;
public StudentDaoImpl(String name, List<String> strList, List<Teacher> teacherList, Map<String, Teacher> teacherMap, Properties properties) {
this.name = name;
this.strList = strList;
this.teacherList = teacherList;
this.teacherMap = teacherMap;
this.properties = properties;
}
public StudentDaoImpl() {
}
public void setName(String name) {
this.name = name;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void setTeacherList(List<Teacher> teacherList) {
this.teacherList = teacherList;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void study() {
System.out.println(strList);
System.out.println(teacherList);
System.out.println(teacherMap);
System.out.println(properties);
System.out.println("我在学习");
}
}
- 创建applicationContext.xml配置文件
在resources目录下创建三个配置文件,在applicationContext.xml中使用import标签引入applicationContext-dao.xml,在applicationContext-dao.xml中引入applicationContext-domain.xml
4. 在配置文件进行配置
!-- applicationContext.xml 引入外部配置文件-->
<import resource="applicationContext-dao.xml"/>
<bean id="studentService" class="com.fxq.service.impl.StudentServiceImpl">
<property name="sdao" ref="studentDao"/>
</bean>
<!-- 引入applicationContext-domain.xml-->
<import resource="applicationContext-domain.xml"/>
<bean id="studentDao" class="com.fxq.dao.impl.StudentDaoImpl">
<property name="name" value="zhangsan"/>
<property name="strList">
<list>
<!-- 普通数据类型直接yongvalue-->
<value>Chinese</value>
<value>math</value>
</list>
</property>
<property name="teacherList">
<list>
<!-- list中存储的引用类型-->
<!--需要用ref引用在applicationContext-domain.xml中创建的bean-->
<ref bean="teacher"/>
<ref bean="teacher2"/>
</list>
</property>
<!-- 配置map集合并注入-->
<property name="teacherMap">
<map>
<entry key="1" value-ref="teacher"/>
<entry key="2" value-ref="teacher2"/>
</map>
</property>
<!-- 配置properties集合并注入-->
<property name="properties">
<props>
<prop key="3">three</prop>
<prop key="4">four</prop>
</props>
</property>
</bean>
- 创建ApplicationContext的对象,调用getBean方法获取Bean
//定义StudentService接口
public interface StudentService {
public void study();
}
//定义studentService接口实现类
import com.fxq.dao.StudentDao;
import com.fxq.service.StudentService;
public class StudentServiceImpl implements StudentService {
private StudentDao sdao;
public StudentServiceImpl(StudentDao sdao) {
this.sdao = sdao;
}
public StudentServiceImpl() {
}
public void setSdao(StudentDao dao){
this.sdao = dao;
}
public void study() {
sdao.study();
}
}
import com.fxq.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class StudentController {
public static void main(String[] args) {
//获取StudentService对象
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new FileSystemXmlApplicationContext("E:\\IdeaProjects\\JavaWeb_code\\frame_spring\\src\\main\\resources\\applicationContext.xml");
// StudentService studentService = (StudentService) app.getBean("studentService");
StudentService studentService = app.getBean(StudentService.class);
studentService.study();
}
}
在StudentController中创建ApplicationContext容器,并通过getBean方法获取StudentService对象,并调用方法
ApplicationContext的实现类
ApplicationContext有3个实现类:
-
ClassPathXmlApplicationContext (推荐)
从类的根路径下加载配置文件 -
FileSystemXmlApplicationContext
从磁盘中加载配置文件,配置文件可以在磁盘的任意位置 -
AnnotationConfigApplicationContext
注解开发
getBean() 方法
- getBean(String name)
参数:配置文件bean的id
当配置文件中出现多个同类型的bean时也可以使用 - getBean(Class aClass)
参数:Bean的字节码对象
只能在文件中同类型的bean只有一个时才能使用
Spring创建Bean的三种方式
- 调用构造器创建,使用如下标签
<constructor-arg name="xxx" ref="xxx"/>`
- 工厂静态方法创建,首先创建工厂类,定义静态方法,使用如下标签
import com.fxq.dao.UserDao;
import com.fxq.dao.impl.UserDaoImpl;
public class StaticFactory {
public static UserDao getUserDao(){
return new UserDaoImpl();
}
}
<!-- 工厂静态方法创建Bean-->
<bean id="userDao" class="com.fxq.factory.StaticFactory" factory-method="getUserDao"></bean>
- 工厂实例方法创建,定义成员方法,使用如下标签
import com.fxq.dao.UserDao;
import com.fxq.dao.impl.UserDaoImpl;
public class DynamicFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
<!-- 工厂实例方法创建Bean-->
<!--<bean id="factory" class="com.fxq.factory.DynamicFactory"></bean>
<bean id="xxx" factory-bean="factory" factory-method="getUserDao"></bean>-->
配置文件标签
bean标签
- id属性:在容器中Bean实例的唯一标识,不允许重复
- class属性:要实例化的Bean的全限定名
- scope属性:Bean的作用范围
singleton(默认): 单例
创建Bean的时机:在创建容器的时候创建Bean,多次创建的 Bean是同一个
prototype:多例
创建Bean的时机:每次调用getBean方法时都会创建一个不同的Bean
property标签
- name属性:属性名称
- value属性:注入的普通属性值
- ref属性:注入的对象引用值