栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring5学习笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring5学习笔记

目录

一、控制反转loC

二、依赖注入DI

2.1 构造器注入 

2.2 Set注入

2.3 扩展方式注入

三、Bean的作用域 Scope

 3.1 单例模式 singleton

 3.2 原型模式 prototype

3.3 其他

 四、Bean的自动装配

4.1 byName自动装配

4.2 byType自动装配

4.3 注解自动装配

@Autowired【常用】

@Resource

五、使用注解开发 

六、使用Java的方式配置Spring

七、代理模式

7.1 静态代理 

7.2 动态代理

八、AOP 面向切面编程

8.1 Spring实现AOP

方式一: 使用原生SpringAPI接口

方式二: 自定义切面类实现AOP

方式三:注解实现AOP

九、整合Mybatis

9.1 回顾Mybatis

9.2 Mybatis-Spring

方式一:使用sqlSessionTemplate

方式二:使用SqlSessionDaoSupport

十、声明式事务 

依赖


Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!

一、控制反转loC

控制反转loC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现loC的一种方法。

  • 之前,程序是主动创建对象!控制权在程序猿手上!
  • 使用了set注入后,程序不再具有主动性,而是变成了被动的接受对象!

这种思想,从本质上解决了问题,我们程序猿不用再去管理对象的创建了。系统的耦合性大大降低,可以更加专主的在业务的实现上!这是Ioc的原型!

ps: Spring会把所有的bean(对象)创建,即使没有被使用

二、依赖注入DI

bean对象的创建依赖于Spring容器,bean对象的属性由容器注入。

2.1 构造器注入 

(有参构造器)

2.2 Set注入

(set+无参构造器)

2.3 扩展方式注入

1.c命名空间(有参构造器) 

xmlns:p="http://www.springframework.org/schema/p"

2.p命名空间(set+无参构造器) 

xmlns:c="http://www.springframework.org/schema/c"

ps: IDEA会报命名空间错误,在设置中忽略即可

三、Bean的作用域 Scope

 3.1 单例模式 singleton

  默认,同一个对象实例user==user2

 3.2 原型模式 prototype

  user!=user2

3.3 其他

request、session、application、websocket在web开发中使用。

 四、Bean的自动装配

4.1 byName自动装配





    
    
    

4.2 byType自动装配





    
    
    

ps: 

byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性名一致

byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性类型一致

4.3 注解自动装配

1.导入约束

xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd"

2.开启注解自动装配

@Autowired【常用】

直接在类属性上使用(不需set方法),也可以在set方法上使用 ,按类型(byType)自动匹配,有多个同类的再按名字(byName)匹配,都匹配不上可以用@Qualifier(value = "cat1")绑定bean id

@Autowired
@Qualifier(value = "cat1")
private Cat cat;

@Resource

 byType和byName都匹配不上可以用name属性绑定bean id

@Resource(name = "cat1")

五、使用注解开发 

在beans.xml中,指定要扫描的包,开启注解


@Component

@Scope("singleton")  //作用域
@Component  //等价于
public class User {
    @Value("lzq")  //等价于
    private String name;
}

六、使用Java的方式配置Spring

  Config.java代替beans.xml。SpringBoot常用

// Config.java

@Configuration  //代表这是一个配置类,就像之前的beans.xml
@ComponentScan("com.lzq.pojo")
@import(Config2.class)
public class Config {

    @Bean
    public User2 getUser2(){
        return new User2();
    }
}
@Test
public void getUser2(){
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    User2 user = context.getBean("getUser2", User2.class);
    System.out.println(user.getName());
}

七、代理模式

7.1 静态代理 

1.抽象角色接口:租房

public interface Rent {
    void reat();
}

2. 被代理角色:房东 ,要出租房子

public class Host implements Rent {
    @Override
    public void reat(){
        System.out.println("房东要出租房子");
    }
}

3.代理角色:中介, 要出租房子,看房,签合同等

public class Proxy implements Rent{
    private Host host;

    public Proxy(Host host) {
        this.host = host;
    }
    @Override
    public void reat() {
        seeHouse();
        host.reat();
        rent2();
    }
    public void seeHouse(){
        System.out.println("中介带客户看房");
    }
    public void rent2(){
        System.out.println("签合同");
    }
}

4.客户:仅通过代理角色租房子

public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        Proxy proxy = new Proxy(host);
        proxy.reat();
    }
}

因此,代理角色除了租房外,还有看房、签合同等附属操作,横向切面加进去的新业务(AOP) 

7.2 动态代理

了解两个类:interface InvocationHandler 和 class Proxy

//自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的接口
    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    //生成代理类
    public Object getProxy(){
       return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                object.getClass().getInterfaces(),this);
    }

    //处理代理实例,并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(method.getName());
        Object result = method.invoke(object, args);
        return result;
    }

    //横向插入的新业务!!!
    public void log(String msg){
        System.out.println("调用了"+msg+"方法");
    }
}
public class Client {
    public static void main(String[] args) {
        //被代理类
        Host host = new Host();
        //调用处理程序
        ProxyInvocationHandler handler = new ProxyInvocationHandler();
        handler.setObject(host);
        //动态生成代理类
        Rent proxy = (Rent) handler.getProxy();
        proxy.reat();
    }
}

         

八、AOP 面向切面编程

8.1 Spring实现AOP

导入依赖

        
            org.aspectj
            aspectjweaver
            1.9.7
        
  • 方式一: 使用原生SpringAPI接口

增加前置后置日志:

public class Log implements MethodBeforeAdvice, AfterReturningAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+",返回结果为"+returnValue);
    }
}

配置AOP


    
    
    
    
        
        
        
        
    

测试

@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口 要用UserService接口不能用UserServiceImpl实现类!!!
        UserService userService = context.getBean("userService", UserService.class);
        userService.query();
    }
  • 方式二: 自定义切面类实现AOP

自定义切面类

public class DiyPointCut {
    public void logBefore(){
        System.out.println("执行前");
    }
    public void logAfter(){
        System.out.println("执行后");
    }
}

 配置AOP

    
    
        
        
            
            
            
            
            
        
    

测试同上  

  • 方式三:注解实现AOP

切面类

@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.lzq.UserServiceImpl.*(..))")
    public void logBefore(){
        System.out.println("执行前");
    }
    @After("execution(* com.lzq.UserServiceImpl.*(..))")
    public void logAfter(){
        System.out.println("执行后");
    }
}

配置AOP

    
    
    
    

测试同上 

九、整合Mybatis

9.1 回顾Mybatis

1.编写实体类

2.编写核心配置文件mybatis-config.xml

3.编写接口

4.编写Mapper.xml

5.测试

9.2 Mybatis-Spring
  • 方式一:使用sqlSessionTemplate

1.编写数据源配置

    
        
        
        
        
    

2. sqlSessionFactory

    
        
        
        
        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680494.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号