狂神spring5

1、介绍

轻量级 控制反转 面向切面编程的框架
支持事务处理 对框架整合的支持。

2、扩展

springboot
快速开发脚手架
快速开发单个微服务
需要完全掌握springmvc和spring

## 3、ioc理论 19

导包 直接导入一个
传统做法
dao
UserDaoImpl getUser
UserService
UserServiceImpl 每次都需要修改这里的数据
组合
private UserDao userDao=new UserDaoImpl();
public void getUser(){
userDao.getUser();
}
//用户实际调用业务层,dao层无需接触

新增一个mysql获取
必须改
private UserDao userDao=new UserMysqlImpl();

每次新增需求,需要修改实现

不太能理解三层架构
只知道大概
//利用set进行动态实现值的注入

用户的需求可能会影响原来代码,需要根据用户需求修改源代码,如果代码量大,修改一次的代价昂贵。
set注入,使用一个set接口实现。
使用set注入,程序变成被动对象。
控制反转
从本质上解决问题,不用再去管理对象创建,耦合性大大降低,可以更加专注业务实现。
程序架构不需要改变

写一遍案例 一定要写
最开始是这么用的

public class UserServiceImpl implements UserService
{
	private UserDao userDao=new UserDaoMysqlImpl();

	@Override
	public void getUser() {
		userDao.getUser();
	}
}
public class UserServiceImpl implements UserService
{
	private UserDao userDao;

	//利用set进行动态实现值的注入  接口的思想
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void getUser() {
		userDao.getUser();
	}
}
public class MyTest {
	public static void main(String[] args) {
		//用户实际调用的是业务层,dao层不需要接触
		UserService userService = new UserServiceImpl();

		((UserServiceImpl)userService).setUserDao(new UserDaoOracleImpl());
		userService.getUser();
	}
}

控制权在用户手上 降低耦合 程序员更专注业务

4、ioc本质

用户访问业务层
主动权在用户,选择调用什么
一开始,主动权在程序员,程序调用什么
di是实现ioc的一种方式
获得依赖对象的方式反转了
ioc容器 解耦
两层之间设置了一个接口
xml实现,bean的定义信息和实现是分离的,采用注解可以将两者结合。

public class MyTest {
	public static void main(String[] args) {
		//获取spring上下文对象
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		//对象都在spring中管理
		Hello hello = (Hello) context.getBean("hello");
		System.out.println(hello.toString());
	}
}

新方法
可以通过修改配置文件

public class MyTest {
	public static void main(String[] args) {
		//获取applicationcontext
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

		//容器get获取对象
		UserServiceImpl userServiceImpl =(UserServiceImpl) context.getBean("UserServiceImpl");

		userServiceImpl.getUser();

	}
}

配置文件

 <bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl"/>
    <bean id="UserServiceImpl" class="com.kuang.service.UserServiceImpl">
        <!--ref引用spring中创建好的对象 value具体的值
        -->
        <property name="userDao" ref="oracleImpl"/>

5、helloSpring 24

ApplicationContext 构造函数的一个或多个位置路径是资源字符串,可以从各种外部资源加载配置元数据。
对象都在spring管理,直接去取出来用。
Hello hello=(Hello)context.getBean(“hello”);
1、hello谁创建的
Bean相当于对象 new Hello();
Hello hello=new Hello();
id 变量名
class new的对象
property 给对象属性设置一个值
对象由spring创建
核心是set
最后直接去拿

有叶子 被托管了
如何从applicationcontext到

非常多的继承层次
在这里已经学过了mybatis
对象由spring创建管理装配。
//获取applivationcontext

//拿到spring的容器
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");

//需要什么就get什么
UserServiceImpl userServiceImpl=(UserServiceImpl)context.getBean("UserServiceImpl")

通过配置文件修改

<property name="userDao" ref="oracleImpl"/>

在这里插入图片描述

6、ioc创建对象方式 19

11/19上午 公司没几个人
今晚回家跑步 十公里到十二
明天早上正常时间来公司学习Spring和mybatis
明天学完了就去游泳
我的表后天应该可以到吧

什么时候new的对象

终于也看到了自己的叶子

三种有参构造方式

<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--        &lt;!&ndash;1、下标赋值&ndash;&gt;-->
<!--        <constructor-arg index="0" value="shenjunhan"/>-->
<!--    </bean>-->

