admin 管理员组

文章数量: 887032

@Bean

  1. @Bean常用来注解@Configuration类中的方法
  2. @Bean方法产生一个bean并交由spring容器管理
  3. bean的类型为@Bean方法返回类型
  4. bean的名称默认为@Bean方法名,可以通过@Bean("customBeanName")的形式指定名称(此时是设置value属性),也可以通过@Bean(name = "customBeanName")@Bean(name = {"customBeanName1", "customBeanName2"})的形式指定一个或多个名称
  5. @Bean方法的入参默认被@AutoWire注解,可以注入其它bean
  6. initMethod属性:类似@PostConstruct
  7. destroyMethod属性:类似@PreDestroy
  8. autowireCandidate属性:参考
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {@AliasFor("name")String[] value() default {};@AliasFor("value")String[] name() default {};// 8 当可注入的bean有多个时,指定注入autowireCandidate=true的bean,即注入代表boolean autowireCandidate() default true;/** 6* The optional(可选的, 非强制的) name of a method to call on the bean instance during initialization.* The default value is "", indicating no init method to be called.*/String initMethod() default "";/** 7* The optional name of a method to call on the bean instance upon(在…上面) closing the application context.* The method must have no arguments but may throw any exception.* The container will attempt to infer a destroy method. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'.* To disable destroy method inference, specify an empty string as the value, e.g. @Bean(destroyMethod="").* Note that the org.springframework.beans.factory.DisposableBean callback interface will nevertheless(仍然, 尽管如此, 不过) get detected and the corresponding destroy method invoked: In other words, destroyMethod="" only affects custom close/shutdown methods and java.io.Closeable/AutoCloseable declared close methods.* Note: Only invoked on beans whose lifecycle is under the full control of the factory, which is always the case for singletons but not guaranteed for any other scope.*/String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

本文标签: Bean