javaWebv-spring-Boot注解全家桶

目录标题

1.@RequestParam

示例1

@RequestMapping("/menuQuery")
public Object meanQuery(@RequestParam Integer id){
	......
	......省略代码
}

在这里插入图片描述这里写入的参数就会从前端传入到后端代码中
如果不强制要求传入参数的话,需要做如下设置

required该参数是否必须。默认为true;
defaultValue请求参数的默认值。

@RequestMapping("/menuQuery")
public Object meanQuery(@RequestParam(required = false) Integer id){
	......
	......省略代码
}

示例2

 
@Controller
@RequestMapping("hello")
public class HelloController2 {
 
    /**
     * 接收普通请求参数
     * http://localhost:8080/hello/show16?name=linuxsir
     * url参数中的name必须要和@RequestParam("name")一致
     * @return
     */
    @RequestMapping("show16")
    public ModelAndView test16(@RequestParam("name")String name){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello2");
        mv.addObject("msg", "接收普通的请求参数:" + name);
        return mv;
    }

在这里插入图片描述

2.@RequestMapping

@RequestMapping("/menuQuery")
public Object meanQuery(){
	......
	......省略代码
}

在这里插入图片描述通过@RequestMapping注解可以实现前端通过访问注解定义url地址,可以访问到后端注解下的代码块.

3. @Autowired与@Resource

public class MenuService {
    @Autowired
    MenuMapper menumapper;
...........
..........省略代码
}

通过@Autowired注解实现的其实就是menumapper = new MenuMapper,把一个设置好的类实例化到当前类内部.

在这里插入图片描述

4.HttpServletRequest

HttpServletRequest与@RequestParam实现目的接收前端发送过来的数据,只不过HttpServletRequest接收的是一个封装好的对象.
HttpServletRequest接口最常用的方法就是获得请求中的参数,这些参数一般是客户端表单中的数据。同时,HttpServletRequest接口可以获取由客户端传送的名称,也可以获取产生请求并且接收请求的服务器端主机名及IP地址,还可以获取客户端正在使用的通信协议等信息。下表是接口HttpServletRequest的常用方法。

在这里插入图片描述在这里插入图片描述

5.HttpServletResponse

在Servlet中,当服务器响应客户端的一个请求时,就要用到HttpServletResponse接口。设置响应的类型可以使用setContentType()方法。发送字符数据,可以使用getWriter()返回一个对象。
下表是接口HttpServletResponse的常用方法。
在这里插入图片描述

5.1sendRedircet(String location)

sendRedircet(放入一个模板页面) 他可以实现页面的跳转,运行到sendRedircet时,就会跳转到括号内传入的模板页面

6.@RequestBody

注意:Get请求中,不能使用@RequestBody

  • @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
  • @RequestBody直接以String接收前端传过来的json数据.
  • 在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
  • 一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam。

7.@RequestParam

@RequestParam注解定义在方法传参内,实现的是前端网页传入参数后发生到后端

http://localhost/account/list?pageNum=1

问号,后面的pageNum=1就是前端写入的参数,会通过@RequestParam注解,传递到后端,然后在后端使用pageNum这个变量,进行相关的操作(一般是使用场景就是翻页的实现)

@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
		
		PageInfo<Account>page = accountSrv.findByPage(pageNum,pageSize);
		model.addAttribute("page", page);
		return "/account/list";
	}
  1. required:请求参数中是否必须提供此参数,默认值是true,true为必须提供
  2. defaultValue:默认值

8.@ConfigurationProperties

8.1使用场景:

将大量的参数配置在 application.properties或application.yml文件中,通过 @ConfigurationProperties 注解,我们可以方便的获取这些参数值

package com.yuge.wechat.questionnaire.conf;


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Data
@Configuration
@ConfigurationProperties(prefix = "template")//通过这个前缀去application.yaml配置文件中找寻对应的参数
public class WechatTemplateProperties {
    private List<WetchTemplate> templates;  //对应下面配置文件的templates
    private int templateResultType; //0-文件夹 1-数据库   //对应下面配置文件的templateResultType
    private String templateResultFilePath;//结果文件路径   //templateResultFilePath


