Spring+SpringMVC介绍+bean实例化+依赖注入实战

Spring介绍

Spring是一个轻量级的Java 开发框架,核心是IOC(控制反转)和AOP(面向切面编程)

Spring解决了业务层(Service包)与其他各层(表现层,包括Model,View,Controller三部分;持久层,jdbc和mybatis……)之间耦合度高的问题

耦合是什么?Spring如何解决高耦合的?

耦合是什么?

耦合是衡量程序模块之间互相依赖程度的指标

耦合也可以用来衡量程序拓展性和维护性

这里的依赖,不是继承关系的那种依赖

本质上指的是模块之间关联关系强弱(属性直接获取修改;直接调用方法;通过方法参数调用;使用同一全局变量;使用同一其他模块;参数为一部分成员变量……)

松耦合代表业务层和其他层之间的耦合度较低,互相依赖的程度较低,彼此之间影响小;
松耦合代表业务层和其他层之间的耦合度较高,互相依赖的程度较高,修改A的代码,B也要修改;

我们开发程序,一般都是以“高内聚,低耦合”为目标的

Spring如何解决高耦合

没有Spring之前,我们使用Servlet,JSP和JDBC作为Java开发框架,开发JavaWeb程序

JSP将业务层和视图层耦合在了一起,JDBC又将业务层和持久层耦合在了一起

Spring解决了这个问题,其中的关键在于IOC,将创建对象,使用对象,销毁对象的权力交给SpringBean容器而不是代码

IOC的关键在于DI(依赖注入)

SpringMVC简介

Model(entity包)-View(thymeleaf等视图引擎)-Controller(Controller包)

Model(模型):用来处理程序中数据逻辑的部分

View(视图):在应用程序中,专门和浏览器进行交互,展示数据的资源

Contreller(控制器):可以理解成是一个分发器,来决定对于视图发来的请求,需要用哪一个模型来处理,以及处理完后需要跳回到哪一个视图,也就是用来连接视图和模型的

Spring框架的特点

  1. 方便解耦,简化开发,Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理。IOC的作用。
  2. AOP编程的支持,Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能。(可扩展性)
  3. 声明式事务的支持,只需要通过配置就可以完成对事务的管理,而无需手动编程。
  4. 方便程序的测试,Spring对Junit4支持,可以通过注解方便的测试Spring程序。
  5. 方便集成各种优秀框架,Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持。
  6. 降低JavaEE API的使用难度,Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。

IOC简介

IOC – Inverse of Control,控制反转,将对象的创建权力反转给Spring框架!!
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。
解决问题:使用IOC可以解决的程序耦合性高的问题。Spring的工厂读取配置文件。

IOC创建的bean,默认情况下整个内存只有一份,也就是单例模式,后续的测试中可以看到这一点

IOC是思想,DI是IOC的实现方法,两者绑定

bean的实例化

准备工作

maven依赖

    <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>

service接口

public interface UserService {public void hello();}

service接口实现类

public class UserServiceImpl implements UserService {@Overridepublic void hello() {System.out.println("Hello IOC!!");}
}

resources下创建applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

通过applicationContext.xml的方式实例化

1. 直接实例化

?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--IOC管理bean--><!--1. 直接实例化--><bean id="userService" class="cn.tx.service.UserServiceImpl" /></beans>

缺点:当需要加载到bean容器中的bean数量太多的时候,这样一个一个导入非常繁琐

2. 静态bean工程实例化

需要一个静态工厂类