<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--        <constructor-arg type="java.lang.String" value="42"/>-->
<!--    </bean>-->

    <bean id="user" class="com.kuang.pojo.User">
        <constructor-arg name="name" value="dd"/>
    </bean>

在配置文件加载时,容器中管理的对象就已经初始化了。

7、Spring配置说明

5.1别名

<alias name="user" alias="userNew"/>

5.2 bean配置

5.3 import 协同开发

<import resource="beans2.xml"/>
    <import resource="beans.xml"/>

8、di依赖注入

多种注入方式
1、构造器注入

2、set注入
依赖:bean对象创建依赖于容器
注入:bean对象中的所有属性由容器来注入

1.复杂类型:

<bean id="student" class="com.kuang.pojo.Student">
        <!--普通注入-->
        <property name="name" value="沈"/>
    </bean>

9、依赖注入之set注入

一些注入

<property name="card">
            <map>
                <entry key="身份证" value="23444444422"/>
                <entry key="银行卡" value="234222222"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>LOL</value>
                <value>Bob</value>
                <value>KoL</value>
            </set>
        </property>

        <property name="wife">
            <null></null>
        </property>
        
        <property name="info">
            <props>
                <prop key="学号">20190525</prop>
                <prop key="性别">男性</prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>

10、c命名和p命名空间注入 12

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,直接注入属性的值-->
    <bean id="user" class="com.kuang.pojo.User" p:name="shenjunhan" p:age="22"/>

    <!--c命名空间注入,通过构造器注入-->
    <bean id="user2" class="com.kuang.pojo.User" c:age="12" c:name="shenjj"/>
</beans>

测试:

@Test
	public void test2(){
		ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
		User user=context.getBean("user",User.class);
		System.out.println(user);
	}

11、bean作用域 8

beanScope 作用域
创建bean时也可以创建作用域

6.4
单例模式:创建的所有实例都只有一个

<bean id="user2" class="com.kuang.pojo.User" c:age="12" c:name="shenjj" scope="singleton"/>

测试

User user=context.getBean("user2",User.class);
		User user1=context.getBean("user2",User.class);

		System.out.println(user==user1);

1、单例模式下 用的是同一个
2、原型(protoype下 就用的不一样):每次从容器中get的时候,都会产生一个新对象
浪费资源
3、request 请求没了就失效
4、session 会话
5、application

12、自动装配Bean 15

spring在上下文中自动寻找并自动给bean装配属性

隐式自动装配bean(重要)
7.1 测试
会自动在容器上下文类查找和自己对象set方法后面的值对应的beanid

<bean id="people" class="com.kuang.pojo.People" autowire="byName">

bytype 找到和对象属性类型相同的bean,需要保证属性类型一样;
使用注解实现自动装配

13、注解实现自动装配 29

spring2.5支持
使用注解需要:1、导入约束 context约束
2、配置注解支持

 <!--开启注解支持-->
    <context:annotation-config/>

直接在属性上使用
@Nullable 说明字段可以为null

public class People {
	//如果显式定义了false,说明对象可以为null,否则不允许为空。
	@Autowired(required = false)
	private Cat cat;
	@Autowired
	private Dog dog;

如果自动装配环境比较复杂,可以使用@Qulifier(value=“xxx”)去配置,指定一个唯一的bean对象使用。

还有一个java自带的@Resource

14、spring注解开发 20

8、使用注解开发

@Value("矿山")
	public String name;

也可以注入在set方法上
一些简单的配置 使用注解可以快很多

3、注解衍生
这几个注解功能相同,都是代表将某个类注册到Spring中,装配bean

dao

@Repository
public class UserDao {
}

注册bean 使用component
注入值 value
作用域 scope
xml用来管理bean
注解只负责完成属性的注入

15、javaConfig实现配置

Configuration 本质上也是Component
这一段不好听


@Configuration
@ComponentScan("com.kuang.pojo")
@Import(KuangConfig2.class)

public class KuangConfig {

	//注册一个bean,相当于之前写的一个bean ,方法名字相当于bean标签的id属性
	//返回值相当于bean的class属性

