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 ...
随机推荐
- 你认识的C# foreach语法糖,真的是全部吗?
本文的知识点其实由golang知名的for循环陷阱发散而来, 对应到我的主力语言C#, 其实牵涉到闭包.foreach.为了便于理解,我重新组织了语言,以倒叙结构行文. 先给大家提炼出一个C#题:观察 ...
- 重学c#系列——委托和匿名函数[二十五]
前言 简单介绍一下什么是委托. 正文 以前也写过委托,这次算是重新归档,和新的补充吧. https://www.cnblogs.com/aoximin/p/13940125.html 有些人说委托是函 ...
- 解决win7设置默认程序打开方式失效
问题描述 我在设置一个文件(.ui)的默认程序打开,总是失效.设置不成功. 原因 正常这个程序应该用 designer.exe 打开,但是我之前设置过(.ui)默认程序打开程序为designer.ex ...
- Duplicate property mapping of xxx found in xx 嵌套异常,重复的属性在映射中发现。
该异常的原意是因为在映射文件中出现了两个一样的属性名: <property name="相同的属性名出现了两次以上" > <property name=" ...
- node学习01
1.前言 Node.js 是一个开源和跨平台的 JavaScript 运行时环境 Node.js 在浏览器之外运行 V8 JavaScript 引擎(Google Chrome 的内核). 这使得 N ...
- NLP实践!文本语法纠错模型实战,搭建你的贴身语法修改小助手 ⛵
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 自然语言处理实战系列:https://www.showmeai.tech ...
- SQL注入绕waf思路总结
1.关键字大小写混合绕过 关键字大小写混合只针对于小写或大写的关键字匹配技术-正则表达式,如果在匹配时大小写不敏感的话,就无法绕过.这是最简单的一个绕过技术. 例如:将union select混写成U ...
- PyQt4编写界面的两种方式
PyQt4编写界面的两种方式 应用PyQt4开发图形化界面有两种方式,一种是直接通过QtDesigner通过提供的窗口部件拖拽进行GUI创建,另外一种是直接进行编程实现. 第一种,QtDesigner ...
- .NET周报【12月第1期 2022-12-08】
国内文章 CAP 7.0 版本发布通告 - 支持延迟消息,性能炸了? https://www.cnblogs.com/savorboard/p/cap-7-0.html) 今天,我们很高兴宣布 CAP ...
- 基于 Spring Cloud 的微服务脚手架
基于 Spring Cloud 的微服务脚手架 作者: Grey 原文地址: 博客园:基于 Spring Cloud 的微服务脚手架 CSDN:基于 Spring Cloud 的微服务脚手架 本文主要 ...