public class StaticBeanFactory {// 静态工厂方式public static UserService staticBeanCreate() {System.out.println("通过静态工厂的方式创建UserServiceImpl对象...");return new UserServiceImpl();}}

随后在xml文件中导入静态工厂bean对象即可

?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--静态工厂方式--><bean id="staticUs" class="com.qcby.mySpring01.beanFactory.StaticBeanFactory" factory-method="staticBeanCreate"/>
</beans>

3. 动态工厂实例化

public class DynamicBeanFactory {//对象方法public UserService dynamicBeanCreate(){System.out.println("动态工厂的方式创建bean对象。。。");return new UserServiceImpl();}
}
?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--动态工厂方式--><bean id="dynamicFactoryBean" class="com.qcby.mySpring01.beanFactory.DynamicBeanFactory"/><bean id="dynamicUs" factory-bean="dynamicFactoryBean" factory-method="dynamicBeanCreate"/>
</beans>

通过注解实例化

@Component
@Service
@Repository
@Controller
@Mapper

bean实例化测试

public class IOCTest {@Testpublic void run() {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
/*        ApplicationContext applicationContext =new FileSystemXmlApplicationContext("D:\\6_WorkSpace\\shiXun\\spring01\\src\\main\\resources\\applicationContext.xml");*/UserService userService = (UserService) applicationContext.getBean("userService");UserService userService1 = (UserService) applicationContext.getBean("userService");System.out.println(userService);System.out.println(userService1);userService.hello("IOC!");}@Testpublic void factoryBeanTest() {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");UserService staticBean = (UserService) applicationContext.getBean("staticUs");UserService dynamicBean = (UserService) applicationContext.getBean("dynamicUs");staticBean.hello("staticFactory");dynamicBean.hello("dynamicFactory");}
}

在这里插入图片描述
在这里插入图片描述

依赖注入

DI:Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时(bean实例化),
动态的将依赖对象(对象的属性)注入到Bean组件中

通过applicationContext.xml的方式注入

set方法注入

必须保证setter方法存在,否则会报错

public class CarServiceImpl implements CarService {private CarDao carDao;@Overridepublic String toString() {return "CarServiceImpl{" +"msg='" + msg + '\'' +", id=" + id +'}';}private String msg;private Integer id;public void setMsg(String msg) {this.msg = msg;}public void setId(Integer id) {this.id = id;}public void setCarDao(CarDao carDao) {this.carDao = carDao;}public List<Car> findAll() {List<Car> carList = carDao.findAll();return carList;}
}
<!--set方法DI注入--><bean id="carDao" class="com.qcby.mySpring01.mapper.impl.CarDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="carService" class="com.qcby.mySpring01.service.impl.CarServiceImpl"><property name="carDao" ref="carDao"/><property name="msg" value="你好"/><property name="id" value="100"/></bean>

构造方法注入

public class Car {private int id;private String carName;private int size;private String color;public Car() {}public Car(String carName, int size, String color) {this.carName = carName;this.size = size;this.color = color;}@Overridepublic String toString() {return "Car{" +"id=" + id +", carName='" + carName + '\'' +", size=" + size +", color='" + color + '\'' +'}';}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName = carName;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}
}
<!--构造器DI注入--><bean id="car1" class="com.qcby.mySpring01.pojo.Car"><!--        <constructor-arg index="0" value="小米Su7 Pro Max"/><constructor-arg index="1" value="10"/><constructor-arg index="2" value="blue"/>--><constructor-arg name="carName" value="小米Su7 Pro Max"/><constructor-arg name="size" value="10"/><constructor-arg name="color" value="blue"/></bean><bean id="car2" class="com.qcby.mySpring01.pojo.Car"><!--        <constructor-arg index="0" value="小米Su7 Pro Max"/><constructor-arg index="1" value="10"/><constructor-arg index="2" value="blue"/>--><constructor-arg name="carName" value="BYD秦PLUS DMI"/><constructor-arg name="size" value="198"/><constructor-arg name="color" value="black"/></bean>

通过接口注入

(暂时略,后续补充)

数组,集合(List,Set,Map),Properties等的注入

public class CollectionBean {// 数组private Student[] studentArr;public void setStudentArr(Student[] studentArr) {this.studentArr = studentArr;}private List<String> list;public void setList(List<String> list) {this.list = list;}private Map<String, String> map;public void setMap(Map<String, String> map) {this.map = map;}private Map<Student, Car> studentCarMap;public void setStudentCarMap(Map<Student, Car> studentCarMap) {this.studentCarMap = studentCarMap;}private Properties properties;public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "CollectionBean{" +"studentArr=" + Arrays.toString(studentArr) +", list=" + list +", map=" + map +", studentCarMap=" + studentCarMap +", properties=" + properties +'}';}
}
<!--引用类型/集合注入--><bean id="student1" class="com.qcby.mySpring01.pojo.Student"><property name="name" value="张三"/><property name="age" value="15"/><property name="grade" value="7"/></bean><bean id="student2" class="com.qcby.mySpring01.pojo.Student"><property name="name" value="李四"/><property name="age" value="14"/><property name="grade" value="6"/></bean><bean id="student3" class="com.qcby.mySpring01.pojo.Student"><property name="name" value="王五"/><property name="age" value="18"/><property name="grade" value="13"/></bean><bean id="collectionBean" class="com.qcby.mySpring01.pojo.CollectionBean"><property name="studentArr"><array><ref bean="student1"/><ref bean="student2"/><ref bean="student3"/></array></property><property name="list"><list><value>熊大</value><value>熊二</value><value>吉吉国王</value></list></property><property name="map"><map><entry key="111" value="aaa"/><entry key="222" value="bbb"/></map></property><property name="studentCarMap"><map><entry key-ref="student1" value-ref="car1"/><entry key-ref="student2" value-ref="car2"/></map></property><property name="properties"><props><prop key="username">root</prop><prop key="password">123456</prop></props></property></bean>

这些其实都是固定写法

测试

@Testpublic void DITest() {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");CarService carService = (CarService) applicationContext.getBean("carService");// set方法注入System.out.println(carService);List<Car> carList = carService.findAll();for (Car car : carList) {System.out.print(car.getCarName() + "\t");}System.out.println();// 构造方法注入Car car1 = (Car) applicationContext.getBean("car1");System.out.println(car1);// 数组,集合(List,Set,Map),Properties注入CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");System.out.println(collectionBean);}

