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应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav