0%

Spring容器和注解

官方文档https://springdoc.cn/spring/core.htm

Bean和容器

在spring中,构成程序的骨干和被容器管理的对象称为Bean。

org.springframework.context.ApplicationContext 接口代表Spring IoC容器,负责实例化、配置和组装bean。

容器获取Bean的方式有

  • 通过类型获取AlphaDao alphaDao = applicationContext.getBean(AlphaDao.class);
  • 通过名字获取AlphaDao alphaDao = (AlphaDao) applicationContext.getBean("alphaHibernate");

可以通过XML和注解将Bean实例初始化到容器中。

@Primary 注解

@Primary 表示,当多个Bean是自动注入到一个单值(single value)依赖的候选者时,应该优先考虑一个特定的Bean。如果在候选者中正好有一个主要(primary)Bean存在,它就会成为自动注入的值。

和生命周期有关的注解

@PostConstruct 在构造函数调用后执行

@PreDestroy 在销毁前执行

@Scope注解

  • 能够管理Bean的作用域
  • 默认情况下是单例 @Scope("singleton")
  • 可以变为任何数量的实例 @Scope("prototype")

示例:

1
2
3
4
5
6
7
8
@Test
public void testBeanManage() {
AlphaService alphaService = applicationContext.getBean(AlphaService.class);
System.out.println(alphaService);

alphaService = applicationContext.getBean(AlphaService.class);
System.out.println(alphaService);
}

@Configuration注解

@Configuration标记的类中的@Bean的方法会被Spring容器统一管理

@Autowired注解

手动获取Bean的方式,用ApplicationContext

使用@Autowired可以自动注入bean

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Autowired
private AlphaDao alphaDao;

@Autowired
private AlphaService alphaService;

@Autowired
private SimpleDateFormat simpleDateFormat;

@Test
public void testDI() {
System.out.println(alphaDao);
System.out.println(alphaService);
System.out.println(simpleDateFormat);
}

@Qualifier注解

缩小注入Bean的类型匹配的范围

@RequestMapping注解

用于映射HTTP请求方式

  1. Get请求获取参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // 只有GET方法能请求到
    // /students?current=1&limit=20
    @RequestMapping(value = "/students", method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
    @RequestParam(name = "current", required = false, defaultValue = "1") int current,
    @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
    System.out.println(current);
    System.out.println(limit);
    return "some students";
    }

    // /student/123, 获取参数的方式
    @RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getStudent(@PathVariable(name = "id")int id) {
    System.out.println(id);
    return "a student";
    }
  2. Post请求获取参数

    场景:路径的长度是有限的,无法在路径中获取很多的参数,因此使用POST请求接收数据。

    1
    2
    3
    4
    5
    6
    7
    8
    // POST请求
    @RequestMapping(value = "/student", method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String name, int age) {
    System.out.println(name);
    System.out.println(age);
    return "success";
    }

@ResponseBody注解

将方法的返回值直接作为HTTP响应的内容返回

不加这个注解,默认返回HTML数据

@RequestParam注解

用于获取 HTTP 请求中的请求参数,从请求的查询参数中获取参数值

1
2
3
4
5
6
7
@RequestMapping(value = "/students", method = RequestMethod.GET)
@ResponseBody
public String getStudents(
@RequestParam(name = "current", required = false, defaultValue = "1") int current,
@RequestParam(name = "limit", required = false, defaultValue = "1") int limit) {
return "some students";
}

request中名为current的参数赋值给函数中的current参数,required = false表示request中可以没有这个参数,defaultValue表示请求中没有这个参数时默认值为1。

@PathVariable注解

用于获取 URL 中的路径变量的值,从请求的URL路径中获取参数值

1
2
3
4
5
6
7
// /student/123, 获取参数的方式
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable(name = "id")int id) {
System.out.println(id);
return "a student";
}

@Component 注解

@Component 注解应用于类上,以指示该类是一个spring所管理的组件。

@Value 注解

@Value 注解用于将配置属性的值注入到Spring管理的Bean的字段中

@Options注解

数据库相关配置

@Options(useGeneratedKeys = true, keyProperty = "id"):表示主键自生成

Java项目中的三层

Controller

”业务控制层“

处理浏览器的请求,调用业务组件Service

控制器层负责处理客户端发起的HTTP请求,并将请求转发给业务逻辑处理层(Service层)进行处理。

Service

”业务服务层“

业务逻辑层通常包含一组Service类,每个Service类对应系统中的一个业务模块,负责实现该模块的具体业务逻辑。

Dao

”数据库持久层“

数据访问层负责与数据库进行交互,执行数据库操作(增删改查),并将数据传递给业务逻辑层进行处理。

Spring中的常用配置

查找:Spring Boot Reference Guide

将需要的配置写入application.properties