在这里插入图片描述

通过注解注入

依赖注入常用的注解
@Value 用于注入普通类型(String,int,double等类型)
@Autowired 默认按类型进行自动装配(引用类型)
@Qualifier 和@Autowired一起使用,强制使用名称注入
@Resource Java提供的注解,也被支持。使用name属性,按名称注入
对象生命周期(作用范围)注解
@Scope 生命周期注解,取值singleton(默认值,单实例)和prototype(多例)
初始化方法和销毁方法注解(了解)
@PostConstruct 相当于init-method
@PreDestroy 相当于destroy-method

@Service
@Qualifier("studentServiceImpl")
public class StudentServiceImpl implements StudentService {public void listAll() {System.out.println("studentService2");}
}
@Service
@Qualifier("studentServiceImpl2")
public class StudentServiceImpl2 implements StudentService {public void listAll() {System.out.println("studentService2");}
}
@Configuration
@ComponentScan("com.qcby")
@Import({SpringConfig2.class})// 多配置文件
public class SpringConfig {}
    @Testpublic void qualifierTest() {ApplicationContext applicationContext =new AnnotationConfigApplicationContext(SpringConfig.class);
//        ApplicationContext applicationContext =
//                new ClassPathXmlApplicationContext("applicationContext_anno.xml");
/*        ApplicationContext applicationContext =new FileSystemXmlApplicationContext("D:\\6_WorkSpace\\shiXun\\spring01\\src\\main\\resources\\applicationContext_anno.xml");*/StudentService studentService = (StudentService) applicationContext.getBean("studentServiceImpl");StudentService studentService2 = (StudentService) applicationContext.getBean("studentServiceImpl2");System.out.println(studentService);System.out.println(studentService2);studentService.listAll();studentService2.listAll();}

在这里插入图片描述

纯注解不需要写applicationContext.xml文件

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/33059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

红队内网攻防渗透:内网渗透之内网对抗:隧道技术篇防火墙组策略ICMPDNSSMB协议出网判断C2上线解决方案