    @Data
    public static class WetchTemplate{
        private String templateId;   //模板编号,用来模板对应接口的唯一标识
        private String templateFilePath;
        private boolean active;
    }
}

在这里插入图片描述

8.2注意:

使用时要通过添加 @Component 或者@Configuration注解让 Component Scan 扫描到

8.3@Validated与@Email配对使用

注解下的字段必须符合emil格式,否则就会报错

@ConfigurationPeroperties(perfix="person")
@Component
@Validated
public class Person{
	@Email
	private String lastName;
}

9.@Configuration与@Bean的共同使用

@Configuration可理解为用spring的时候xml里面的标签
@Bean可理解为用spring的时候xml里面的标签

    <beans>
        <bean id="myService" class="com.acme.services.MyServiceImpl"/>
    </beans>

使用@Bean 注解表明MyServiceImpl需要交给Spring进行管理,做一个单例模式,这样我们所以的线程都是共享同一个MyServiceImpl,不会出现线程安全问题

10Model

Model是放在方法的参数列表里面,使用model.addAttribute(“供前端调用的名称”,数据集)可以实现:从后端mysql查询数据传递给前端页面使用

  @RequestMapping("/list")
    public String list(Model model){

        List<Account>accountList = accountSrv.findAll();//去server层获取数据库内容
        model.addAttribute("accountList",accountList);//通过model把从数据库获取信息传递到前端

        return "account/list"; 
    }

11.PageHelper:分页的实现

PaheHelper官方文档

11.1controller层示例代码

@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5" ) int pageSize,Model model) {
		
		PageInfo<Account>page = accountSrv.findByPage(pageNum,pageSize);
		//我们拿到server层返回过来的封装好的PageInfo类型数据集,我们通过model发送给前端页面,前端可以通过page来调用
		model.addAttribute("page", page);
		return "/account/list";
	}

11.2service层的示例代码

public PageInfo<Account> findByPage(int pageNum, int pageSize) {

		PageHelper.startPage(pageNum, pageSize); //页码,和每页显示数量

		AccountExample example = new AccountExample();
		List<Account> list = accMapper.selectByExample(example);

		//通过传入的数据集list,和准备分几页,计算一页多少数据,做一个PageInfo的数据集返回出去
		return new PageInfo<>(list, 5);
	}

12.MultipartFile用户文件上传

MultipartFile是写在方法传入参数位置,通过MultipartFile实现用户上传文件,后端接收

//用户上传文件存储的路径应该避免中文字符,否则会报错
@RequestMapping("/fileUploadController")
	public String fileUpload (MultipartFile filename,String password) { //这里要注意必须叫filename,要不然后面找不到MultipartFile
		System.out.println("password:" + password);
		System.out.println("file:" + filename.getOriginalFilename());
		try {
			
		File path = new File(ResourceUtils.getURL("classpath:").getPath());
        File upload = new File(path.getAbsolutePath(), "static/upload/");
        
        System.out.println("upload:" + upload);
        
        filename.transferTo(new File(upload+"/"+filename.getOriginalFilename()));
        
        
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "profile";
	}

上述代码就是实现的一个文件上传接口.一般文件上传都按照此类进行书写.

13.@Controller和@RestController

13.@Controller

将方法类注解为一个可供前端调用的接口

@Controller
public class HelloController{
	@RequestMapping("/hello)
	@RequestBody
	public String hello(){
		return "hello"
	}
}

13.2@RestController

@RestController他是@ResponseBody与@Controller整合版,有些人嫌上述麻烦,可以使用@RestController来简化代码

@RestController
public class HelloController{
	@RequestMapping("/hello) //这里就少了@RequestBody,@RequestBody就被@RestController给整合了
	public String hello(){
		return "hello"
	}
}

14.@component

1、@controller 控制器(注入服务)

2、@service 服务(注入dao)

3、@repository dao(实现dao访问)

4、@component (把普通pojo实例化到spring容器中)

@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
使用了这个参数才可以在其他地方使用@Autowired进行注入

15.元注解

java中定义了四个元注解,他们的作用是负责注解其他的注解

15.1@Target

描述注解可以在什么地方使用

@Target(ElementType.FIELD,ElementType.MEYHOD...) //字段,方法...

15.2@Retention

描述注解的生命周期(souce,class,runtime),可以指定注解在什么阶段生效

15.3@Document

说明该注解被包含在javadoc中

15.4@Inherited

说明子类可以继承父类中的该注解

16.@WebServlet与@ServletComponentScan共同使用

16.1通过@WebServlet定义一个servlet

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name="myServlet",urlPatterns = "/srv")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("111");
        super.doGet(req, resp);
    }
}

