Spring初学入门(跟学笔记)

2024-05-15 16:44

本文主要是介绍Spring初学入门(跟学笔记),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Spring概述

Spring是一款主流的Java EE轻量级开源框架。
Spring的核心模块:IoC(控制反转,指把创建对象过程交给Spring管理 )、AOP(面向切面编程,在不修改源代码的基础上增强代码功能)

二、Spring入门

2.1 入门案例开发步骤

  1. 引入spring相关依赖
  2. 创建类,定义属性和方法
  3. 按照spring要求创建配置文件
  4. 在spring配置文件中配置相关信息
  5. 进行测试

2.2 案例开发实例
创建一个spring maven项目名为Spring6,再在Spring6下面创建一个名为Spring-first的模块,在此模块下的pom.xml里增加依赖。

<dependencies><!-- 第2.1个spring context依赖(这是spring的基础依赖) --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.7</version><!--如果是JDK17及以上的,就写6.0.0版本+--></dependency><!--第2.2个spring junit依赖 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version>
<!--            <scope>test</scope>--></dependency></dependencies>

复制之后,点击m小图标进行联网下载。
在这里插入图片描述

创建一个User类,里面创建一个add方法。

public class User {public void add(){System.out.println("添加。。。");}
}

创建一个bean.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--    完成user对象创建-->
<!--    id属性:唯一标识-->
<!--    class属性:要创建的对象所在类的全路径--><bean id="user" class="com.hbnu.spring6.User"/>
</beans>

再写一个测试类,测试方法调用

public class TestUser {
//这里的注解要导入 import org.junit.Test;@Testpublic void testUser(){//加载spring配置文件,创建对象ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");//获取创建的对象User user = (User) applicationContext.getBean("user");//getBean --> xml文件里的id值System.out.println(user);//使用对象调用方法进行测试user.add();}
}

如果是利用反射创建对象呢?

//利用反射创建对象public void testUserObject1() throws Exception {Class clazz=Class.forName("com.hbnu.spring6.User");//调用方法创建对象User user = (User) clazz.getDeclaredConstructor().newInstance();System.out.println(user);user.add();}

三、容器:IoC

IoC是Inversion of Control的简写,译为”控制反转”,是一种设计思想(不是一种技术),是一个重要的面向对象编程法则,是Spring框架中最重要的核心组件之一。
Spring通过IoC容器来管理所有的Java对象的实例化和初始化,控制对象与对象之间的依赖关系。我们将由IoC容器管理的Java对象称为Spring Bean,它与new一个对象没有区别。

控制反转,反转的是什么?

  • 将对象的创建权力交出去,交给第三方容器负责
  • 将对象和对象之间关系的维护权交出去,交给第三方容器负责

控制反转这种思想如何实现?

  • DI :依赖注入

3.1 依赖注入
指Spring创建对象的过程中,将对象依赖属性通过配置进行注入
常见方式包括两种:

  • set注入
  • 构造注入

3.2 IoC容器在Spring的实现

IoC容器中管理的组件也叫做bean,在创建bean之前,首先要创建IoC容器。Spring提供了两种实现方式:

BeanFactory 是IoC容器的基本实现,是Spring内部使用的接口,面向Spring本身,不提供给开发人员

ApplicationContext 是BeanFactory的子接口,提供了更多高级特性,面向Spring的使用者

ApplicationContext的主要实现类:
在这里插入图片描述

3.3 基于xml管理bean

1.获取bean

xml文件里配置id值

<bean id="user" class="com.hbnu.spring6.User"/>

①根据id获取

public void test1(){ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");User user = (User) applicationContext.getBean("user");
}

②根据id和类型获取

public void test2(){ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");User user = applicationContext.getBean("user",User.class);
}

③根据类型获取

public void test3(){ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");User user = applicationContext.getBean(User.class);
}

且当根据类型获取bean的时候,要求ioc容器里指定类型(class)的bean只允许有一个,配置了两个会报错。即下面两行是错的。

<bean id="user" class="com.hbnu.spring6.User"/>
<bean id="user1" class="com.hbnu.spring6.User"/>

注意:①如果组件类实现了接口,根据接口类型可以获取bean吗?可以,前提是bean唯一 ②如果一个接口有多个实现类,这些实现类都配置了bean,根据接口类型可以获取bean吗?不行,因为bean不唯一

以下是错误的:

<!--    一个接口实现类获取过程--><bean id="UserDaoImpl" class="com.hbnu.spring6.bean.UserDaoImpl"/><bean id="PersonDaoImpl" class="com.hbnu.spring6.bean.PersonDaoImpl"/>
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");UserDao userDao = applicationContext.getBean(UserDao.class);System.out.println(userDao);userDao.run();

2.依赖注入

如果是原生Java,set方法和构造器方法怎么注入?

		//set方法注入Book book=new Book();book.setAuthor("牛顿");//通过构造器注入Book book1=new Book("1","鹤");

接着看用配置文件如何注入。。。

①根据set方法注入
创建一个Book类,定义属性,生成属性set方法之后,在spring配置文件里配置。。。

