目录 一、相关导读 1. Maven系列专栏文章 2. Mybatis系列专栏文章 3. Spring系列专栏文章 二、前言 Spring简介 Spring体系结构 三、Spring实现IOC 1. 创建Maven工程,引入对应依赖 2.
目录
大家如果对于本期内容有什么不了解的话也可以去看看往期的内容,下面列出了博主往期精心制作的Maven,mybatis等专栏系列文章,走过路过不要错过哎!如果对您有所帮助的话就点点赞,收藏一下啪。其中spring专栏有些正在更,所以无法查看,但是当博主全部更完之后就可以看啦。
Maven系列专栏 | Maven工程开发 |
Maven聚合开发【实例详解---5555字】 |
Spring是一个开源框架,为简化企业级开发而生。它以ioc(控制反转)和aop(面向切面)为思想内核,提供了控制层springMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
Spring官网地址:Spring | HomeSpring官方网站:
Spring框架根据不同的功能被划分成了多个模块,这些模块可以满足一切企业级应用开发的需求,在开发过程中可以根据需求有选择性地使用所需要的模块。
接下来我们使用Spring实现IOC,Spring内部也有一个容器用来管理对象。
org.springframework spring-context 5.3.13 junit junit 4.12 test
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,"程序员","北京"); }}
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)); }}
OK,同样返回两个对象的哈希值都是一样的,说明了确实是从容器中获取同一个对象。
- BeanFactory:BeanFactory是Spring容器中的顶层接口,它可以对Bean对象进行管理。
- ApplicationContext:ApplicationContext是BeanFactory的子接口。它除了继承 BeanFactory的所有功能外,还添加了对国际化、资源访问、事件传播等方面的良好支持。ApplicationContext有以下三个常用实现类:
- ClassPathXmlApplicationContext:该类可以从项目中读取配置文件
- FileSystemXmlApplicationContext:该类从磁盘中读取配置文件
- AnnotationConfigApplicationContext:使用该类不读取配置文件,而是会读取注解
@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));; }
OK,本次使用Spring实现IOC就到这里了,上述讲到的三个实现类会在接下来中多次使用,希望对大家有所帮助
来源地址:https://blog.csdn.net/qq_53317005/article/details/129780496
--结束END--
本文标题: IOC使用Spring实现附实例详解
本文链接: https://www.lsjlt.com/news/391424.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0