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整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

Java Lettuce 客户端入门到生产的实现步骤

《JavaLettuce客户端入门到生产的实现步骤》本文主要介绍了JavaLettuce客户端入门到生产的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录1 安装依赖MavenGradle2 最小化连接示例3 核心特性速览4 生产环境配置建议5 常见问题

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

Java 的ArrayList集合底层实现与最佳实践

《Java的ArrayList集合底层实现与最佳实践》本文主要介绍了Java的ArrayList集合类的核心概念、底层实现、关键成员变量、初始化机制、容量演变、扩容机制、性能分析、核心方法源码解析、... 目录1. 核心概念与底层实现1.1 ArrayList 的本质1.1.1 底层数据结构JDK 1.7

Java Map排序如何按照值按照键排序

《JavaMap排序如何按照值按照键排序》该文章主要介绍Java中三种Map(HashMap、LinkedHashMap、TreeMap)的默认排序行为及实现按键排序和按值排序的方法,每种方法结合实... 目录一、先理清 3 种 Map 的默认排序行为二、按「键」排序的实现方式1. 方式 1:用 TreeM

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node