<!--    1.set方法完成注入--><bean id="book" class="com.hbnu.spring6.DI.Book"><property name="bname" value="spring"/><property name="author" value="小唐"/></bean><!--name对应Book类里面的属性-->

测试一下set方法注入:

@Testpublic void testSet(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-DI.xml");Book book = (Book)applicationContext.getBean("book");System.out.println(book);//Book{bname='spring', author='小唐'}}

输出Book类里重写的toString

②根据构造器注入
创建一个Book类,定义属性,生成有参数的构造方法,在spring配置文件里配置。。。

<!--    2.有参构造注入--><bean id="bookCons" class="com.hbnu.pojo.Book"><constructor-arg name="author" value="小李"/><constructor-arg name="name" value="Spring学习之有参构造注入"/></bean>

测试跟上面一样,只是配置文件里写的内容不一样了

3.特殊值处理

①字面量赋值
②null值 用null标签表示空值
③xml实体 用&lt;&gt;表示大小尖括号
④CDATA节 <![CDATA[a<b]]>

4.为对象类型属性赋值

创建两个类对象:dept和emp
在emp类里写一个work方法

public class Emp {//对象类型属性:员工属于某个部门private Dept dept;private String ename;private String age;public void work(){System.out.println(ename+" is working...");dept.info();}...}

①引用外部bean

    1.引用外部bean注入<bean id="dept" class="com.hbnu.DITest.Dept"><property name="dname" value="安保部"/></bean><bean id="emp" class="com.hbnu.DITest.Emp">
<!--        普通类型属性注入--><property name="ename" value="lucy"/><property name="age" value="23"/>
<!--        对象类型属性注入-->
<!--        表示引入外部bean,这里的ref是上面id为dept的值--><property name="dept" ref="dept"/></bean>

②使用内部bean

<!--    2.使用内部bean注入--><bean id="emp" class="com.hbnu.DITest.Emp"><property name="age" value="25"/><property name="ename" value="joey"/><property name="dept"><bean id="dept" class="com.hbnu.DITest.Dept"><property name="dname" value="财务部"/></bean></property></bean>

③ 使用级联赋值

<!--    3.级联赋值--><bean id="emp" class="com.hbnu.DITest.Emp"><property name="ename" value="mary"/><property name="age" value="25"/><property name="dept" ref="dept"/><property name="dept.dname" value="测试部"/></bean><bean id="dept" class="com.hbnu.DITest.Dept"><!--这一行可要可不要,仅作参考,上面已经给部门名称赋值为了测试部--><property name="dname" value="技术部"/></bean>

测试

@Testpublic void testemp(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-ditest.xml");Emp emp=applicationContext.getBean("emp",Emp.class);emp.work();}

5.复杂类型注入

①数组类型注入

在 Emp 类里面加一个数组属性private String[] hobbies;后生成get、set方法,编写xml文件

<!--    数组类型赋值--><bean id="dept" class="com.hbnu.DITest.Dept"/><bean id="emp" class="com.hbnu.DITest.Emp"><property name="ename" value="tom"/><property name="age" value="34"/><property name="dept" ref="dept"/><property name="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property></bean>

②集合类型注入

在Dept类里加一个员工listprivate List<Emp> empList;,一个部门里可以有多个员工

<!--    为list集合赋值--><bean id="emp1" class="com.hbnu.DITest.Emp"><property name="ename" value="tom1"/><property name="age" value="31"/></bean><bean id="emp2" class="com.hbnu.DITest.Emp"><property name="ename" value="tom2"/><property name="age" value="32"/></bean><bean id="dept" class="com.hbnu.DITest.Dept"><property name="dname" value="技术部"/><property name="empList"><list><ref bean="emp1"></ref><ref bean="emp2"></ref></list></property></bean>

输出技术部里的员工:

Dept{dname='技术部', empList=[Emp{dept=null, ename='tom1', age='31', hobbies=null}, Emp{dept=null, ename='tom2', age='32', hobbies=null}]}

③map类型注入

创建Student和Teacher类
对于学生来说,一个学生对应多个讲师

<!--    map类型注入--><bean id="teacherone" class="com.hbnu.DITest.Teacher"><property name="tid" value="121"/><property name="tname" value="莉莉"/></bean><bean id="teachertwo" class="com.hbnu.DITest.Teacher"><property name="tid" value="122"/><property name="tname" value="小月"/></bean><bean id="student" class="com.hbnu.DITest.Student"><property name="sid" value="111"/><property name="sname" value="张三"/><property name="teacherMap"><map><entry><key><value>10010</value></key><ref bean="teacherone"></ref></entry><entry><key><value>10011</value></key><ref bean="teachertwo"></ref></entry></map></property></bean>

输出{10010=Teacher{Tname='莉莉', Tid='121'}, 10011=Teacher{Tname='小月', Tid='122'}}

④引用集合类型的bean

创建一个Lesson类(getter、setter、重写toString),再在Student类里面加一个 public List<Lesson> lessonList;,一个学生有多个老师,选多门课

<bean id="lesson1" class="com.hbnu.DITest.Lesson"><property name="lessonname" value="前端开发"/></bean><bean id="lesson2" class="com.hbnu.DITest.Lesson"><property name="lessonname" value="java开发"/></bean><bean id="teacher1" class="com.hbnu.DITest.Teacher"><property name="tname" value="张老师"/><property name="tid" value="001"/></bean><bean id="teacher2" class="com.hbnu.DITest.Teacher"><property name="tname" value="王老师"/><property name="tid" value="002"/></bean><bean id="student" class="com.hbnu.DITest.Student"><property name="sid" value="1000"/><property name="sname" value="lucy"/><!--        注入list、map类型属性--><property name="lessonList" ref="lessonlist"/><property name="teacherMap" ref="teachermap"/></bean><util:list id="lessonlist"><ref bean="lesson1"/><ref bean="lesson2"/></util:list><util:map id="teachermap"><entry><key><value>10010</value></key><ref bean="teacher1"/></entry><entry><key><value>10011</value></key><ref bean="teacher2"/></entry></util:map>
</beans>

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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

输出:

{10010=Teacher{Tname='张老师', Tid='001'}, 10011=Teacher{Tname='王老师', Tid='002'}}
[Lesson{lessonname='前端开发'}, Lesson{lessonname='java开发'}]

脱离数据库思想注入复杂类型,创建一个dataType类,里面拥有private String[] arr;private List<String> lists;private Map<String,String> maps;private Properties properties;属性,编写xml文件:

<!--        注入复杂类型--><bean id="dataType" class="com.hbnu.pojo.DataType">
<!--                1.注入数组类型--><property name="arr"><list><value>斯蒂芬金</value><value>毛姆</value><value>陀思妥耶夫斯基</value><value>茨威格</value></list></property><!--                2.注入list集合类型--><property name="lists"><list><value>马克吐温</value><value>马克吐温</value><value>马克吐温</value><value>马克吐温</value></list></property><!--                3.注入map集合类型--><property name="maps"><map><entry key="username" value="雨果"/><entry key="age" value="1984"/></map></property><!--                4.注入properties--><property name="properties"><props><prop key="driverClass">com.mysql.cj.jdbc.Driver</prop><prop key="url">jdbc:mysql://localhost:3306/class2110?serverTimezone=GMT&amp;useSSL=false</prop><prop key="username">root</prop><prop key="password">123456</prop></props></property></bean>

6.p命名空间

<!--    p命名空间注入--><bean id="studentp" class="com.hbnu.DITest.Student"p:sid="100" p:sname="mary" p:lessonList-ref="lessonlist" p:teacherMap-ref="teachermap">

头部加一行

xmlns:p="http://www.springframework.org/schema/p"

7.bean的作用域

概念
在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围

取值含义创建对象的时机
singleton在IOC容器中,这个bean的对象始终为单实例IOC容器初始化时
prototype这个bean在IOC容器中有多个实例获取bean时
    @Testpublic void testOrders(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-ditest.xml");Orders orders=applicationContext.getBean("orders",Orders.class);Orders orders2=applicationContext.getBean("orders",Orders.class);System.out.println(orders);System.out.println(orders2);}

8.bean的生命周期

1.bean对象创建(调用无参数构造)
2.给bean对象设置相关属性
3.bean后置处理器(初始化之前)
4.bean对象初始化(调用指定初始化方法)
5.bean后置处理器(初始化之后)
6.bean对象创建完成
7.bean对象销毁(配置指定销毁的方法)
8.IoC容器关闭

3.4 基于注解管理bean
步骤:1.引入依赖 2.开启组件扫描 3.使用注解定义bean 4.依赖注入

1.开启组件扫描
开启组件扫描(开启此功能后,spring就会自动从扫描指定的包,及其子包下的所有类。如果类上使用了@Component注解,就将该类装配到容器中)

<context:component-scan base-package="com.hbnu"></context:component-scan>

2.使用注解定义bean

注解说明
@Component该注解用于描述Spring中的bean,仅仅表示容器中的一个组件,并且可以作用在应用的任何层次,例如Service层,Dao层。
@Repository该注解用于将数据访问层(Dao层)的类标识为S加粗样式pring中的bean,其功能与@Component相同
@Service该注解通常作用在业务层(Service层),用于将业务层的类标识为Spring中的bean,其功能与@Component相同
@Controller该注解通常作用在控制层(如SpringMVC中的Controller),用于将控制层的类标识为Spring中的bean,其功能与@Component相同

3.@Autowired注入

注入可以理解为导包,注入后就可以调用方法
在controller里注入service,在service里注入dao

写个测试感受一下@Autowired注入…项目结构如下:
在这里插入图片描述
首先在bean.xml里编写代码进行包扫描,然后创建UserDao以及其实现类,
在这里插入图片描述
在这里插入图片描述

接着创建UserService以及其实现类,并将UserDao注入到UserService中。
在这里插入图片描述
在这里插入图片描述
最后创建UserController,将UserService注入后进行测试。
在这里插入图片描述

public class Test {public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");UserController userController=applicationContext.getBean(UserController.class);userController.add();}
}

在这里插入图片描述

这篇关于Spring初学入门(跟学笔记)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis