@Configuration

简介: @Configuration

被 @Configuration 标注的类,会被 Spring 的IOC容器认定为配置类。
一个被 @Configuration 标注的类,相当于一个 applicationContext.xml 的配置文件。

例如:声明一个类,并标注 @Configuration 注解:
@Configuration
public class ConfigurationDemo {
@Bean
public Date currentDate() {
return new Date();
}
}

上述注册Bean的方式类比于xml:

之后使用注解启动方式,初始化一个IOC容器,并打印IOC容器中的所有bean的name:

public class MainApp {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigurationDemo.class);
String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
Stream.of(beanDefinitionNames).forEach(System.out::println);
}
}

输出结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
configurationDemo
currentDate

可以发现组件,以及配置类本身被成功加载。

相关文章
|
7月前
|
Java 数据库连接 数据库
@Configuration大悟!
@Configuration大悟!
|
XML Java 数据格式
@Configuration
@Configuration
|
Java 数据库连接
org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration
Error creating bean with name 'entityManagerFactory' defined in file [E:\eclipseworkspace\wms_ims\.metadata\.plugins\org.eclipse.wst.server.core\tmp9\wtpwebapps\shopping\WEB-INF\classes\applicationContext-configuration.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMetho
108 0
|
Java Maven Spring
4. 成功解决:Spring Boot Configuration Annotation Processor not configured
在 Spring Boot 项目中,添加一个配置文件时,会提示“Spring Boot Configuration Annotation Processor not configured”
2253 0
|
Java Spring
Spring Boot Configuration Annotation Processor not configured
Spring Boot Configuration Annotation Processor not configured
499 0
|
XML 存储 SQL
Configuration和Mapper|学习笔记
快速学习Configuration和Mapper
177 0
Configuration和Mapper|学习笔记
|
Java Spring 容器
@Configuration注解
@Configuration注解
168 0
|
Java Spring
SpringBoot中@ConfigurationProperties提示:Configuration Annotation Processor not found in classpath
SpringBoot中@ConfigurationProperties提示:Configuration Annotation Processor not found in classpath
414 0