红队内网攻防渗透 1. 内网隧道技术1.1 学隧道前先搞清楚的知识1.2 常用的隧道技术1.3 判断协议出网的命令1.4 C2上线-开防火墙入站只80&出站只放ICMP1.4.1 icmp隧道上线CS后门1.4.1 icmp隧道上线MSF后门1.5 C2上线-开防火墙入站只80&出站只放DNS1.5.1 DNS隧道上线CS后门…

计算机相关专业的发展与选择。

亲爱的考生们&#xff0c;站在人生的十字路口&#xff0c;你们可能会在考虑是否选择计算机相关专业时感到困惑。作为一名即将毕业的计算机专业学生&#xff0c;同时也是这个行业的从业者&#xff0c;我认为这个选择确实存在挑战&#xff0c;但同时也充满了机遇。  首先&#…

车辆数据的提取、定位和融合(其二.一 共十二篇)

第一篇&#xff1a; System Introduction 第二篇&#xff1a;State of the Art 第三篇&#xff1a;localization 第四篇&#xff1a;Submapping and temporal weighting 第五篇&#xff1a;Mapping of Point-shaped landmark data 第六篇&#xff1a;Clustering of landma…

【Spring】Spring Boot 快速入门

&#x1f4da;博客主页&#xff1a;爱敲代码的小杨. ✨专栏&#xff1a;《Java SE语法》 | 《数据结构与算法》 | 《C生万物》 |《MySQL探索之旅》 |《Web世界探险家》 ❤️感谢大家点赞&#x1f44d;&#x1f3fb;收藏⭐评论✍&#x1f3fb;&#xff0c;您的三连就是我持续更…

Spring MVC的核心类和注解——@RequestMapping注解(一)@RequestMapping注解的使用

一、RequestMapping注解作用 RequestMapping注解用于建立请求URL和Handler&#xff08;处理器&#xff09;之间的映射关系&#xff0c;该注解可以标注在方法上和类上。下面分别对RequestMapping注解的这两种使用方式进行介绍。 &#xff08;一&#xff09;、方式一—标注在方法…

计算机网络期末

1、IP 地址为:192.168.0.254,它的子网掩码应该为( ) A.255.255.255.0 B.255.255.254.0 C.255.255.252.0 D.255.255.0.0 2、最容易产生网络可靠性瓶颈问题的拓扑构型是&#xff08; &#xff09;。 A 总线型 B 星型 C 环型 D 网状型 3、HTTP 就是电子邮件阅读协议&#xff0…

UE5 中的碰撞问题

文章目录 一、初始准备二、重叠和碰撞三、自定义碰撞 一、初始准备 首先我们创建一个 BP_ThirdPerson 项目&#xff0c;然后在项目中创建两个 Actor 的蓝图 Blueprint 首先是一个移动的 BP_Push&#xff0c;这里使用 time line 循环旋转 cube 的相对位置 得到效果如下 然后是…

在QVBoxLayout中如何将小部件垂直对齐到顶部而不是居中

在使用Qt框架进行开发时&#xff0c;当你将小部件添加到布局中&#xff0c;小部件默认会垂直居中。我们如何能让这些小部件从顶部开始垂直排列而不是默认的居中呢&#xff1f; 方法一&#xff1a;使用addStretch 如果你希望在一个QVBoxLayout中将固定大小的小部件堆叠在顶部&…

计算机网络 —— 应用层(FTP)

计算机网络 —— 应用层&#xff08;FTP&#xff09; FTP核心特性&#xff1a;运作流程&#xff1a; FTP工作原理主动模式被动模式 我门今天来看应用层的FTP&#xff08;文件传输协议&#xff09; FTP FTP&#xff08;File Transfer Protocol&#xff0c;文件传输协议&#x…

qt基本窗口类(QWidget,QDialog,QMainWindow)

1.三个基本窗口类 1.1QWidget 所有窗口的基类 可以内嵌到其他窗口的内部&#xff0c;无边框 也可以作为独立窗口显示&#xff0c;有边框 1.2QDialog 继承于QWidget 对话框窗口类 不可以内嵌到其他窗口 有模态和非模态两种显示方式 1.3QMainWind 继承于QWidget 主窗口类 不可以…

