Spring学习笔记

spring全家桶:https://www.springcloud.cc/spring-reference.html

spring中文文档:http://c.biancheng.net/spring/

spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的容器框架。

IOC本质:实质上是一种设计思想,DI(依赖注入)是实现ioc的一种方法。。没有ioc的程序中,使用面向对象编程,对象的创建于对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方

注意:使用xml配置的Bean,Bean的定义信息是和实现分离的,采用注解的方式可以把二者合为一体。

定义:控制反转是一种通过描述(XML或注解)并通过第三方生产获取特定对象的方式,在spring中实现控制反转的是ioc容器,实现方法是依赖注入

1、由 Spring IoC 容器管理的对象称为 Bean,Bean 根据 Spring 配置文件中的信息创建。
Properties 配置文件主要以 key-value 键值对的形式存在,只能赋值,不能进行其他操作,适用于简单的属性配置。

2、ClassPathXmlApplicationContext 该类从类路径 ClassPath 中寻找指定的 XML 配置文件,并完成 ApplicationContext 的实例化工作

3、

通过set注入

导入pom依赖

    <dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
</dependencies>

2、学生信息

package com.zheng.pojo;

public class Student {
private String name;
private int age; public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public int getAge() {
return age;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

3、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-3.0.xsd"> <bean id="student" class="com.zheng.pojo.Student">
<property name="name" value="小红"></property>
<property name="age" value="12"></property>
</bean> </beans>

4、测试

package com.zheng.pojo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentTest {
public static void main(String[] args) {
//获取容器
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student st1 = (Student) context.getBean("student");
System.out.println("姓名:" + st1.getName());
System.out.println("年龄:" + st1.getAge());
System.out.println(st1.toString());
}
}

通过构造函数注入

    public Student(String name, int age) {
this.name = name;
this.age = age;
}

xml文件

    <bean id="student1" class="com.zheng.pojo.Student">
<constructor-arg name="name" value="小黑"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>

测试

System.out.println("通过构造函数");
Student st2 = (Student) context.getBean("student1");
System.out.println(st2.toString());

结果

注意:

在application中导入其他配置

<?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">
<import resource="beans.xml"/>
<import resource="beans1.xml"/> </beans>

获得容器方法

  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

依赖注入

构造函数注入

setter注入

复杂类型依赖注入

student


package com.zheng.dao; import java.util.*; 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 String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} public String[] getBooks() {
return books;
} public void setBooks(String[] books) {
this.books = books;
} public List<String> getHobbys() {
return hobbys;
} public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
} public Map<String, String> getCard() {
return card;
} public void setCard(Map<String, String> card) {
this.card = card;
} public Set<String> getGames() {
return games;
} public void setGames(Set<String> games) {
this.games = games;
} public String getWife() {
return wife;
} public void setWife(String wife) {
this.wife = wife;
} public Properties getInfo() {
return info;
} public void setInfo(Properties info) {
this.info = info;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}

address

package com.zheng.dao;

public class Address {
private String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}

配置文件

<?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">
<bean id="address" class="com.zheng.dao.Address">
<property name="address" value="上海"></property>
</bean> <bean id="student" class="com.zheng.dao.Student">
<property name="name" value="小红"></property> <property name="address" ref="address"></property> <!--为数组注入值-->
<property name="books">
<array>
<value>JAVA</value>
<value>PHP</value>
<value>C++</value>
</array>
</property> <!--为list集合注入值-->
<property name="hobbys">
<list>
<value>足球</value>
<value>篮球</value>
<value>乒乓球</value>
</list>
</property> <!--map集合注入值-->
<property name="card">
<map>
<entry key="身份证" value="234567655678987678"></entry>
<entry key="信用卡" value="234567789087654678"></entry>
</map>
</property> <!--set注入-->
<property name="games">
<set>
<value>和平精英</value>
<value>明日之后</value>
<value>王者荣耀</value>
</set>
</property> <!--注入空值-->
<property name="wife">
<null/>
</property> <!--注入properties-->
<property name="info">
<props>
<prop key="学号">B20180702224</prop>
<prop key="姓名">张三</prop>
<prop key="性别">男</prop>
<prop key="年龄">18</prop> </props>
</property> </bean> </beans>

测试

import com.zheng.dao.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}


Bean的作用域

1)singleton
默认值,单例模式,表示在 Spring 容器中只有一个 Bean 实例,Bean 以单例的方式存在。

2)prototype
原型模式,表示每次通过 Spring 容器获取 Bean 时,容器都会创建一个 Bean 实例。

Bean的自动装配

  • byName自动装配
    <bean id="cat" class="com.zheng.pojo.Cat"></bean>
<bean id="dog" class="com.zheng.pojo.Dog"></bean>
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="user" class="com.zheng.pojo.User" autowire="byName">
<property name="name" value="小红"></property> </bean>
  • byType自动装配

<bean class="com.zheng.pojo.Cat"></bean>
<bean class="com.zheng.pojo.Dog"></bean> <!--
byName:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="user" class="com.zheng.pojo.User" autowire="byType">
<property name="name" value="小红"></property>
</bean>

注解自动装配

1、导入约束context约束
2、配置注解支持 <context:annotation-config/>


<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解的支持-->
<context:annotation-config/> </beans>

举例

  • @Autowired
    可以应用到 Bean 的属性变量、属性的 setter 方法、非 setter 方法及构造函数等,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。

@Qualifier
与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。

package com.zheng.pojo;

import org.springframework.beans.factory.annotation.Autowired;

