Annotation-Based Configuration Beans for Ioc Container

Annotation-Based Configuration

Spring 2.5 introduced annotation-based configuration as the first step to enable bean configurations in 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>

参考:Difference between context:annotation-config vs context: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 {
    
    

Java-Based Configuration Beans for Ioc Container
Annotation-Based Configuration Beans for Ioc Container
XML-Based Configuration Beans for Ioc Container


参考: Spring Component Scanning

猜你喜欢

转载自blog.csdn.net/weixin_37646636/article/details/133281705
今日推荐