16.2想要上述的Servlet生效还需要在启动类上面加入@ServletComponentScan,在把其注解为bean对象,通过上述操作springBoot才能知道这个Servlet什么时候生效

import com.example.demo.servlet.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.Servlet;

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

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

    @Bean
    public ServletRegistrationBean<MyServlet> getServletRegistrationBean(){
        ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(new MyServlet());
        bean.setLoadOnStartup(1);
        return bean;
    }

}

17.@GetMapping和@PostMapping

17.1@GetMapping和@PostMapping

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。

@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。

17.2若符合下列任一情况,则用POST方法:

  • 请求的结果有持续性的副作用,例如,数据库内添加新的数据行。
  • 若使用GET方法,则表单上收集的数据可能让URL过长。
  • 要传送的数据不是采用7位的ASCII编码。

17.3若符合下列任一情况,则用GET方法:

  • 请求是为了查找资源,HTML表单数据仅用来帮助搜索。
  • 请求结果无持续性的副作用。
  • 收集的数据及HTML表单内的输入字段名称的总长不超过1024个字符。

18.@Id与@GeneratedValue

@id:主键
@GeneratedValue(strategy=GenerationType.IDENTITY) 主键自增长

19.@Table

用来命名,当前实体类对应数据库的表名称
常用的两个属性:
1、name 用来命名 当前实体类 对应的数据库 表的名字