public class User {
@Autowired
private Dog dog; @Autowired
private Cat cat; private String name; 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 String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解的支持-->
<context:annotation-config/> <bean id="cat" class="com.zheng.pojo.Cat"></bean>
<bean id="dog" class="com.zheng.pojo.Dog"></bean> <bean id="user" class="com.zheng.pojo.User"/>
</beans>

两者配合使用

    @Autowired
@Qualifier("dog")
private Dog dog;

<bean class="com.zheng.pojo.Cat"></bean>
<bean id="dog" class="com.zheng.pojo.Dog"></bean>

二、spring注解开发

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--指定扫描下的包,这个包下的注解就会自动生效-->
<context:component-scan base-package="com.zheng.pojo"/> <context:annotation-config/> </beans>

实体类

package com.zheng.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller; @Component //相当于== <bean id="student" class="com.zheng.pojo.Student"/>
public class Student {
@Value("小红") //相当于== <property name="name" value="小红"/>
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

测试

import com.zheng.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student st1 = context.getBean("student", Student.class);//默认的实例名是类的小写
System.out.println(st1.getName()); }
}


衍生的注解(重点理解)
@Component有几个衍生注解,在web开发中,按照mvc三层架构

  • dao【@Repository】
  • service【@Service】
  • controller【@Controller】

四个注解的功能一样,都是代表将某个类注册到spring中,装配Bean

后端框架学习1-----Spring的更多相关文章

  1. 第65节:Java后端的学习之Spring基础

    Java后端的学习之Spring基础 如果要学习spring,那么什么是框架,spring又是什么呢?学习spring中的ioc和bean,以及aop,IOC,Bean,AOP,(配置,注解,api) ...

  2. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

  3. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  4. 后端框架学习3------SpringMVC

    springMVC学习笔记 官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html# ...

  5. 框架学习之Spring(一IOC)----HelloWrod

    一.概述 Spring是一个开源框架,它的核心是控制反转(IOC)和面向切面(AOP).简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架. EE 开发分 ...

  6. Spring框架学习02——Spring IOC 详解

    1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...

  7. 后端框架学习-----mybatis(使用mybatis框架遇到的问题)

    1.配置文件没有注册(解决:在核心配置文件中注册mapper,注册有三种形式.资源路径用斜杆,包和类用点) <mappers> <!--每一个mapper.xml文件都需要在myba ...

  8. 后端框架学习-----mybatis(4)

    文章目录 4.解决属性名和字段名不一致的问题 4.解决属性名和字段名不一致的问题 1.问题.数据库字段名和属性名不一致,导致查出的数据部分为空 2.resultMap(用于解决数据库表中的字段和属性) ...

  9. Springboot 框架学习

    Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...

随机推荐

  1. 日志(logging模块)

    1. 为什么要使用日志(作用) 在学习过程中,写一个小程序或小demo时,遇到程序出错时,我们一般会将一些信息打印到控制台进行查看,方便排查错误.这种方法在较小的程序中很实用,但是当你的程序变大,或是 ...

  2. 英特尔CPU系列

    1.酷睿(Core)系列,主要应用于管理 3D.高级视频和照片编辑,玩复杂游戏,享受高分辨率 4K 显示. 2.奔腾(PenTIum)系列,主要应用于借助功能丰富的处理器,加快便携式 2 合 1 电脑 ...

  3. uniapp中用canvas实现小球碰撞的小动画

    uniapp 我就不想喷了,踩了很多坑,把代码贡献出来让大家少踩些坑. 实现的功能: 生成n个球在canvas中运动,相互碰撞后会反弹,反弹后的速度计算我研究过了,可以参考代码直接用 防止球出边框 防 ...

  4. SpringBoot连接MySQL配置(yml)?

    SpringBoot连接MySQL 需要导入的依赖 <!--这是直接在创建springboot项目时直接勾选生成的--><!--jdbc--><dependency> ...

  5. 总结-LCT

    题单: https://www.zybuluo.com/xzyxzy/note/1027479 LuoguP3203 [HNOI2010]弹飞绵羊 动态加边,删边 #include <cstdi ...

  6. Reader和Writer区别final.finally.finalize区别

    Reader和Writer是字符操作流,Writer是输出的,而Reader是输入的. 首先找到一个文件,比如:File file=new File("."+File.separa ...

  7. Java 断点下载(下载续传)服务端及客户端(Android)代码

    原文: Java 断点下载(下载续传)服务端及客户端(Android)代码 - Stars-One的杂货小窝 最近在研究断点下载(下载续传)的功能,此功能需要服务端和客户端进行对接编写,本篇也是记录一 ...

  8. Windows批量修改文件

    如图我是建立了壁纸文件夹 Windows自带的排序方式 如何不用自带的呢? 在这个文件夹里面建一个.txt文件 如下 ok第二步骤 将UTF-8格式改为ANSI格式 点击文件-另存为ANSI格式-替换 ...

  9. 【c#语言简单算法】1-角谷猜想

    角谷猜想 算法目的 一个正整数x,如果是奇数就乘以3再加1,如果是偶数就析出偶数因数2ⁿ,这样经过若干个次数,最终回到1. 这里计算0-100的所有计算过程 代码实现 for (int n = 1; ...

  10. C++工厂方法模式讲解和代码示例

    在C++中使用模式 使用示例: 工厂方法模式在 C++ 代码中得到了广泛使用. 当你需要在代码中提供高层次的灵活性时, 该模式会非常实用. 识别方法: 工厂方法可通过构建方法来识别, 它会创建具体类的 ...