【服务器07】之【GitHub项目管理】及【Unity异步加载场景】

登录GitHub官网 GitHub: Let’s build from here GitHub 注册账号 登录账号 输入一个自定义名字&#xff0c;点击创建存储库就可以了 现在我们下载Fork Fork - a fast and friendly git client for Mac and Windows (git-fork.com) 免费的 下载完成之后点击File下的Clone …

IPython使用技巧整理

以下是一些常见且有用的IPython使用技巧&#xff0c;整理如下&#xff1a; 一、基本功能 1. 启动IPython&#xff1a;在终端输入ipython命令即可启动IPython环境。 2. 自动补全&#xff1a;使用Tab键进行变量和函数名的自动补全。例如&#xff0c;输入pri后按Tab键&#xff0c…

Docker 多阶段构建

多阶段构建 目录 尝试创建 Dockerfile构建容器镜像运行 Spring Boot 应用程序使用多阶段构建额外资源 在传统构建中&#xff0c;所有构建指令都在一个构建容器中顺序执行&#xff1a;下载依赖项、编译代码、打包应用程序。所有这些层最终都在你的最终镜像中。这种方法虽然可行…

TLS握手中的RTT

文章目录 TLS 1.2 握手过程中的 RTT 次数TLS 1.3 1-RTT 初次TLS1.3 0-RTT 握手过程总结 TLS 1.2 握手过程中的 RTT 次数 TLS 1.2 握手通常需要2 RTT 才能完成。具体步骤如下&#xff1a; 第一次 RTT&#xff1a; 客户端发送 ClientHello&#xff1a;客户端生成一个随机数&…

怎么描述shareed_ptr与unique_ptr的共享与独占

std::shared_ptr 和 std::unique_ptr 是 C11 引入的智能指针类型&#xff0c;它们用于自动管理动态分配的内存&#xff0c;防止内存泄漏。这两种智能指针在内存所有权和生命周期管理方面有着明显的区别。 std::shared_ptr&#xff08;共享所有权&#xff09; std::shared_ptr …

深度神经网络简介

深度神经网络&#xff08;Deep Neural Networks, DNNs&#xff09;是机器学习领域中一种重要的技术&#xff0c;特别是在处理复杂和大规模数据方面表现出色。以下是关于深度神经网络的详细介绍&#xff1a; 一、定义与原理 深度神经网络是由多层神经元组成的人工神经网络&…

分别使用autofs和修改/etc/fstab文件挂载其他ip地址路径的示例

题目&#xff1a; 按照以下要求&#xff0c;通过 autofs 自动挂载远程用户的主目录&#xff1a; server1.lab0.example.com(172.25.0.254)通过 NFS 共享目录/rhome 到你的系统&#xff0c; 此文件系统中包含为用户 ldapuser0 预配置分别使用autofs和修改/etc/fstab文件挂载其他…

ServletRequest类及其使用方法介绍

在Java的Servlet技术中&#xff0c;ServletRequest是一个接口&#xff0c;它定义了客户端发送到服务器的请求的API。这个接口提供了获取请求信息的方法&#xff0c;包括参数、头信息、属性等。以下是对ServletRequest接口中一些重要方法的介绍以及它们的使用示例。 ServletReq…

【退役之重学 AI】Ubuntu 安装 Anaconda

一. 下载 安装文件 https://www.anaconda.com/download/success 二. 安装 bash anaconda****.bash 一路 enter&#xff0c;yes 最后一个问你 要不要 conda init&#xff0c;这里得输入yes&#xff08;默认是no&#xff0c;所以不要直接 enter&#xff09;&#xff0c;否则你…

Pytest和Unitest框架对比

在学到自动化的时候,很多同学都遇到了Pytest和Unitest框架,有的人是两个都学,但是学的不精只是知道分别怎么用.不了解两个区别是什么.有的是犹豫到底要学习那个框架.其实要做好自动化测试,是有必要了解不同框架之间的差异化的. Pytest 特点: Pytest采用了更简洁、更灵活的语法…