彷徨 | spring框架学习笔记二

2023-11-03 04:40

本文主要是介绍彷徨 | spring框架学习笔记二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

 

10 . spring分模块配置开发

11 . 注入复杂属性类型

12 . IOC 和 DI 的区别

13 . spring框架的bean管理(注解)

14 AOP

15 spring的AOP操作


10 . spring分模块配置开发

spring框架分模块配置开发有俩种方式

第一种 : 加载XML配置文件的时候,加载多个配置文件

/*** 测试分模块配置开发,引入多个配置文件
*/
@Test
public void testUser() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");UserDao userDao = context.getBean("userDao",UserDao.class);userDao.add();UserServices userServices = context.getBean("userServices",UserServices.class);userServices.add();
}

第二种 : 在一个配置文件中导入另一个配置文件

<import resource="classpath:applicationContext2.xml"/>

11 . 注入复杂属性类型

第一步 : 定义一个类,里面设置复杂属性类型 , 包括数组,list集合,map集合,properties配置文件

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Customer {private String[] arrs;private List<String> list;private Map<String,String> map;private Properties properties;public void setArrs(String[] arrs) {this.arrs = arrs;}public void setList(List<String> list) {this.list = list;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}public void show() {System.out.println("数组arrs:" + Arrays.toString(arrs));System.out.println("List集合:" + list);System.out.println("Map集合" + map);System.out.println("Properties配置文件" + properties);}
} 

第二步 : 通过XML配置文件 , 给复杂类型属性赋值

数组,List集合,Map集合都是用<property>标签

        数组和List集合采用<list>标签里面用<value>添加属性值

        map集合用<Map>标签,里面<entry>标签,key值为键值,value为键对应的值

	<bean id="customer" class="com.edu.xiaoniu.user.Customer"><!-- 数组 --><property name="arrs"><list><value>张俊杰</value><value>张三</value><value>李四</value></list></property><!-- list集合 --><property name="list"><list><value>Tom</value><value>Jim</value><value>JiJi</value></list>		</property><!-- Map集合 -->	<property name="map"><map><entry key="学号:11" value="张俊杰"></entry><entry key="学号12" value="张三"></entry><entry key="学号13" value="张飞"></entry></map></property><!-- 配置文件properties --><property name="properties"><props><prop key="driverClass">com.mysql.jdbc.Driver</prop><prop key="jdbcurl">jdbc://mysql</prop></props></property></bean>

第三步 : 测试

	/*** 测试复杂类型注入*/@Testpublic void testUser() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Customer customer = context.getBean("customer",Customer.class);customer.show();}

12 . IOC 和 DI 的区别

1 ioc:控制反转,把对象创建不是new出来的,通过spring配置出来的
(1)spring创建对象
2 di:依赖注入
(1)属性注入,向类里面属性中设置值
3 ioc和di关系:
(1)di需要在ioc基础之上完成

(2)向类里面设置属性值,首先创建类的对象,使用对象才可以设置

13 . spring框架的bean管理(注解)

1 : 注解介绍

1 .1 什么是bean管理
(1)创建对象
(2)注入属性
1.2  使用注解方式实现创建对象和注入属性
1.3  什么是注解?
(1)注解代码中特殊标记
(2)写法: @注解名称(属性名称1=属性值1, 属性名称2=属性值2)
(3)注解可以使用类上面、使用方法上面、使用在属性上面

(4)为什么使用注解:替代配置文件实现一部分功能,简化配置

2 : 注解开发过程

2.1 导入基本jar包和aop jar包spring-aop-4.2.4.RELEASE.jar

2.2 创建类和方法

public class UserDao {public void add() {System.out.println("我是UserDao类的add()方法");}
}

2.3创建spring配置文件,引入约束

做spring的ioc注解开发,引入新的约束

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --></beans>

2.4 开启注解扫描

在配置包里面找所有类的上面,方法的上面和属性上面的注解,根据不同的注解实现不同的功能

	<!-- 开启注解扫描 --><!-- 到配置包里面找所有类的上面,方法上面和属性上面注解,根据不同的注解实现不同的功能 --><context:component-scan base-package="com.edu.xiaoniu.bean"></context:component-scan>

3 : 注解创建对象

3.1spring创建对象注解

在创建对象所在类上面使用注解实现

