Spring介绍
Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级(lightweight container)。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。
Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和
Spring Web MVC。
Spring的核心就是 控制反转(IoC)和面向切面(AOP) 。
Spring的特点:
1. 方便解耦,简化开发
2. AOP编程的支持
3. 声明式事务的支持
4. 方便程序的测试
5. 方便集成各种优秀框架
6. 降低Java EE API的使用难度
7. Java 源码是经典学习范例
SpringIOC入门案例
1、概念
IOC(Inversion of Control,控制反转)。这是spring的核心。对于spring框架来说,IOC就是由spring来负责控制对象的生命周期和对象间的关系。而Spring提供了IOC容器来帮我们生成所需要的对象。也就是说之前对象的控制权在类手上,现在反转后到了Spring手上。
2、案例实现
2.1、导入Spring相关依赖
导入SpringIOC相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2.2、编写Spring配置文件
在resources目录下创建applicationContext.xml文件,文件名可以任意取。但是一般建议取名为applicationContext。
文件内容如下:
<?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">
<!--
classs:配置类的全类名
id:配置一个唯一标识
-->
<bean class="com.ziejie.dao.impl.StudentDaoImpl" id="studentDao" >
</bean>
</beans>
2.3、创建实体类,接口以及接口实现类并从容器中获取对象进行测试
定义实体类:com.zijie.domain.Student
package com.zijie.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student(){}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
定义接口:com.zijie.dao.StudentDao
package com.zijie.dao;
import com.zijie.domain.Student;
public interface StudentDao {
Student getStudentById(Integer id);
}
定义接口实现类:com.zijie.dao.Impl.StudentDaoImpl
package com.zijie.dao.Impl;
import com.zijie.dao.StudentDao;
import com.zijie.domain.Student;
public class StudentDaoImpl implements StudentDao {
public Student getStudentById(Integer id) {
Student s = new Student(1,"zijie",22);
if(id == s.getId()){
return s;
}
return null;
}
}
创建容器读取配置文件,从容器中获取对象并进行测试
public static void main(String[] args) {
//1.获取StudentDaoImpl对象
//创建Spring容器,指定要读取的配置文件路径
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象
StudentDao studentDao = (StudentDao) app.getBean("studentDao");
//StudentDao studentDao = app.getBean("studentDao",StudentDao.class);
//调用对象的方法进行测试
System.out.println(studentDao.getStudentById(1));
}
Spring中Bean的常用属性配置
1.id
bean的唯一标识,同一个Spring容器中不允许有重复的id
2.class
全类名,用于反射创建对象(可以是接口全类名,也可以是接口实现类全类名)
3.scope
scope主要有两个值:singleton和prototype
如果设置为singleton则一个容器中只会有这个一个bean对象。默认容器创建的时候就会创建该对象(单例)。
如果设置为prototype则一个容器中会有多个该bean对象。每次调用getBean方法获取时都会创建一个新对象(原型)。
想要学习的同学可以访问b站三更草堂老师的视频:三更草堂spring框架教程