Spring boot学习记录

ApplicationContext的中文意思是“应用前后关系”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持。
ApplicationContext由BeanFactory派生而来;BeanFactory的许多功能需要编程实现,而在ApplicationContext中则可以通过配置的方式实现;

创建Spring的工厂类

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

通过工厂解析XML获取Bean实例

User user = (User) applicationContext.getBean("UserSingleton");

hashCode()方法给对象返回一个hash code值。
对象相等则hashCode一定相等, hashCode相等对象未必相等。

spring boot启动方式

 public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

@Scope注解是 Spring IOC 容器中的一个作用域,在 Spring IOC 容器中,他用来配置Bean实例的作用域对象。
singleton 单实例的: 容器启动实例就创建好了,容器销毁的时候,也会调用Bean的销毁方法
prototype 多实例的: IOC容器启动的时候,并不会创建对象,而是在第一次使用的时候才会创建
reqeust 同一次请求: 每一次HTTP请求都会产生一个新的bean
session 同一个会话级别:

针对每一个Bean实例,都会有一个initMethod() 和 destroyMethod() 方法,我们可以在Bean 类中自行定义,也可以使用 Spring 默认提供的这两个方法。

配置文件
1 配置文件类型
properties
yml/yaml
2 yaml 以数据为核心 基本语法

数值前必须有空格,作为分隔符

数据格式

对象
数组,使用"-"表示数组每个元素
纯量

参数引用

${key}

读取配置内容

---第一种方式---
@Value("${    }")
注意: 使用@Value的类在调用的时候,不能直接通过new操作符进行调用,需要使用@Autowired进行注入

---第二种方式---
@Autowired
private Environment env;
env.getProperty(" ");

---第三种方式---
@Component
@ConfigurationProperties(prefix = " ")

profile用来完成不同环境下,配置动态切换功能的。
profile配置方式
多profile文件方式:提供多个配置文件,每个代表一种环境
yml多文档方式 在yml中使用 —分隔不同配置

profile激活方式

配置文件: 配置文件中配置:spring.profiles.active=dev
虚拟机参数: 在VM options指定 -Dspring.profiles.active=dev
命令行参数: java -jar xxx.jar --spring.profiles.active=dev

内部配置加载顺序

spring boot程序启动时,会从以下位置加载配置文件,高优先级配置的属性会生效:
    1. file:./config/: 当前项目下的/config目录下
    2. file:/:     当前项目的根目录
    3. classpath:/config/:    classpath的/config目录
    4. classpath:/ :    classpath的根目录

Spring boot 整合其他框架
1.springBoot整合Junit

①搭建SpingBoot工程
②引入starter-test起步依赖
③编写测试类
④添加测试相关注解
@RunWith(SpringRunner.class)
@SpringBooTest(classes = SpringbootTestApplication.class)

示例:

@RunWith(SpringRunner.class)
@SpringBooTest(classes = SpringbootTestApplication.class)
public class UserServiceTest{

    @Autowired
    private UserService userService
    
    @Test
    public void testAdd(){
        userService.add();
    }
}

2.Spring整合Redis

①搭建SpringBoot工程
②引入redis起步依赖
③配置redis相关属性
④注入RedisTemplate模板
   @Autowired
    private RedisTemplate redisTemplate;
⑤编写测试方法,测试
    存值: redisTemplate.boundValueOps("name").set("zhangsan");
    取值: redisTemplate.boundValueOps("name").get()

3.SpringBoot整合MyBatis

① 搭建SpringBoot工程
② 引入mybatis起步依赖,添加mysql驱动
③ 编写DataSource和MyBatis相关配置
Mybatis:
   mapper-location: classpath:mapper/*Mapper.xml
   type-aliases-package: 
④ 编写dao和mapper文件/纯注解开发
⑤ 测试
  1. 将文件以流的形式一次性读取到内存,通过响应输出流输出到前端
    /**
  • @param path 想要下载的文件的路径
  • @param response
  • @功能描述 下载文件:
    */
    @RequestMapping(“/download”)
    public void download(String path, HttpServletResponse response) {
    try {
    // path是指想要下载的文件的路径
    File file = new File(path);
    log.info(file.getPath());
    // 获取文件名
    String filename = file.getName();
    // 获取文件后缀名
    String ext = filename.substring(filename.lastIndexOf(“.”) + 1).toLowerCase();
    log.info(“文件后缀名:” + ext);

// 将文件写入输入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();

// 清空response
response.reset();
// 设置response的Header
response.setCharacterEncoding(“UTF-8”);
//Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 “Content-Disposition: inline; filename=文件名.mp3”
// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
response.addHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(filename, “UTF-8”));
// 告知浏览器文件的大小
response.addHeader(“Content-Length”, “” + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType(“application/octet-stream”);
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}

问题及解决方案
1. 普通类 读取不到mapper 报错空指针错误
解决方案:
① @Component 注册该普通Java类
② @Resource 注册 Mapper 层接口

@Resource
private XXXMapper xxxMapper;

③ 声明该类自身的静态类变量

private static EmailTimerTask emailTimerTask;

④ 用@PostConstruct 注入 bean,声明 init() 方法,进行初始化挂载;

@PostConstruct
    private void init(){
        emailTimerTask = this;
        emailTimerTask.xxxMapper = this.xxxMapper;
    }

⑤ 使用声明的该类的静态类变量,调用 Mapper 层

List<xxx> xxxList = emailTimerTask.xxxMapper.selectAll();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值