value的值相当于XML配置文件中的id对应的值

@Component(value="userDao")
public class UserDao {public void add() {System.out.println("我是UserDao类的add()方法");}
}

3.2 注解创建对象是单实例还是多实例

@Scope("prototypy")为多实例,如果不写,默认单实例

@Component(value="userDao")
@Scope("prototype")
public class UserDao {public void add() {System.out.println("我是UserDao类的add()方法");}
}

spring创建对象注解总结

在spring里面创建对象可以使用的注解有四个,这四个注解功能都是一样的,都可以创建对象

4 : 注解注入属性

4.1 注入普通类型属性

在要注入的属性上面使用注解@value

@Component("book")
public class Book {@Value("如何像我一样优秀")private String bname;@Value("张俊杰")private String author;public void show() {System.out.println("书名:" + bname + "\n作者:" + author);}
}

使用配置文件注入属性的时候,需要创建set()方法或者有参数构造

使用注解的方式,不需要set()方法

4.2 注入对象类型属性

第一步 : 新建俩个对象,并使用注解的方式创建

@Component(value="userDao")
public class UserDao {	public void add() {System.out.println("我是UserDao类的add()方法");}
}
@Component("userServices")
public class UserServices {}

第二步 : 三种注解方式:

将一个对象作为属性注入另一个对象中

方式一 : 自动注入,根据属性类型完成属性注入

@Autowired  

方式二 : @Qualifire需要和@Autowired  一起使用,根据Dao里面value值进行对象注入

@Autowired  

@Qualifire(value = "userDao")

方式三 : 这个注解实现上面俩个注解功能

@Resource(name= "userDao")

@Component("userServices")
public class UserServices {//第一个注解,@Autowired自动注入,根据属性类型完成属性注入//@Autowired//第二个注解//@Autowired//@Qualifier("userDao")//第三种注解@Resource(name="userDao")private UserDao userDao;public void add() {System.out.println("我是UserServices类的add()方法");userDao.add();}}

5 : 生命周期配置

@PostConstruct : 相当于init-method

@PreDestroy :相当于desdestroy-method

@Service("beanLive")
public class BeanLive {public void show() {System.out.println("我是BeanLive的show()方法");}@PostConstructpublic void init() {System.out.println("我是init()方法,创建的之后执行我");}@PreDestroypublic void destroy() {System.out.println("我是destroy()方法,销毁的之后执行我");}}

6 : 配置文件和注解混合使用

经常配置文件方式和注解方式混合使用

混合方式:

创建对象使用配置文件实现

