Beans de configuración basados en anotaciones para contenedor Ioc

Configuración basada en anotaciones

Spring 2.5se presentó annotation-based configurationcomo el primer paso para habilitar las configuraciones de beans en Java.

#  via XML configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  
  <context:annotation-config/>
  <context:component-scan base-package="com.annotationconfigvscomponentscan.components" />

</beans>

Descripción: Diferencia entre contexto:annotation-config vs contexto:component-scan


# In a Spring Application
@Configuration
@ComponentScan
public class SpringComponentScanApp {
    
    

@ComponentScan(basePackages = "com.componentscan.springapp.animals")
@Configuration
public class SpringComponentScanApp {
    
    
# In a SpringBoot Application
@SpringBootApplication
public class SpringBootComponentScanApp {
    
    

@SpringBootApplication
@ComponentScan(basePackages = "com.componentscan.springbootapp.animals")
public class SpringBootComponentScanApp {
    
    

Beans de configuración basados ​​en Java para el contenedor Ioc Beans
de configuración basados ​​en anotaciones para el contenedor Ioc
Beans de configuración basados ​​en XML para el contenedor Ioc


Referencia: Escaneo de componentes de resorte

Supongo que te gusta

Origin blog.csdn.net/weixin_37646636/article/details/133281705
Recomendado
Clasificación