Spring框架讲解

1.1 单例和多例

1.1.1 关于单例和多例说明

  1. 单例: 在内存中只有一份

  2. 多例: 在内存中可能有多份

    package com.jt.demo;
    
    public class User {
        public User(){
            System.out.println("无参构造");
        }
        public void say(){
            System.out.println("测试对象是单例还是多例");
        }
    }
    
    package com.jt.config;
    
    import com.jt.demo.User;
    import org.springframework.context.annotation.*;
    
    @Configuration
    @ComponentScan("com.jt")
    public class SpringConfig {
        //@Scope("singleton")
        @Scope("prototype")
        @Bean
        public User run() {
            return new User();
        }
    }
    
    @Test
        public void testDemo1() {
            //容器启动对象创建
            ApplicationContext context =
                    new AnnotationConfigApplicationContext(SpringConfig.class);
            //从容器中获取对象(要用)
            User user1 = context.getBean(User.class);
            User user2 = context.getBean(User.class);
            user1.say();//测试对象是单例还是多例
            System.out.println(user1==user2);//false
        }

    1.2 懒加载机制

    1.2.1 懒加载说明

    说明: 如果Spring容器创建,对象立即创建则该加载方式为 “立即加载”, 容器启动创建 如果Spring容器创建,对象在被使用的时候才创建, 则称为"懒加载" 用时才创建

    注解: @Lazy 添加表示改为懒加载 测试说明: 主要测试对象中的无参构造什么时候执行!!!

1.2.2 懒加载用法  

package com.jt.demo;

public class User {
    public User(){
        System.out.println("无参构造");
    }
    public void say(){
        System.out.println("测试对象是单例还是多例");
    }
}
package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.jt")
public class SpringConfig {
//    @Scope("singleton")
//    @Scope("prototype")
    @Lazy  //只对单例有效,多例模式默认就是懒加载
    @Bean
    public User run() {
        return new User();
    }
}
 @Test
    public void testDemo1() {
        //容器启动对象创建
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        //从容器中获取对象(要用)
        User user1 = context.getBean(User.class);
        User user2 = context.getBean(User.class);
        user1.say();//测试对象是单例还是多例
        System.out.println(user1==user2);//false
    }

1.2.3 多例与懒加载的关系

说明: 只要对象是多例模式,则都是懒加载!, 在单例模式中控制懒加载才有效. 规则说明: lazy true lazy false 单例模式: 有效 懒加载 有效 立即加载 多例模式: 无效 懒加载 无效 懒加载

1.2.4 关于lazy 使用场景的说明

场景1: 服务器启动时,如果加载太多的资源,则必然导致服务器启动慢, 适当的将不重要的资源设置为懒加载. 场景2: 有时用户会需要一些特殊的"链接",而这些链接的创建需要很长的时间.可以使用懒加载.

1.3 Spring对象生命周期管理

1.3.1 关于对象生命周期说明

说明: 一个对象从创建到消亡,可以划分为四个阶段. 如果需要对程序进行干预.则可以通过周期方法进行干预. (回调函数/钩子函数/接口回调) 图解说明生命周期函数的作用: 主要作用可以在各个时期对对象进行干预(无参构造,init:加注解@PostConstruct,普通方法,销毁:@PreDestory) 

1.3.2 生命周期函方法的使用

package com.jt.demo;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component()
public class Person {
    public Person(){
        System.out.println("无参构造");
    }
    @PostConstruct //对象创建后立即调用
    public void init(){
        System.out.println("张三出生,资质拉满");
    }
    public void doWork(){
        System.out.println("迎娶美人鱼");
    }
    @PreDestroy //对象消亡时调用
    public void destory(){
        System.out.println("销毁:全世界哀悼");
    }
}
package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.jt")
public class SpringConfig {
//    @Scope("singleton")
//    @Scope("prototype")
    @Lazy  //只对单例有效,多例模式默认就是懒加载
    @Bean
    public User run() {
        return new User();
    }
}
 @Test
    public void testDemo2() {
        //容器启动对象创建
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        //从容器中获取对象(要用)
        Person person = context.getBean(Person.class);
        person.doWork();
        context.close(); //将容器关闭
    }

1.3.3 使用注解

  1. @PostConstruct //在对象创建之后立即调用

  2. @PreDestroy //对象消亡时 进行调用

1.2 依赖注入(Dependency Injection,简称DI)

1.2.1 @Autowired注解

说明: 在对象中如果需要使用属性注入.一般使用@Autowired注解. 功能: 可以将Spring容器中的对象,自动注入到属性中. 注入方式: \1. 默认按照类型注入. 如果注入的属性是接口,则自动注入实现类 \2. 按照名称注入(key). 一般不用

重要前提: 如果需要依赖注入.则对象必须交给Spring容器管理.

