spring可能会迟到,但不会缺席

2023-10-21 03:30

本文主要是介绍spring可能会迟到,但不会缺席,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

抱歉了大家,SSM框架不小心遗漏掉了spring框架,在此补上,下一期补springMVC执行原理,下下一期就是springBoot框架了,完了之后,我们一起做一个完整的项目哟,期待哈-------

在这里插入图片描述

一.ioc控制反转

背景引入:我们从service层到dao层来分析
dao层

/UserDao接口

package com.huang.dao;public interface UserDao {void getUser();
}

/UserDao接口实现类1:/UserDaoImpl实现类

package com.huang.dao;public class UserDaoImpl implements UserDao{@Overridepublic void getUser() {System.out.println("获取普通用户信息");}
}

/UserDao接口实现类2:/UserMysqlImpl实现类

package com.huang.dao;public class UserMysqlImpl implements UserDao{@Overridepublic void getUser() {System.out.println("获取mysql用户信息");}
}
service层

/UserService接口

package com.huang.service;public interface UserService {void getUser();
}

/UserService接口实现类:/UserServiceImpl实现类

package com.huang.service;import com.huang.dao.UserDao;
import com.huang.dao.UserDaoImpl;
import com.huang.dao.UserMysqlImpl;
import org.springframework.beans.factory.annotation.Autowired;public class UserServiceImpl implements UserService{UserDao userDao = new UserDaoImpl();@Overridepublic void getUser() {userDao.getUser();}
}
测试类
@Test
public void test1(){UserService userService = new UserServiceImpl();userService.getUser();
}

问题:当我们的dao层接口有很多很多实现类,我们程序员就必须修改:/UserServiceImpl实现类中的:

UserDao userDao = new UserDaoImpl();

这一段的代码,这样我们的开发做了很多无用功;

我们能不能采用一种方法让用户来决定调用哪个dao层接口实现类呢?

一切对象交由spring管理
我们只需要修改service层接口的实现类:/UserServiceImpl实现类:

注意:ioc依赖注入的方式之一:set注入,所以此处我们一定要给一个set方法

public class UserServiceImpl implements UserService{private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}@Overridepublic void getUser() {userDao.getUser();}
}
spring配置文件:
  1. id:bean的唯一标识,后面的class表示类
  2. ref:表示引用类型,通常还有value表示简单类型
  3. 我们在测试类中使用getBean(“userService”);返回的结果就是id为userService对应的类的对象
<?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-3.0.xsd"><bean id="userDao" class="com.huang.dao.UserDaoImpl"/><bean id="userMysql" class="com.huang.dao.UserMysqlImpl"/><bean id="userService" class="com.huang.service.UserServiceImpl"><property name="userDao" ref="userMysql"/></bean></beans>
测试类
@Test
public void test2(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");UserServiceImpl userService = (UserServiceImpl) context.getBean("userService");userService.setUserDao(new UserMysqlImpl());userService.getUser();
}

此后调用service层的用户只需要修改以下代码中的dao层接口的实现类

userService.setUserDao(new UserMysqlImpl());

以上代码实现了:从程序员修改代码到用户来传入实现类,这是我对ioc控制反转的理解

二.ioc创建对象的方式(构造方式注入)

1.无参构造

必须有set方法和无参构(默认)

<bean id="hello" class="com.huang.pojo.Hello"><property name="address" value="123"></property>
</bean>
public class Hello {private String address;public void setAddress(String address) {this.address = address;}public void show(){System.out.println(address);}
}

2.有参构造

<bean id="hello" class="com.huang.pojo.Hello"><constructor-arg index="0" value="index"/>
</bean><bean id="hello1" class="com.huang.pojo.Hello"><constructor-arg type="java.lang.String" value="type"/>
</bean><bean id="hello2" class="com.huang.pojo.Hello"><constructor-arg name="address" value="name"/>
</bean>
public class Hello {private String address;public Hello(String address) {this.address = address;}public void setAddress(String address) {this.address = address;}public void show(){System.out.println(address);}
}

注意:在spring配置文件中注册多个bean时,只要我们执行以下代码:那么所有的bean都被初始化了

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello1");

三.DI注入创建对象(set注入)

常见数据类型的注入

创建实体类,此处省略get/set方法和toString()方法(一万字)

