Spring(IOC实际开发使用、底层原理)
实际开发的使用
实际开发中会将程序分为3层:
- Controller
- Servlet
- Repository(DAO)
关系Controller 调运Servlet 调运 Repository(DAO)
@Component 注解是将标注的类加载到IoC容器中,实际开发中可以分别根据
@Controller 控制层
@Service 业务层
@Repository 持久层
代码:
package com.southwind.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public interface myRepository {
public String domyRepository(Double score);
}
package com.southwind.Repository.impl;
import com.southwind.Repository.myRepository;
import org.springframework.stereotype.Repository;
@Repository
public class mymyRepositoryImpl implements myRepository {
@Override
public String domyRepository(Double score) {
String result="";
if(score<60){
result="不及格";
}else if(score>=60&&score<80){
result="合格";
}else {
result="优秀";
}
return result;
}
}
package com.southwind.Service;
import org.springframework.stereotype.Component;
public interface myService {
public String doSrvice(Double score);
}
package com.southwind.Service.impl;
import com.southwind.Repository.myRepository;
import com.southwind.Service.myService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Setter
@Service
public class myServiceImpl implements myService {
@Autowired
private myRepository myRepository;
@Override
public String doSrvice( Double score) {
return myRepository.domyRepository(score);
}
}
package com.southwind.Controller;
import com.southwind.Service.myService;
import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller(value = "a")
@Data
public class myControlller {
//客户端请求
@Autowired
private com.southwind.Service.myService myService;
; public String service(Double score){
return myService.doSrvice(score);
}
}
配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- <bean id="repository" class="com.southwind.entity.Repository"></bean>-->
<!-- 自动扫包-->
<context:component-scan base-package="com.southwind"></context:component-scan>
<!-- <bean id="controller" class="com.southwind.Controller.myControlller">-->
<!-- <property name="myService" ref="service"></property>-->
<!-- </bean>-->
<!-- <bean id="service" class="com.southwind.Service.impl.myServiceImpl">-->
<!-- <property name="myRepository" ref="repository"></property>-->
<!-- </bean>-->
<!-- <bean id="repository" class="com.southwind.Repository.impl.mymyRepositoryImpl"></bean>-->
</beans>
测试类:
package com.southwind.test;
import com.southwind.Controller.myControlller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test9 {
public static void main(String[] args) {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-annotation.xml");
// String[] s= applicationContext.getBeanDefinitionNames();
// for(String name :s){
// System.out.println(name);
// }
// 客户端请求
myControlller myControlller =(myControlller) applicationContext.getBean("a") ;
String result = myControlller.service(new Double(77));
System.out.println(result);
}
}
Spring IoC的底层实现:
核心技术: XML解析+反射
具体思路:
- 根据需求编写XML文件,配置需要的创建的Bean.
- 编写程序需要的XML文件,获取Bean的相关信息,类,属性,id
- 根据第二步骤获得到的信息,结合反射机制动态的创建对象,同时完成属性的赋值
- 将创建好的bean存入Map集合中,设置key就是bean的id的值,value就是bean的对象
- 提供方法从Map中获得对应的value
Spring(IOC实际开发使用、底层原理)的更多相关文章
- Spring注解驱动开发之扩展原理
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- 【spring 注解驱动开发】扩展原理
尚学堂spring 注解驱动开发学习笔记之 - 扩展原理 扩展原理 1.扩展原理-BeanFactoryPostProcessor BeanFactoryPostProcessor * 扩展原理: * ...
- Spring Boot-自动配置之底层原理
一.SpringBoot启动的时候加载主配置类,开启了自动配置的功能 @SpringBootApplication public class SpringBoot02Application { pub ...
- 关于spring,IOC和AOP的解析原理和举例
引用自:https://blog.csdn.net/paincupid/article/details/43152397 IOC:就是DAO接口的实现不再是业务逻辑层调用工厂类去获取,而是通过容器(比 ...
- Spring注解驱动开发(五)-----扩展原理
扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFacto ...
- Spring IOC容器解析及实现原理
最近一段时间,“容器”两个字一直萦绕在我的耳边,甚至是吃饭.睡觉的时候都在我脑子里蹦来蹦去的.随着这些天一次次的交流.讨论,对于容器的理解也逐渐加深.理论上的东西终归要落实到实践,今天就借助sprin ...
- 【Spring boot】整合tomcat底层原理
本文结论 源码基于spring boot2.6.6 项目的pom.xml中存在spring-boot-starter-web的时候,在项目启动时候就会自动启动一个Tomcat. 自动配置类Servle ...
- Spring _day02_IoC注解开发入门
1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...
- spring ioc
spring ioc是spring的核心之一,也是spring体系的基础,那么spring ioc所依赖的底层技术是什么的?反射,以前我们开发程序的时候对象之间的相互调用需要用new来实现,现在所有的 ...
- Spring IOC 剖析
模拟实现 Spring Ioc 控制反转功能 使用 => 原理 => 源码 => 模拟实现 使用:了解 原理:熟悉 源码 And 模拟实现: 精通 对照 Spring 功能点 Spr ...
随机推荐
- Fastjson漏洞+复现
1.漏洞介绍 FastJson在解析json的过程中,支持使用autoType来实例化某一个具体的类,并调用该类的set/get方法来访问属性.通过查找代码中相关的方法,即可构造出一些恶意利用链. ...
- 【基础语法规范】【函数式编程、字符串分割】BC6:输出输入的第二个整数
思路:数组or字符串split分割 一.Scala 方法1:Int数组[不行] import scala.io.StdIn object Main{ def main(args:Array[Strin ...
- CKA考试经验:报考和考纲
1 报考相关 1.有效期一年.在一年内需要定好考试的时间. 2.提前15分钟进入考试系统, 提前进入考试系统后并不是立马开始考试,而是预留给考官时间考察你的考试环境 3.考试时间 ,注意报考的Time ...
- 一文理解什么是DevOps,通俗易懂白话文
一文理解什么是DevOps,通俗易懂白话文 devops是什么❝DevOps维基百科定义 DevOps(Development和Operations的组合词)是一种重视"软件开发人员(Dev ...
- CountDownLatch闭锁源码解析(基于jdk11)
目录 CountDownLatch闭锁源码解析(基于jdk11) 1.1 CountDownLatch概述 1.2 CountDownLatch原理 1.2.1 基本结构(jdk11) 1.2.2 a ...
- kestrel网络编程--开发redis服务器
1 文章目的 本文讲解基于kestrel开发实现了部分redis命令的redis伪服务器的过程,让读者了解kestrel网络编程的完整步骤,其中redis通讯协议需要读者自行查阅,文章里不做具体解析. ...
- elasticsearch倒排索引(全面了解)
SimpleAI推荐语: 前年转过这篇文章,最近在看检索相关论文,发现又有点忘记倒排索引(inverted index)的具体内容,遂翻出来再看看,不得不说,这个漫画画的太好了,娓娓道来,一看就懂,再 ...
- Navicat Premium无法连接到oracle数据库的解决方法
原因:Navicat Premium连不上oracle数据库一般是因为oci.dll文件的问题 解决方法:找到oracle安装路径中的oci.dll文件或者PL/SQL Developer安装路径中的 ...
- [python] 基于matplotlib实现雷达图的绘制
雷达图(也称为蜘蛛图或星形图)是一种可视化视图,用于使用一致的比例尺显示三个或更多维度上的多元数据.并非每个人都是雷达图的忠实拥护者,但我认为雷达图能够以视觉上吸引人的方式比较不同类别各个特征的值.本 ...
- [R语言] ggplot2入门笔记4—前50个ggplot2可视化效果
文章目录 通用教程简介(Introduction To ggplot2) 4 ggplot2入门笔记4-前50个ggplot2可视化效果 1 相关性(Correlation) 1.1 散点图(Scat ...