	@Bean
	public User getUser(){
		return new User();
	}
}

这段随便听听
纯java配置方式 在springboot随处可见

这一课很重要 很基本
springboot一开始就可以用

注解学习 平时多看源码

16、内容回顾 5

Bean的自动装配 spring会在上下文自动寻找,并自动给bean装配属性。
mybatis建议使用xml 适合处理复杂东西

作用域不用怎么了解

17、静态代理模式 22

是springaop的底层
静态代理
动态代理
专心做一件事
10.1 静态代理
角色分析
抽象角色:抽象类或者接口
真实角色:被代理的角色
代理角色 :代理真实角色 代理后一般会做附属操作
客户:访问代理对象的人

public class Proxy implements Rent {
	private Host host;

	//丢哪个房东,就能代理哪个
	public Proxy(Host host){
		this.host=host;
	}

	public Proxy() {
	}

	@Override
	public void rent() {
		host.rent();//帮房东租房子
		seeHouse();;
		fare();
		hetong();
	}

	//看房
	public void seeHouse(){
		System.out.println("中介带你看房");
	}

	//收中介费
	public void fare(){
		System.out.println("收中介费");
	}

	//签订合同
	public void hetong(){
		System.out.println("签合同");
	}
}

1121下午在公司 大家都来加班 厉害 好好学习

18、静态代理再理解 12

单一职能原则 解耦
对当前项目,添加一个代理,实现新增日志方法。
使用set方法注入方式
改动原有代码,大忌。

纵向开发 aop
横向开发 log
不改变原有代码

19、动态代理详解 32

配置文件 核心的东西
不想增加类

动态代理和静态代理角色一样
动态代理类是动态生成的,不是直接写好的。


基于接口 jdk动态代理
Proxy,InvocationHandler:调用处理程序

没有学明白反射,这里很迷惑。

20、AOP实现方式 23

代理对象
允许用户自定义切面

横切关注点:跨越应用程序多个模块的方法或功能。
日志,安全,缓存,事务等等
切面 一个类
通知 类中的一个方法
目标

反射–动态代理–spring Aop

用spring方式实现aop

使用spring的接口


    <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
    <bean id="log" class="com.kuang.log.Log"/>
    <bean id="afterLog" class="com.kuang.log.AfterLog"/>

    <!--使用原生springapi接口-->
<!--    配置aop-->
    <aop:config>
        <!--execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

测试代码

public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		//动态代理代理的是接口
		UserService userService = (UserService)context.getBean("userService", UserService.class);
		userService.select();
	}

21、AOP实现方式二 9

<!--方式二:自定义类-->
    <bean id="diy" class="com.kuang.diy.DiyPointCut"/>

    <aop:config>
        <aop:aspect ref="diy">
            <!--限制包或者类-->
            <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>

        </aop:aspect>
    </aop:config>

重点是要多使用
1、springapi接口
2、自定义实现aop

22、注解实现AOP 14

最后一课
使用注解实现

反射
mybatis
反射
mybatisplus
spring5 完成 剩下和mybatis整合部分
springmvc
springboot
vue
linux 学过 简单操作
docker 学过

springcloud

@Aspect  //标志这个类是一个切面
public class AnnotationPointCut {

	@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
	public void before(){
		System.out.println("方法执行前");
	}

	@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
	public void after(){
		System.out.println("====方法执行后=====");
	}

	@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
	public void around(ProceedingJoinPoint jp) throws Throwable {
		System.out.println("====环绕前=====");

		Signature signature = jp.getSignature();
		System.out.println("signature:"+signature);

		//执行方法
		Object proceed = jp.proceed();

		System.out.println("环绕后");

		System.out.println(proceed);
	}

}

23、回顾mybatis 18

mybatis-spring

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>

一些配置的东西

mybatis我还没学过,所以需要抽时间学习。

1、编写实体类
2、编写核心配置文件
3、编写接口
4、编写mapper.xml
5、测试

24、整合mybatis方式1

使用sqlsessionfactory作为构造方法参数创建sqlsessiontemplate对象。

25、整合mybatis方式二

先跳过

26、事务回顾 16

要么都成功 要么都失败
acid 原子性 一致性
隔离性:多个业务可能操作同一个资源,防止数据损坏
持久性:事务一旦提交,无论系统发生什么,都不影响结果。

27、spring声明式事务 15

挺疑惑的,先学着吧,先去学一下mybatis 不然这里学不动。

多用点时间,学完了mybatis之后,回来看一下整合mybatis部分。

你关于spring5的理解到了什么程度
赚钱 需要适应社会需求 而不是做你想做的。

28、总结回顾 10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值