1.@Bean注解

  • Post author:
  • Post category:其他



思考:

  1. 该注解用在哪里
  2. 该注解的作用是什么
  3. 如何使用

一.该注解用在哪里

1.由spring-context提供

该注解位于spring-context.jar包下

2.详解

	@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	public @interface Bean {
		@AliasFor("name")
		String[] value() default {};
		@AliasFor("value")
		String[] name() default {};
		Autowire autowire() default Autowire.NO;
		String initMethod() default "";
		String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

	}

1.@Bean是一个方法级别上的注解,有五个属性

全解析:

@Bean 注解全解析 – 程序员cxuan – 博客园

2.@Bean的value和name属性:

2.3@Bean的value和name属性 – 戴着假发的程序员0-1 – 博客园

在没有设置value和name属性的时候,beanName的生成规则又是什么?


Spring中注解方式的默认beanName生成规则_风尘甘露的博客-CSDN博客_spring默认beanname

3.initMethod和destroyMethod属性:(其中有对

@PostConstruct和@PreDestroy注解的使用



Spring中init-method和destroy-method的四种方式_星夜孤帆的博客-CSDN博客_init-methodh​​​​​​​4


@PostConstruct和@PreDestroy注解的作用详解:


@PostConstruct和@PreDestroy注解的作用详解_技术路上的苦行僧的博客-CSDN博客_predestroy注解


Spring JSR-250 注释_w3cschool

3.Bean 增强


@Bean注解详解_技术宅丶拾年的博客-CSDN博客_@bean

二.该注解的作用是什么

通过

@Configuration和@Bean来代替xml文件。

带有

@Configuration

的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。

@Bean

注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。

三. 如何使用

@bean和@Configuration、 @bean和@Component搭配使用。

1.@bean和@Configuration使用:


  1. Spring中@Bean的作用_云哥Y的博客-CSDN博客_bean作用

  2. Spring 基于 Java 的配置_w3cschool

  3. springboot 中@Bean的注解作用_健康平安的活着的博客-CSDN博客_bean注解作用

2.@bean和@Component使用

3.@Bean注解和@Configuration、@Component注解组合使用的区别:


https://www.jb51.net/article/227032.htm



版权声明:本文为yiguang_820原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。