• 扫描 + 注解完成bean的自动配置


    链接:https://pan.baidu.com/s/1W3TINXNnqpxmkIADOcJZCQ
    提取码:fmt5

    我们知道,我们一般是通过id或name调用getBean方法来从IOC容器中获取相应的bean对象

    这样就带来一个问题,我们需要写application.xml文件来把bean写入配置文件中

    这种方式属于手动配置,比较麻烦

    接下来,我们使用扫描 + 注解的方式,完成bean的自动配置:

    首先在配置文件application.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        xmlns:context="http://www.springframework.org/schema/context"
    5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6     <context:component-scan base-package="top.bigking.bean"/>
    7 </beans>

    可以看到,我们使用了<context:component-scan>标签来进行自动扫描,扫描哪里呢? base-package的值就是指定相应的包,该包下面的类全部被转化为bean对象

    同时应该注意的是,类名首字母是大写,自动扫描时,会把类名首字母小写作为id,getBean时,使用这个id即可

    同时,在top.bigking.bean包下的类,应该加上@Component注解,表示这个类是被扫描的bean对象之一

    在测试类中:

     1 package top.bigking.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 import top.bigking.bean.Animal;
     7 import top.bigking.conf.TestConfiguration;
     8 
     9 public class BigKingTest {
    10     public static void main(String[] args) {
    11         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
    12         Animal animal = (Animal) applicationContext.getBean("dog");
    13         animal.bark();
    14     }
    15 }

    我们通过ClassPathXmlApplicationContext来解析配置文件,初始化IOC容器

    并通过getBean来获取bean对象

    你能注意到,这仍然使用了application.xml配置文件,和之前没什么区别嘛

    所以,我们只是用java代码,进行更加自动的配置

    创建一个配置类

     1 package top.bigking.conf;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.ComponentScan;
     5 import org.springframework.context.annotation.Configuration;
     6 import top.bigking.bean.Animal;
     7 import top.bigking.bean.Cat;
     8 import top.bigking.bean.Dog;
     9 
    10 @Configuration
    11 @ComponentScan("top.bigking.bean")
    12 public class TestConfiguration {
    13 
    14     @Bean
    15     public Animal Cat(){
    16         return new Cat();
    17     }
    18     @Bean
    19     public Animal Dog(){
    20         return new Dog();
    21     }
    22 
    23 }

    应该注意其中的@ComponentScan注解,括号中的参数的作用,和之前配置文件中的base-package="top.bigking.bean"的作用一致

    @Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean

    所以在相应的类定义中,@Component注解不能删去

    在测试类中,相应的,从获取配置文件,变成了获取配置类

     1 package top.bigking.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 import top.bigking.bean.Animal;
     7 import top.bigking.conf.TestConfiguration;
     8 
     9 public class BigKingTest {
    10     public static void main(String[] args) {
    11         //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
    12         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfiguration.class);
    13         Animal animal = (Animal) applicationContext.getBean("dog");
    14         animal.bark();
    15     }
    16 }

    这样,就实现了bean的自动配置,不需要自己再写在配置文件里了!

    金麟岂是池中物,一遇风云便化龙!
  • 相关阅读:
    一个.java源文件中可以有多个类吗?(内部类除外)有什么条件?
    接口中定义的变量为什么是常量
    关于String s = new String("xyz");创建了几个字符串对象?的问题
    java面试题之----JVM架构和GC垃圾回收机制详解
    Object中的clone方法
    C/S与B/S架构的区别和优缺点
    EJB是什么?
    JNDI是什么,怎么理解
    java中什么是上下文(servletContext)
    java面试题----String、StringBuffer、StringBudder区别
  • 原文地址:https://www.cnblogs.com/ABKing/p/12044143.html
Copyright © 2020-2023  润新知