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. 使用 for 循环 打印 9X9乘法表

    C 语言自学之99乘法表 请使用for循环,倒序打印9*9乘法表 1 #include <stdio.h> 2 3 int main() 4 { 5 int i,j,result;//定义 ...

  2. day21--Java集合04

    Java集合04 9.Set接口方法 Set接口基本介绍 无序(添加和取出的顺序不一致),没有索引 不允许重复元素,所以最多只有一个null JDK API中接口的实现类有: Set接口的常用方法:和 ...

  3. java学习第七天注解.day19

    注解 可以使用注解来修饰类中的成员信息 "注解,可以看作是对 一个 类/方法 的一个扩展的模版 元注解 注解:用来贴在类/方法/变量等之上的一个标记,第三方程序可以通过这个标记赋予一定功能 ...

  4. Spring Boot部署方法

    Spring Boot部署方法     网上搜到的部署方法无非是打成jar包,然后shell执行nohup java调用jar命令,或者是打成war包然后部署到tomcat或者jetty容器上面. S ...

  5. PHP一句话简单免杀

    PHP一句话简单免杀 原型 几种已经开源的免杀思路 拆解合并 <?php $ch = explode(".","hello.ev.world.a.l"); ...

  6. 安卓平台RTMP推流或轻量级RTSP服务(摄像头或同屏)编码前数据接入类型总结

    很多开发者在做Android平台RTMP推流或轻量级RTSP服务(摄像头或同屏)时,总感觉接口不够用,以大牛直播SDK为例 (Github) 我们来总结下,我们常规需要支持的编码前音视频数据有哪些类型 ...

  7. gem5使用记录,gem5入门学习和环境配置。

    工作需要,对gem5有点兴趣,准备自己研究一下,gem5新手刚开始入门学习,主要参考了网上其他前人的文章和官方说明. 目前网上的前人文章有部分有点太早了,稍显过时,我结合自己的经历来整理一下流程. g ...

  8. 从EDR的火热看安全产品的发展

    从EDR的火热看安全产品的发展 2021年4月8日23:13 当开始写这篇博客时,外面正是护网进行得如火如荼的时候.作为一个产品经理,在吃瓜的同时,也在思考着安全产品的发展.这几年一些看得到的变化在深 ...

  9. MQ的消息丢失/重复/积压的问题解决

    在我们实际的开发过程中,我们肯定会用到MQ中间件,常见的MQ中间件有kafka,RabbitMQ,RocketMQ.在使用的过程中,我们必须要考虑这样一个问题,在使用MQ的时候,我们怎么确保消息100 ...

  10. Django提交时报错

    错误描述: RuntimeError at /saveBlog You called this URL via POST, but the URL doesn't end in a slash and ...