学习记录
一、Spring 框架组成
Spring 框架的分为7个模块,组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:
- 1.核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
- 2.Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
- 3.Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。
- 4.Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
- 5.Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
- 6.Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
- 7.Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。
Spring 框架的功能可以用在任何 J2EE 服务器中,大多数功能也适用于不受管理的环境。Spring 的核心要点是:支持不绑定到特定 J2EE 服务的可重用业务和数据访问对象。毫无疑问,这样的对象可以在不同 J2EE 环境 (Web 或 EJB)、独立应用程序、测试环境之间重用。
二、Spring框架的核心——Spring Framework
1、简介
- Spring Framework的创始人——Rod Johnson,悉尼大学音乐学博士,头发健在
- Spring理念:使现有的技术更加容易使用,本身是一个大杂烩。
- SSH:Struct + Spring + Hibernate
SSM:SpringMVC + Spring + Mybatis
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
AI 代码解读
2、优点
- Spring 是一个开源的免费的框架(容器)
- Spring 是一个轻量级的、非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事务的处理,对框架整合的支持
==总结:Spring 是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架==
3、拓展
现代化的Java 开发,就是基于Spring的开发
Spring Boot
- 一个快速开发的脚手架
- 基于SpringBoot可以快速的开发单个微服务
- 约定大于配置!
- 学习前提:掌握Spring以及SpringMVC!
Spring Cloud
- SpringCloud是基于SpringBoot实现的
三、IOC理论
- USerDao 接口——dao包
- UserDaoImpl 实现USerDao类——dao包
- UserService 业务接口——service包
- UserServiceImpl 业务实现类——service包
业务层service调用dao层,使用组合
,在此,就把dao层引入了service
private UserDao userdao = new UserDaoImpl();
AI 代码解读
- 在使用的时候首先用户实际调用业务层service,dao层不需要接触
IOC本质
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入(Dependency Injection,DI)。
HelloSpring运行结果中,Hello 代表的是类名,str 代表的是自己设置的键的名字,Spring代表的是键所对应的设置的值
思路:在Hello类中定义自己的变量键,并生成get、set和tostring方法,在beans.xml文件中放置自己的键值对并设置id属性以及要引用的类,继而在测试类中得以取值并调用
官网地址:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html
控制反转
- Hello 对象是Spring创建的
- hello 对象的属性是由Spring 容器设置的
- 控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的
- 反转:程序本身不创建对象,而变成被动的接收对象
- 依赖注入:就是利用set方法来进行注入的
IOC 是一种编程思想,由主动的编程编程被动的接收
因此,当我们需要实现不同的操作时,只需要在xml配置文件中进行修改,所谓的IOC,就是==对象由Spring来创建,管理,装配!==
IOC创建对象方式
使用无参构造创建对象,默认!
==不需要new对象去==
User.java
package com.kuang.pojo;
public class User {
private String name;
public User(){
System.out.println("使用了User的无参构造的方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
AI 代码解读
MyTest.java
import com.kuang.pojo.User;
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");
User user = (User)context.getBean("user");
user.show();
}
}
AI 代码解读
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User">
<!-- collaborators and configuration for this bean go here -->
<property name="name" value="小猪"/>
</bean>
</beans>
AI 代码解读
使用有参构造创建对象
1、下标赋值
<!-- 第一种,下标赋值 -->
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg index="0" value="小猪"/>
</bean>
AI 代码解读
2、通过类型创建,
<!-- 第二种,不建议使用 -->
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="小猪"/>
</bean>
AI 代码解读
3、通过参数名设置
<!-- 第三种,直接通过参数名来设置 -->
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg name="name" value="小猪"/>
</bean>
AI 代码解读
四、Spring配置
1、别名
User.java
package com.kuang.pojo;
public class User {
private String name;
public User(){
System.out.println("使用了User的无参构造的方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
AI 代码解读
MyTest.java
import com.kuang.pojo.User;
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");
//此处的别名uesrNew,也可正确取得对象
User user = (User)context.getBean("userNew");
user.show();
}
}
AI 代码解读
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User">
<!-- collaborators and configuration for this bean go here -->
<property name="name" value="小猪"/>
<property name="str" value="字符串"/>
</bean>
<!-- 别名,如果添加类别名,我们也可以使用别名获取到这个对象 -->
<alias name="user" alias="userNew"/>
</beans>
AI 代码解读
2、bean的配置
id:bean的唯一标识符,相当于对象名
class:bean 对象所对应的权限定名:包名+类型
name:别名,并且name可以同时取多个别名
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User" name="user-2,user-1">
<!-- collaborators and configuration for this bean go here -->
<property name="name" value="小猪"/>
<property name="str" value="字符串"/>
</bean>
AI 代码解读
3、import
一般用与团队开发使用,它可以将多个配置文件,导入合并成为一个
假设现在项目中有多人开发,在使用不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import
将所有人的bean.xml合并为一个总的,
- 张三
beans.xml
- 李四
beans2.xml
- 王五
beans3.xml
- 总的
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!---->
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans.xml"/>
</beans>
AI 代码解读
五、DI依赖注入
1、构造器注入
2、Set注入【重点】
- 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
【环境搭建】
- 复杂类型
- 真实测试对象
3、其他方式注入
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User">
<!-- collaborators and configuration for this bean go here -->
<property name="name" value="小猪"/>
<property name="str" value="字符串"/>
</bean>
<!-- 别名,如果添加类别名,我们也可以使用别名获取到这个对象 -->
<alias name="user" alias="userNew"/>
</beans>
AI 代码解读
4、C命名和P命名空间注入
p命名空间的注入,直接注入属性的值
c命名空间注入,通过构造器注入:construct-args
使用:
测试:
注意点:
p命名和c命名空间不能直接使用,需要导入xml约束xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
六、bean的作用域
1. 单例模式( Spring默认机制 )
2. 原型模式:每次从容器中get的时候,都会产生一个新对象!
3. 其余的request、session、application,这些只能在web开发中使用到!
七、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中由三种装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配bean【重要!】
7.1 测试
- 环境搭建
构建自己的对象及类,并生成get、set、toString方法以及构造器constructs
7.2 ByName自动装配
7.3. ByType自动装配
==前提是需要在方法中包含该对象并且在xml中已经注入==
小结
- byname 的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法值一致
- bytype 的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性类型一致
7.4 使用注解实现自动装配
jdk1.5支持的注解,Spring2.5支持注解
使用注解须知:
- 导入约束
- 配置注解的支持
- 科普
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法,前提是在这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname!@Nullable
字段标记类这个注解,说明这个字段可以为null
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@QUalifier(value=“xxx“)去配置@Autowired的使用,指定一个唯一的bean对象注入
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byname的方式实现,而且必须要求这个对象存在【常用】
- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
- 执行顺序不同,@Autowired 通过byType的方式实现。@Resource默认通过byname的方式实现
7.5、常用的依赖
八、注解说明
- @Autowired:自动装配通过类型-名字 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
- @Resource: 自动装配通过名字-类型
- @Nullable:字段标记了这个注解,说明这个字段可以为null
- @context:组件,放在类上,说明这个类被Spring管理了,就是bean
使用注解开发 使用注解开发就必须保证aop的包都导入
使用注解需要导入context约束,增加注解的支持!
1. @context bean注入 指定要扫描的包,在这个包下的注解就会生效
2. @Value 属性如何注入
3. 衍生的注解
@context有几个衍生的注解,我们在web开发中,会按照mvc三层架构分层!
- dao 【@Repository】
- service 【Service】
- controller【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring容器中进行装配Bean
4. 自动装配值
- @Autowired:自动装配通过类型-名字 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
- @Resource: 自动装配通过名字-类型
- @Nullable:字段标记了这个注解,说明这个字段可以为null
AI 代码解读
5. 作用域
@Scope("singleton")——单例模式
@Scope("prototype")——原型模式
6. 小结
xml与注解:
- xml更加万能,适用于任何场合!维护简单方便
- 注解——不是自己的类不能使用,维护相对复杂!
xml与注解最佳实践:
- xml用来管理bean
- 注解只负责完成属性的注入
- 我们在使用的过程中,只需要注意一个问题:必须让注解生效,需要开启注解的支持。——指定要扫描的包,在这个包下的注解就会生效
九、 使用Java的方式配置Spring
我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心的功能,配置类
//这个也会被Spring容器托管,注册到容器中,因为它本来就是一个@Component
//@Configuration代表这个是一个配置类,就和我们之前看的beans.xml
@Configuration
@ComponentScan("需要扫的包名")
@Importe(类名)
public class AppConfig {
// 注册一个bean,就相当于我们之前写的一个bean标签
// 这个方法的名字,就相当与bean标签中的id属性
// 这个方法的返回值,就相当与bean标签中的class属性
@Bean
public MyService myService() {
return new MyServiceImpl();// 就是返回要注入到bean的对象
}
}
AI 代码解读
等价与
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
AI 代码解读
测试类
这种纯Java的配置方式,在SpringBoot中随处可见