Spring @Configuration注解

1 该注解的用途

这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换。有了这个注释,Spring framework就能在需要的时候构造出bean出来,然后完成bean的注入。

2 一般使用方式

package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

等价于
<beans>
   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" />
</beans>

3 使用@Configuration创建单例

@Bean默认就是单例的。

4 关于@Configuration里面返回Bean的函数的名字

这个名字可以任意取。

原文地址:https://www.cnblogs.com/hustdc/p/9541532.html