public class Student {private String name;private Address address;private String[] books;private List<String> hobbys;private Map<String,String> card;private Set<String> games;private String wife;private Properties info;
}
public class Address {private String address;
}

spring配置文件:

<bean id="address" class="com.huang.pojo.Address"><property name="address" value="address1"/>
</bean><bean id="student" class="com.huang.pojo.Student"><property name="name" value="huangjie"/><property name="address" ref="address"/><property name="books"><array><value>book1</value><value>book2</value><value>book3</value></array></property><property name="hobbys"><list><value>hobby1</value><value>hobby2</value><value>hobby3</value></list></property><property name="card"><map><entry key="card1" value="421281"/><entry key="card2" value="12345"/></map></property><property name="games"><set><value>lol</value><value>bob</value><value>coc</value></set></property><property name="wife"><null/></property><property name="info"><props><prop key="info1">200191003</prop><prop key="info2">male</prop><prop key="info3">20</prop></props></property>
</bean>

每个实体类重写toString()然后测试类中测试

四.自动装配

xml配置实现自动装配

一个人有一只猫和一条狗:

看到这句话,我们要能立刻联想到三个类,人,猫,狗,人中有猫和狗两个属性

实体类:

public class Person {private Dog dog;private Cat cat;public Person() {}public Person(Dog dog, Cat cat) {this.dog = dog;this.cat = cat;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}public Cat getCat() {return cat;}public void setCat(Cat cat) {this.cat = cat;}
}
public class Cat {private String name = "喵喵";public void shut(){System.out.println(name);}
}
public class Dog {private String name = "旺旺";public void shut(){System.out.println(name);}
}

/beans.xml

<bean id="cat" class="com.huang.pojo.Cat"/>
<bean id="dog" class="com.huang.pojo.Dog"/>
<bean id="person" class="com.huang.pojo.Person" autowire="byType"></bean>

自动装配autowire可选值:

  • byname: 需bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
  • bytype:需bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

也就是说:byname和bytype一个取决于id,一个取决于class,两者都有局限性

注解实现自动装配

配置文件:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><bean id="cat" class="com.huang.pojo.Cat"/><bean id="dog" class="com.huang.pojo.Dog"/><bean id="dog2" class="com.huang.pojo.Dog"/><bean id="people" class="com.huang.pojo.People"/>
</beans>

实体类:主要看People类

import org.springframework.beans.factory.annotation.Autowired;public class People {@Autowiredprivate Cat cat;@Autowired@Qualifier("dog2")private Dog dog;public People() {}public People(Cat cat, Dog dog) {this.cat = cat;this.dog = dog;}public Cat getCat() {return cat;}public Dog getDog() {return dog;}
}
  • @Autowired注解也可以加在set方法上,加在属性上可以省略set方法
  • @Autowired默认按照bytype来装配,如果要指定按byname装配,我们一般配合@Qualifier(“dog2”)来使用

五.spring注解开发

/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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">//扫描包 如果在com.huang下还有Dao,service,controller层,base-package值应该改为com.huang<context:component-scan base-package="com.huang.pojo"/>//开启注解支持<context:annotation-config/></beans>

实体类

@Component   //等同于 <bean id="teacher" class="com.huang.pojo.Teacher "/>
@Scope("prototype")
public class Teacher {private String name;@Value("黄老师")  //等同于 <property name="name" value="黄老师"/>public void setName(String name) {this.name = name;}public String getName() {return name;}
}
  • @Scope(“prototype”) 指定单例模式
  • @Value(“黄老师”)可以放在属性上,此时set方法可以省;也可以放在set方法上

测试类

context.getBean中bean的名字默认为类首字母小写

@Test
public void test7(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Teacher teacher = context.getBean("teacher",Teacher.class);System.out.println(teacher.getName());
}

类似@Component的注解:都是代表将莫个类注册到spring容器中并装配

  • Dao层:@Repository
  • Service层:@Service
  • Controller层:@Controller

在SSM项目整合和springBoot阶段在来总结常见注解

六.spring切面编程(待续)

七.spring整合mybatis(之前博客有)

下一阶段总结springMVC的执行原理,完了之后开始springBoot之旅

这篇关于spring可能会迟到,但不会缺席的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/251629

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项