zoukankan      html  css  js  c++  java
  • 扫描 + 注解完成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的自动配置,不需要自己再写在配置文件里了!

    金麟岂是池中物,一遇风云便化龙!
  • 相关阅读:
    VisualSVN-Server windows 版安装时报错 "Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for more details."
    Pytest 单元测试框架之初始化和清除环境
    Pytest 单元测试框架入门
    Python(email 邮件收发)
    Python(minidom 模块)
    Python(csv 模块)
    禅道简介
    2020年最好的WooCommerce主题
    Shopify网上开店教程(2020版)
    WooCommerce VS Magento 2020:哪个跨境电商自建站软件更好?
  • 原文地址:https://www.cnblogs.com/ABKing/p/12044143.html
Copyright © 2011-2022 走看看