current position:Home>Chapter 216 Play with Spring5.X Xml configuration conversion to annotation configuration

Chapter 216 Play with Spring5.X Xml configuration conversion to annotation configuration

2022-08-06 19:04:57LiuJia111222333

1集 论Spring使⽤⽅式之XML和注解的优缺点

简介:讲解Spring使⽤xml和注解的优缺点 

  • spring的使⽤⽅式有两种 xml配置和注解 
    • Some companies only⽤其中⼀种,也有公司xml Configuration and annotation configuration⼀起使⽤
  • 注解的优势:配置简单,维护⽅便 
  • xml的优势:单修改xml时不⽤改源码,不⽤重新编译和部署
  • 结论: Look at the team's development specifications⾏选择,没有强调⼀定⽤哪个 更多的是xml+Annotation cooperates⽤,⽐如spring整合mybatis

2集 实战Spring5.XThe annotation configuration item⽬ 

简介:讲解spring的使⽤Annotation configuration items⽬ 

  • Enable annotation configuration and package scanning 
**
 * 使用Spring注解开发
 */
public class AnnotationMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        //扫描bean包,包括子包 xj.com下所有包
        context.scan("xj.com");

        //完成xj.comThe initialization operation under the package,核心方法
        context.refresh();
        //Class methods are lowercase
        Video video = (Video)context.getBean(Video.class);
        video.init();


    }
}

Sprig配置:使用第三方bean

/**
 * Create a third partybean
 */
@Configuration //配置一个Configuration配置beans
public class AppConfig {
    //配置一个bean,绑定Bean的初始化
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Video video(){
        return new Video();
    }

}

3集 实战Spring5.X的常⽤注解和xml对⽐《上》

  • 常⽤注解
    • xml⽅式:
      • 注解⽅式:@Component 通⽤组件 细分: @Controller (⽤于web层) @Service (service) @Repository (⽤于dao仓库层)
    •  bean取名
      • xml⽅式:通过id或者name
      • 注解⽅式:@Component("XXXX")
    • bean注入  
      • xml⽅式:通过
      • 注解⽅式:类型注⼊@Autowired name note⼊@Qualififier

4集 实战Spring5.X的常⽤注解和xml对⽐《下》 

  • bean⽣命周期
    • xml⽅式:init-methoddestroy-method
    • 注解⽅式:@PostConstruct初始化、@PreDestroy销毁
使用Component注册Bean
@Component
@Scope //设置单例
public class Video {
    private Integer id;
    private String title;
    private String summary; //概述

    private String coverImg;
    private Integer price;
    private Date createTime;
    private Integer cId;
    private Double point;

    //Implement the initialization operation of the entity class
    @PostConstruct
    public void init(){
        System.out.println("使用Bean的Init初始化方法");
    }
    //Implement the destroy operation of the entity class
    @PreDestroy
    public void destroy(){
        System.out.println("video销毁方法");
    }
}

测试类:        

   public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        //扫描bean包,包括子包 xj.com下所有包
        context.scan("xj.com");

        //完成xj.comThe initialization operation under the package,核心方法
        context.refresh();
        //Class methods are lowercase
        Video video = (Video)context.getBean(Video.class);
        video.init();

}

运行结果:Perform initialization twice,是因为使用了  @PostConstruct注解,还有Video.init()调用了一次.l

copyright notice
author[LiuJia111222333],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061859100928.html

Random recommended