@Table(name = "tab_user"

2、uniqueConstraints 用来批量命名唯一键
其作用等同于多个:@Column(unique = true)

@Table(name = "tab_user",uniqueConstraints = {@UniqueConstraint(columnNames={"uid","email})})

在这里插入图片描述在这里插入图片描述

20@ExceptionHandler

@ExceptionHandler:统一处理某一类异常,从而能够减少代码重复率和复杂度
该注解作用对象为方法,并且在运行时有效
@ControllerAdvice与@ExceptionHandler配合使用捕获全局的异常,拦截controller所有抛出的异常

21@ControllerAdvice:

异常集中处理,更好的使业务逻辑与异常处理剥离开;其是对Controller层进行拦截

22@ResponseStatus:

可以将某种异常映射为HTTP状态码

23@MappedSuperclass注解

使用条件:

当我们进行开发项目时,我们经常会用到实体映射到数据库表的操作,此时我们经常会发现在我们需要隐射的几个实体类中,有几个共同的属性,例如编号ID,创建者,创建时间,修改者,修改时间,备注等。遇到这种情况,我们可能会想到把这些属性抽象出来当成一个父类,然后再以不同的实体类来继承这个父类。

那么,我们便可以使用@MappedSuperclass注解,通过这个注解,我们可以将该实体类当成基类实体,它不会隐射到数据库表,但继承它的子类实体在隐射时会自动扫描该基类实体的隐射属性,添加到子类实体的对应数据库表中。

使用环境:

1.@MappedSuperclass注解使用在父类上面,是用来标识父类的

2.@MappedSuperclass标识的类表示其不能映射到数据库表,因为其不是一个完整的实体类,但是它所拥有的属性能够隐射在其子类对用的数据库表中

3.@MappedSuperclass标识得嘞不能再有@Entity或@Table注解

25.@JsonIgnore

@JsonIgnore注解作用
此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
此注解用于属性或者方法上(最好是属性上)

public class user {
private String name;
@JsonIgnore
private int age; //生成json 时不生成age 属性

28.@ResponseBody

在这里,@responsebody这个注解表示你的返回值将存在response body中返回到前端,前端接收后会显示将数据到页面,页面不会跳转。

如果不加的话 返回值将会作为url的一部分,也就是页面会进行跳转,跳转到你返回的路径。
假如是字符串则直接将字符串写到客户端,假如是一个对象,此时会将对象转化为json串然后写到客户端。这里需要注意的是,如果返回对象,按utf-8编码。如果返回String,默认按iso8859-1编码,页面可能出现乱码。因此在注解中我们可以手动修改编码格式,例如@RequestMapping(value="/cat/query",produces=“text/html;charset=utf-8”),前面是请求的路径,后面是编码格式。

29.JPA相关注解

在这里插入图片描述[@Column]
用于指定列的相关属性

  • name - 可选,表示数据库表中列的名称。
    默认为属性或字段名称。
  • nullable - 可选,表示该字段是否允许为 null,默认为 true(null)
    若设置为false 则该列不可为null值
  • unique - 可选,表示该字段是否是唯一标识,默认为 false(不唯一)
    若为true 则表示该列唯一
    如 uuid, email, mobile 等属性
  • length - 可选,表示该字段的大小,仅对 String 类型的字段有效,默认值 255.
    用来自定义列的长度 如 mobile (length=11)
  • insertable - 可选,表示在 ORM 框架执行插入操作时,该字段是否应出现 INSETRT
    语句中,默认为 true
  • updateable - 可选,表示在 ORM 框架执行更新操作时,该字段是否应该出现在 UPDATE 语句中,
    默认为 true. 对于一经创建就不可以更改的字段,该属性非常有用,
    如对于 birthday 字段。
    或者创建时间/注册时间(可以将其设置为 false 不可修改)
  • precision 可选,列十进制精度(decimal precision)(默认值 0)
  • scale 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值 0)
  • columnDefinition - 可选,表示该字段在数据库中的实际类型

30.@EnableJpaRepositories

在Springboot应用开发中使用JPA时,通常在主应用程序所在包或者其子包的某个位置定义我们的Entity和Repository,这样基于Springboot的自动配置,无需额外配置,我们定义的Entity和Repository即可被发现和使用。但有时候我们需要定义Entity和Repository不在应用程序所在包及其子包,那么这时候就需要使用@EntityScan和@EnableJpaRepositories了。

@EnableJpaRepositories用来扫描和发现指定包及其子包中的Repository定义
@EnableJpaRepositories(basePackages = {“com.department.repositories”,“come.employee.repositories”})

31.在 SpringBoot 中使用 @EnableScheduling、@Scheduled 轻松实现定时任务

1.时候注解可以达到的效果:

在这里插入图片描述

2.代码具体实现步骤:

2.1在 main 中开启定时任务的注解 @EnableScheduling

package com.cun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling   //开启定时任务注解
@SpringBootApplication
public class AsMailTaskApplication {

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

2.2在 Service 中编写定时任务 @Scheduled

package com.cun.service;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class TaskService {

    /**
     * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
     * 例子:
     *  【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
     *  【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
     *  【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
     *  【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
     *  【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
     */
    @Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
    public void runTask(){
        System.out.println(new Date()+"发布王者战报");
    }
}

2.3maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

32.读取配置文件application.yml 的属性值:@Value("${属性名}")注解

如果需要读取配置文件application.yml 的属性值,只需要在变量上加 @Value("${属性名}") 注解,就可以将配置文件 application.yml 的一个属性值赋值给变量。
在这里插入图片描述

如果我们在对象的构造方法中使用这个变量,结果发现这个变量的值为null。
原因是 @Value("${属性名}") 注解是通过对象的set 方法赋值的,构造方法的执行在set方法之前,所以在构造方法中使用变量会发现变量为 null。
那么在构造方法中如果要使用配置文件中的属性值,该怎么使用呢?见下方代码:

public RedisBeanFactory(@Value("${myredis.hostname}") String hostName,
			@Value("${myredis.port}") int port, 
			@Value("${myredis.password}") String password) {
	try {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxTotal(maxActive);
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMaxWaitMillis(maxWait);
		poolConfig.setTestOnBorrow(testOnBorrow);
		if ("".equals(password.trim())) {
			password = null;
		}
		pool = new JedisPool(poolConfig, hostName.trim(), port, timeOut, password);
		pool.getResource();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

33.解决跨域问题:@CrossOrigin

1.跨域问题产生的原因:

出于安全原因,浏览器禁止Ajax调用驻留在当前原点之外的资源。例如,当你在一个标签中检查你的银行账户时,你可以在另一个选项卡上拥有EVILL网站。来自EVILL的脚本不能够对你的银行API做出Ajax请求(从你的帐户中取出钱!)使用您的凭据。

跨源资源共享(CORS)是由大多数浏览器实现的W3C规范,允许您灵活地指定什么样的跨域请求被授权,而不是使用一些不太安全和不太强大的策略,如IFRAME或JSONP。

2.@CrossOrigin注解

Spring Framework 4.2 GA为跨域提供了第一类支持,使您比通常的基于过滤器的解决方案更容易和更强大地配置它。所以springMVC的版本要在4.2或以上版本才支持@CrossOrigin

3.@CrossOrigin使用方法:

3.1controller配置跨域

controller方法的跨域配置,您可以向@RequestMapping注解处理程序方法添加一个@CrossOrigin注解,以便启用跨域(
其中@CrossOrigin中的2个参数:

@RestController
@RequestMapping("/account")
public class AccountController {
 
    @CrossOrigin
    @GetMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }
 
    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}

3.2为整个controller启用@CrossOrigin

origins : 允许可访问的域列表
maxAge:准备响应前的缓存持续的最大时间(以秒为单位)。

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
 
    @GetMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }
 
    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}

2.spring注解@CrossOrigin不起作用的原因

1、是springMVC的版本要在4.2或以上版本才支持@CrossOrigin

2、非@CrossOrigin没有解决跨域请求问题,而是不正确的请求导致无法得到预期的响应,导致浏览器端提示跨域问题。

3、在Controller注解上方添加@CrossOrigin注解后,仍然出现跨域问题,解决方案之一就是:

在@RequestMapping注解中没有指定Get、Post方式,具体指定后,问题解决。

@CrossOrigin
@RestController
public class person{
    
    @RequestMapping(method = RequestMethod.GET)
    public String add() {
        // 若干代码
    }
}

34.枚举注解@Enumerated

@Enumerated用于标注枚举字段
使用@Enumerated 默认存储为Integer类型,存储的是枚举类的ORDINAL属性,根据枚举值在枚举类中的顺序按0,1,2…排列,
如果使用@Enumerated(EnumType.STRING),存储的是枚举类的名称

35.@RequestBody注解:

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
RequestBody 接收的是请求体里面的数据,会根据json字符串中的key来匹配对应实体类的属性
而RequestParam接收的是key-value里面的参数,所以它会被切面进行处理从而可以用普通元素、数组、集合、对象等接收)。

语法:@RequestParam(value=”参数名”,required=true/false,defaultValue=””)
 
value:参数名
 
required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。
 
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

35.lombok汇总

1.@NoArgsConstructor:

自动生成无参数构造函数。

2.@AllArgsConstructor:

自动生成全参数构造函数。

3.@Data:

作用于类上,是以下注解的集合:@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
注解功能
1、@Data可以为类提供读写功能,从而不用写get、set方法。
2、他还会为类提供 equals()、hashCode()、toString() 方法。

4.@Getter/@Setter:

作用类上,生成所有成员变量的getter/setter方法;作用于成员变量上,生成该成员变量的getter/setter方法。可以设定访问权限及是否懒加载等。

5.@ToString:

作用于类,覆盖默认的toString()方法,可以通过of属性限定显示某些字段,通过exclude属性排除某些字段。
在这里插入图片描述

6.@Builder

  • @Builder提供在设计数据实体时,对外保持private setter,而对属性的赋值采用Builder的方式,这种方式最优雅,也更符合封装的原则,不对外公开属性的写操作!
  • @Builder声明实体,表示可以进行Builder方式初始化,@Value注解,表示只公开getter,对所有属性的setter都封闭,即private修饰,所以它不能和@Builder现起用。
    在这里插入图片描述

36@PostConstruct

@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值