Spring 2.5
introduced annotation-based configuration
as the first step to enable bean configurations in Java.
# via XML configuration
<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>
Further Reading: context:annotation-config 和 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 {
Further Reading: Spring Component Scanning