	<!-- 测试配置文件和注解的混合使用 --><!-- 使用配置文件创建对象 --><bean id="personDao" class="com.edu.xiaoniu.bean.PersonDao"></bean><bean id="personServices" class="com.edu.xiaoniu.bean.PersonServices"></bean>
public class PersonDao {public void show() {System.out.println("我是PersonDao类的show()方法");}
}
public class PersonServices {//使用注解的方式注入PersonDao对象属性@Resource(name="personDao")private PersonDao personDao;public void show() {System.out.println("我是PersonServices类的show()方法");personDao.show();}}

注入属性使用注解方式实现

public class PersonServices {//使用注解的方式注入PersonDao对象属性@Resource(name="personDao")private PersonDao personDao;public void show() {System.out.println("我是PersonServices类的show()方法");personDao.show();}}

14 AOP

1 : AOP概念和原理

1 .1 AOP:Aspect Oriented Programming面向切面(方面)编程,不通过修改源代码方式实现功能扩展
(1)拦截器是aop思想
1.2  AOP 采取横向抽取机制,取代了传统纵向继承体系重复性代码
(1)纵向机制:BaseServlet
1.3  AOP 底层使用动态代理实现(横向机制)
(1)使用动态代理作用增强类里面的方法,不需要修改源代码实现
(2)分成两种情况
第一种 有接口情况
使用jdk动态代理
使用jdk动态代理,创建接口实现类代理对象
第二种 没有接口情况
使用cglib动态代理

使用cglib动态代理方式,创建类的子类代理对象

15 spring的AOP操作

1 : Aspectj介绍

1 在spring做aop操作,有多种方式,目前使用 aspectj实现
2 在spring2.0开始支持aspectj,aspectj本身不是spring一部分,是单独aop框架,经常把aspectj和spring一起使用,进行aop操作
3 基于aspectj实现aop操作两种方式
(1)基于xml配置文件方式

(2)基于注解方式

2 : AOP操作准备

除了导入基本的jar包之外,还需要导入AOP相关的jar包

创建spring核心配置文件,导入AOP约束

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

3 : 使用表达式配置切入点

Aspectj里面使用表达式完成切入点配置
1 切入点:实际增强的方法
2 常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(0)execution(public void cn.itcast.UserDao.update(..))
* 如果访问修饰符不写* ,比如写public,返回类型必须要写出来
(1)execution(* cn.itcast.UserDao.update(..))
* 增强UserDao类里面update方法
(2)execution(* cn.itcast.UserDao.*(..))
* 增强UserDao类里面所有方法
(3)execution(* *.*(..))
* 所有增强
(4)execution(* save*(..))
- 对方法名称,如果方法名是以save开头的进行增强
(5)execution(* cn.itcast.dao..*(..))  
- 两个点表示当前包和子包,孙包,类方法进行增强
(6)execution(* cn.itcast.dao.GenericDAO+.*(..))
- + ;针对接口情况,接口里面所有实现类
3 通知类型
前置通知:在方法之前执行
后置通知:在方法之后执行
环绕通知:在方法之前和之后执行
异常通知:被增强的方法运行过程中,出现异常,执行这个通知

最终通知:类似于finally,总会被执行

4 : AOP操作

第一步 : 创建被增强类,创建被增强方法

public class User {	public void add() {System.out.println("我是User类的add()方法");}public void show() {System.out.println(1/0);}
}
package com.edu.xiaoniu.bean;import org.aspectj.lang.ProceedingJoinPoint;public class MyUser {/*** 前置增强方法*/public void before() {System.out.println("我是前置增强");}/*** 后置增强方法*/public void after() {System.out.println("我是后置增强");}/*** 环绕增强方法* @throws Throwable */public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("我是环绕增强方法---之前");//做环绕增强的方法里面,获取增强方法proceedingJoinPoint.proceed();System.out.println("我是环绕增强方法---之后");}/*** 异常增强* */public void error() {System.out.println("我是异常增强");}/*** 最终增强*/public void finallyAdd() {System.out.println("我是最终增强");}}
	<!-- AOP配置 --><aop:config><!-- 配置切入点 --><aop:pointcut expression="execution(* com.edu.xiaoniu.bean.User.*(..))" id="p1"/><!-- 配置切面 --><aop:aspect ref="myUser"><!-- 前置增强 --><!-- <aop:before method="before" pointcut-ref="p1"/> --><!-- 后置增强 --><!-- <aop:after-returning method="after" pointcut-ref="p1"/> --><!-- 环绕增强 --><!-- <aop:around method="around" pointcut-ref="p1"/> --><!-- 异常增强 --><!-- <aop:after-throwing method="error" pointcut-ref="p1"/> --><!-- 最终增强 --><aop:after method="finallyAdd" pointcut-ref="p1"/></aop:aspect></aop:config>
</beans>

第二步 : 配置增强

创建User和MyUser类对象

    <bean id="user" class="com.edu.xiaoniu.bean.User"></bean><bean id="myUser" class="com.edu.xiaoniu.bean.MyUser"></bean>

配置切入点和切面

	<!-- AOP配置 --><aop:config><!-- 配置切入点 --><aop:pointcut expression="execution(* com.edu.xiaoniu.bean.User.*(..))" id="p1"/><!-- 配置切面 --><aop:aspect ref="myUser"><!-- 前置增强 --><!-- <aop:before method="before" pointcut-ref="p1"/> --><!-- 后置增强 --><!-- <aop:after-returning method="after" pointcut-ref="p1"/> --><!-- 环绕增强 --><!-- <aop:around method="around" pointcut-ref="p1"/> --><!-- 异常增强 --><!-- <aop:after-throwing method="error" pointcut-ref="p1"/> --><!-- 最终增强 --><aop:after method="finallyAdd" pointcut-ref="p1"/></aop:aspect></aop:config>
</beans>

第三步 : 测试

	@Testpublic void testUser() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");User user = context.getBean("user",User.class);user.add();user.show();}

这篇关于彷徨 | spring框架学习笔记二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过