package com.jt.pojo;

public interface Pet {
    void hello();
}
package com.jt.pojo;

import org.springframework.stereotype.Component;

@Component
public class Cat implements Pet {

    @Override
    public void hello() {
        System.out.println("我是喵喵喵");
    }
}
package com.jt.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component//将User交给Spring容器管理
public class User {
    @Autowired
    private Pet pet;
    public void say(){
        //调用宠物方法
        pet.hello();
    }
}
package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//标识我是配置类
@ComponentScan("com.jt")//必须添加包路径
public class SpringConfig {

}
@Test
    public void testDemo1(){
        ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.say();//我是喵喵喵
    }

1.3 接口多实现的情况说明

1.3.1 代码说明

1.一个接口 2个实现类 结构如下

 狗类:

 猫类:

1.3.2 报错说明

说明: 一个接口应该只有一个实现类,否则spring程序无法选择.

1.3.3 解决方案

1.3.4 @Resource注解说明(尽量不要用)

@Resource(name="cat")    @Autowired+ @Qualifier("dog")的组合体

1.4 MVC 设计思想

1.4.1 MVC思想说明

经典MVC模式中,M是指业务模型,V是指用户界面,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。其中,View的定义比较清晰,就是用户界面。

M: model 业务模型 V: view 用户界面 C: control 控制层

历史说明: JSP动态页面 html代码 + java代码 写到一起 xxx.jsp 不方便后期维护. 页面和业务执行紧紧的绑定在一起耦合性高.

小结: \1. MVC是一种设计思想,编码中降低代码的耦合性. \2. 前端专注于开发页面 view \3. 后端专注于开发后端 model

历史说明: JSP动态页面 html代码 + java代码 写到一起 xxx.jsp 不方便后期维护. 页面和业务执行紧紧的绑定在一起耦合性高.

小结: \1. MVC是一种设计思想,编码中降低代码的耦合性. \2. 前端专注于开发页面 view \3. 后端专注于开发后端 model \4. 2者通过control 进行控制

1.4.2 层级代码结构

说明: MVC设计思想,实现了前端和后端的松耦合.但是根据实际的开发情况,很多的业务逻辑比较复杂.如果后端将所有的代码都写到同一个java类中.这样的代码结构很臃肿. 为了很好的实现MVC设计思想.所以后端代码也应该分层.

分层说明: \1. 控制层 Controller 与前端页面交互的. @Controller \2. 业务层 Service 编辑业务逻辑. @Service \3. 持久层 Mapper 实现数据库的相关操作 暂时:@Repository MVC > 三层代码结构!!!

1.5 三层代码结构

 

 mapper层

 service层

 

 controlle层

 

 测试类

1.6 请谈一下你对IOC/DI的看法(开放)

历史: 传统代码其中的属性对象一般都是通过new关键字手动创建,这样的代码耦合性高,不方便扩展 功能: \1. IOC: 由Spring容器管理对象的生命周期. \2. 使得对象与对象之间的耦合性降低. \3. DI是依赖注入. 只有被spring容器管理的对象才可以被依赖注入. 默认的条件下采用类型注入.如果有特殊需求也可以采用名称注入(@Qualifier(“cat”)) \4. Spring中 IOC和DI相互配合,可以极大程度上降低耦合性.

意识: Spring由于采用了IOC/DI的设计方式,可以整合其它的第三方框架.使得程序的调用"浑然一体"

1.7 @Value注解说明

1.7.1 注解赋值

说明: @Value注解 可以直接为基本类型赋值和String类型 问题: 如果像图中赋值,则耦合性依然很高,不通用. 需要优化!!!

1.7.2 编辑user.properties

说明: 对象中的属性一般都是业务数据,如果需要为业务数据赋值,则一般采用properties文件 更加灵活. 位置: 在resources目录下

 

字符集编码说明:

 

配置文件内容:

#1.注意事项:  key=value  等号连接  2.中间不要添加多余的空格
#2.说明: windows系统中有环境变量username=系统用户名称,以后写业务数据时,最好绕开关键字username
#3.编码规则: 程序默认读取properties文件时,采用ISO-8859-1编码
user.username1="葫芦娃"

1.7.3 @value为属性赋值

package com.jt.mapper;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

@Repository
@PropertySource(value = "classpath:/user.properties",encoding = "utf-8")
//classpath:/  代表resources的根目录
public class UserMapperImpl implements UserMapper{
    //耦合性!!!!
    // 表达式: 固定写法  springel表达式 取值方式 缩写spel表达式
    // 规则:通过key动态获取spring容器中的value
   @Value("${user.username1}")
    private  String username1;
    @Override
    public void adduser() {
        System.out.println("增加用户:"+username1);
    }
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睡不醒的小小秦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值