目录
一、相关导读
1. Maven系列专栏文章
Maven系列专栏 | Maven工程开发 |
Maven聚合开发【实例详解---5555字】 |
2. Mybatis系列专栏文章
Mybatis系列专栏 | MyBatis入门配置 |
Mybatis入门案例【超详细】 | |
MyBatis配置文件 —— 相关标签详解 | |
Mybatis模糊查询——三种定义参数方法和聚合查询、主键回填 | |
Mybatis动态SQL查询 --(附实战案例--8888个字--88质量分) | |
Mybatis分页查询——四种传参方式 | |
Mybatis一级缓存和二级缓存(带测试方法) | |
Mybatis分解式查询 | |
Mybatis关联查询【附实战案例】 | |
MyBatis注解开发---实现增删查改和动态SQL | |
MyBatis注解开发---实现自定义映射关系和关联查询 |
3. Spring系列专栏文章
Spring系列专栏 | Spring IOC 入门简介【自定义容器实例】 |
IOC使用Spring实现附实例详解 | |
Spring IOC之对象的创建方式、策略及销毁时机和生命周期且获取方式 | |
Spring DI简介及依赖注入方式和依赖注入类型 | |
Spring IOC相关注解运用——上篇 | |
Spring IOC相关注解运用——下篇 | |
Spring AOP简介及相关案例 | |
注解、原生Spring、SchemaBased三种方式实现AOP【附详细案例】 | |
Spring事务简介及相关案例 | |
Spring 事务管理方案和事务管理器及事务控制的API | |
Spring 事务的相关配置、传播行为、隔离级别及注解配置声明式事务 |
二、前言
Spring简介
Spring体系结构
三、Spring实现IOC
1. 创建Maven工程,引入对应依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
2. 创建实体类,Dao接口及实现类
Student实体类
package com.example.pojo;
public class Student {
private int id;
private String name;
private String address;
public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public Student(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student[ " +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
" ]";
}
}
StudentDao接口
package com.example.dao;
import com.example.pojo.Student;
public interface StudentDao {
// 根据id查询学生
Student findById(int id);
}
StudentDao接口实现类StudentDaoImpl1
package com.example.dao;
import com.example.pojo.Student;
public class StudentDaoImpl1 implements StudentDao{
public StudentDaoImpl1() {
}
public StudentDaoImpl1(int a){};
@Override
public Student findById(int id){
return new Student(id,"程序员","北京");
}
}
3. 编写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"
default-autowire="constructor">
<bean id="studentDao" class="com.example.dao.StudentDaoImpl1"/>
</beans>
4. 测试从Spring容器获取对象
package com.example;
import com.example.dao.StudentDao;
import com.example.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestContainer {
@Test
public void t1(){
// 创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 从容器中获取对象
StudentDao studentDao1 = ac.getBean("studentDao",StudentDao.class);
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);
System.out.println(studentDao1.hashCode());
System.out.println(studentDao2.hashCode());
System.out.println(studentDao1.findById(1));
}
}
5. 测试结果
四、Spring容器类型
1. 容器接口
2. ApplicationContext容器实现类
3. 测试从磁盘读取配置文件
@Test
public void t2(){
// 创建spring容器
ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\JavaProjects\\06SSM_Projects\\springdemo\\spring_ioc1\\src\\main\\resources\\bean.xml");
// 从容器中获取对象
StudentDao userDao = ac.getBean("studentDao",StudentDao.class);
System.out.println(userDao);
System.out.println(userDao.findById(1));;
}
4. 测试结果