• SpringBoot:第一章JavaConfig、@ImportResource、@PropertyResource等总结(动力)


    在做spring、springmvc项目的时候需要写大量的配置文件,比较麻烦,在大型的项目,对象较多,写的配置文件较多,容易出错,还需要继承第三方的框架,需要写配置文件,比如说写一个mybatis,需要在spring的配置文件中写mybatis集成的对象,做每个对象的配置才能用。Spring也发现了这个问题,spring想改变这种格局,少点写配置,直接去做项目功能,所以打造除了springboot这个框架,解决了spring、sporingmvc的痛点,几乎很少写配置文件,他可以把大多数的框架和第三方资源都已经写好了配置,直接在我们项目中来使用。

    用springboot可以直接实现spring、springmvc那些功能,而不用在写配置文件了,极大的 提高了开发效率,减轻了我们使用配置文件的难度,springboot相当于一个更加快速的spring+springmvc

     

     

     


    目录:

    (1)通过配置文件创建对象:

    (2)使用JavaConfig配置容器:配置Bean

    (3)@ImportResource的使用:读入xml配置文件

     (4)@PropertyResource读入配置文件


     

    pom.xml:引入依赖

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.bjpowernode</groupId>
    7. <artifactId>001-springboot-pre</artifactId>
    8. <version>1.0.0</version>
    9. <dependencies>
    10. <!--spring依赖-->
    11. <dependency>
    12. <groupId>org.springframework</groupId>
    13. <artifactId>spring-context</artifactId>
    14. <version>5.3.1</version>
    15. </dependency>
    16. <dependency>
    17. <groupId>junit</groupId>
    18. <artifactId>junit</artifactId>
    19. <version>4.12</version>
    20. </dependency>
    21. </dependencies>
    22. <build>
    23. <plugins>
    24. <!--编译插件-->
    25. <plugin>
    26. <artifactId>maven-compiler-plugin</artifactId>
    27. <!--插件的版本-->
    28. <version>3.5.1</version>
    29. <!--编译的级别-->
    30. <configuration>
    31. <source>1.8</source>
    32. <target>1.8</target>
    33. <!--编码格式-->
    34. <encoding>UTF-8</encoding>
    35. </configuration>
    36. </plugin>
    37. </plugins>
    38. </build>
    39. </project>

    (1)通过配置文件创建对象: 

    创建spring配置文件

    beans.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <!--声明bean对象,并初始化-->
    6. <bean id="myStudent" class="com.bjpowernode.vo.Student">
    7. <property name="name" value="李四"/>
    8. <property name="age" value="20"/>
    9. <property name="sex" value="女"/>
    10. </bean>
    11. </beans>

    Student类:

    1. package com.bjpowernode.vo;
    2. public class Student {
    3. private String name;
    4. private Integer age;
    5. private String sex;
    6. public String getName() {
    7. return name;
    8. }
    9. public void setName(String name) {
    10. this.name = name;
    11. }
    12. public Integer getAge() {
    13. return age;
    14. }
    15. public void setAge(Integer age) {
    16. this.age = age;
    17. }
    18. public String getSex() {
    19. return sex;
    20. }
    21. public void setSex(String sex) {
    22. this.sex = sex;
    23. }
    24. @Override
    25. public String toString() {
    26. return "Student{" +
    27. "name='" + name + '\'' +
    28. ", age=" + age +
    29. ", sex='" + sex + '\'' +
    30. '}';
    31. }
    32. }

    测试类:

    MyTest:

    1. package com.bjpowernode;
    2. import com.bjpowernode.vo.Student;
    3. import org.junit.Test;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;
    6. public class MyTest {
    7. /*
    8. * 使用xml作为容器配置文件
    9. * */
    10. @Test
    11. public void test01(){
    12. String config="beans.xml";
    13. ApplicationContext ctx=new ClassPathXmlApplicationContext(config);//获取工厂
    14. Student student = (Student) ctx.getBean("myStudent");
    15. System.out.println("容器中的对象:"+student);
    16. }
    17. }

     

    (2)使用JavaConfig配置容器 

    创建配置Bean Java类:SpringConfig:

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.vo.Student;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. @Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
    6. public class SpringConfig {
    7. /*
    8. * 创建方法,方法的返回值是对象,在方法的上面加入@Bean
    9. * 方法的返回值对象就注入到容器中
    10. *
    11. * @Bean:把对象注入到spring容器中,作用相当于<bean>标签
    12. * 说明:@Bean,不指定对象的名称,默认是方法名是id
    13. * */
    14. @Bean
    15. public Student createStudent(){
    16. Student s1=new Student();
    17. s1.setName("张三");
    18. s1.setAge(26);
    19. s1.setSex("男");
    20. return s1;
    21. }
    22. /*
    23. * 指定对象在容器中的名称(指定<bean>的id属性)
    24. * @Bean的Name属性,指定对象的名称(id)
    25. * */
    26. @Bean(name = "lisiStudent")
    27. public Student makeStudent(){
    28. Student s2=new Student();
    29. s2.setName("李四");
    30. s2.setAge(20);
    31. s2.setSex("男");
    32. return s2;
    33. }
    34. }

    MyTest:

    1. package com.bjpowernode;
    2. import com.bjpowernode.config.SpringConfig;
    3. import com.bjpowernode.vo.Student;
    4. import org.junit.Test;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    7. import org.springframework.context.support.ClassPathXmlApplicationContext;
    8. public class MyTest {
    9. /*
    10. * 使用xml作为容器配置文件
    11. * */
    12. @Test
    13. public void test01(){
    14. String config="beans.xml";
    15. ApplicationContext ctx=new ClassPathXmlApplicationContext(config);//获取工厂
    16. Student student = (Student) ctx.getBean("myStudent");
    17. System.out.println("容器中的对象:"+student);
    18. }
    19. /*
    20. * 不使用配置文件,使用JavaConfig
    21. * */
    22. @Test
    23. public void test02(){
    24. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);//获得工厂
    25. Student student = (Student) ctx.getBean("createStudent");//方法名字作为默认的id
    26. System.out.println("使用JavaConfig创建bean对象:"+student);
    27. }
    28. //给对象取名字:@Bean(name = "lisiStudent")
    29. @Test
    30. public void test03(){
    31. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);//获得工厂
    32. Student student = (Student) ctx.getBean("lisiStudent");//方法名字作为默认的id
    33. System.out.println("使用JavaConfig创建bean对象:"+student);
    34. }
    35. }

    test02:

    test03: 

     (3)@ImportResource的使用:读入xml配置文件

     

     Cat类:通过applicationContext.xml配置

    1. package com.bjpowernode.vo;
    2. public class Cat {
    3. private String cardId;
    4. private String name;
    5. private Integer age;
    6. public String getCardId() {
    7. return cardId;
    8. }
    9. public void setCardId(String cardId) {
    10. this.cardId = cardId;
    11. }
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name = name;
    17. }
    18. public Integer getAge() {
    19. return age;
    20. }
    21. public void setAge(Integer age) {
    22. this.age = age;
    23. }
    24. @Override
    25. public String toString() {
    26. return "Cat{" +
    27. "cardId='" + cardId + '\'' +
    28. ", name='" + name + '\'' +
    29. ", age=" + age +
    30. '}';
    31. }
    32. }

    创建一个需要导入的配置文件applicationContext.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="myCat" class="com.bjpowernode.vo.Cat">
    6. <property name="name" value="tom猫"/>
    7. <property name="age" value="2"/>
    8. <property name="cardId" value="uw521651451"/>
    9. </bean>
    10. </beans>

    配置Bean中导入:配置文件:

    SpringConfig:

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.vo.Student;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.context.annotation.ImportResource;
    6. @Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
    7. @ImportResource(value = "classpath:applicationContext.xml") //通过注解引入applicationContext配置文件
    8. //@ImportResource(value = {"classpath:applicationContext.xml","classpath:beans.xml"}) //通过{} 可以导入多个配置文件
    9. public class SpringConfig {
    10. /*
    11. * 创建方法,方法的返回值是对象,在方法的上面加入@Bean
    12. * 方法的返回值对象就注入到容器中
    13. *
    14. * @Bean:把对象注入到spring容器中,作用相当于<bean>标签
    15. * 说明:@Bean,不指定对象的名称,默认是方法名是id
    16. * */
    17. @Bean
    18. public Student createStudent(){
    19. Student s1=new Student();
    20. s1.setName("张三");
    21. s1.setAge(26);
    22. s1.setSex("男");
    23. return s1;
    24. }
    25. /*
    26. * 指定对象在容器中的名称(指定<bean>的id属性)
    27. * @Bean的Name属性,指定对象的名称(id)
    28. * */
    29. @Bean(name = "lisiStudent")
    30. public Student makeStudent(){
    31. Student s2=new Student();
    32. s2.setName("李四");
    33. s2.setAge(20);
    34. s2.setSex("男");
    35. return s2;
    36. }
    37. }

    测试类测试MyTest:

    1. //测试注解@ImportResource 导入xml配置文件
    2. @Test
    3. public void test04(){
    4. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
    5. Cat cat = (Cat) ctx.getBean("myCat");
    6. System.out.println("cat==="+cat);
    7. }

    输出结果证明applicationContest导入成功 

     (4)@PropertyResource读入配置文件

     

     

    Tiger:属性值来自属性配置文件

    1. package com.bjpowernode.vo;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.stereotype.Component;
    4. @Component("tiger") //注解创建Tiger对象,就不用写<bean>标签创建了
    5. public class Tiger {
    6. @Value("${tiger.name}")
    7. private String name;
    8. @Value("${tiger.age}")
    9. private Integer age;
    10. @Override
    11. public String toString() {
    12. return "Tiger{" +
    13. "name='" + name + '\'' +
    14. ", age=" + age +
    15. '}';
    16. }
    17. }

     创建config.properties配置文件:

    tiger.name=东北虎
    tiger.age=3

     在配置Bean中加入注解:SpringConfig:@PropertySource @ComponentScan

    1. package com.bjpowernode.config;
    2. import com.bjpowernode.vo.Student;
    3. import org.springframework.context.annotation.*;
    4. @Configuration //声明注解,配置Bean,代替spring配置文件,就是用来配置容器的
    5. @ImportResource(value = "classpath:applicationContext.xml") //通过注解引入applicationContext配置文件
    6. //@ImportResource(value = {"classpath:applicationContext.xml","classpath:beans.xml"}) //通过{} 可以导入多个配置文件
    7. @PropertySource(value = "classpath:config.properties") //注解读取到properties配置文件
    8. @ComponentScan(basePackages = "com.bjpowernode.vo") //老虎对象使用注解创建的,告诉容器去哪找这个对象上面的注解,使用包扫描
    9. public class SpringConfig {
    10. /*
    11. * 创建方法,方法的返回值是对象,在方法的上面加入@Bean
    12. * 方法的返回值对象就注入到容器中
    13. *
    14. * @Bean:把对象注入到spring容器中,作用相当于<bean>标签
    15. * 说明:@Bean,不指定对象的名称,默认是方法名是id
    16. * */
    17. @Bean
    18. public Student createStudent(){
    19. Student s1=new Student();
    20. s1.setName("张三");
    21. s1.setAge(26);
    22. s1.setSex("男");
    23. return s1;
    24. }
    25. /*
    26. * 指定对象在容器中的名称(指定<bean>的id属性)
    27. * @Bean的Name属性,指定对象的名称(id)
    28. * */
    29. @Bean(name = "lisiStudent")
    30. public Student makeStudent(){
    31. Student s2=new Student();
    32. s2.setName("李四");
    33. s2.setAge(20);
    34. s2.setSex("男");
    35. return s2;
    36. }
    37. }

    测试类:MyTest:

    1. //测试@PropertySource注解,引入配置文件的使用 使用@Component创建对象
    2. @Test
    3. public void test05(){
    4. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
    5. Tiger tiger = (Tiger) ctx.getBean("tiger");
    6. System.out.println("tiger==="+tiger);
    7. }

     

     说明:@Configuration就相当于xml,在类上面写的注解就相当于在原来的Spring配置文件中写的:

     

  • 相关阅读:
    网络编程套接字
    腾讯云2核4G服务器5M带宽 218元一年 优惠价格明细表
    【调研】在线考试系统调研
    C++ Qt开发:TreeWidget 树形选择组件
    大数据平台下的数据治理
    我认为除了HelloWorld之外,Python的三大数据转换实例可以作为开始学习Python的入门语言。
    MySQL 是怎样运行的:单表访问方法及基于成本的优化
    百度智能云千帆AppBuilder升级!开放多源模型接入,思考模型再次加速!
    系统集成|第十六章(笔记)
    总结 Thread 类的基本用法
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125504953