admin 管理员组文章数量: 887032
@Bean
- @Bean常用来注解@Configuration类中的方法
- @Bean方法产生一个bean并交由spring容器管理
- bean的类型为@Bean方法返回类型
- bean的名称默认为@Bean方法名,可以通过
@Bean("customBeanName")
的形式指定名称(此时是设置value属性),也可以通过@Bean(name = "customBeanName")
或@Bean(name = {"customBeanName1", "customBeanName2"})
的形式指定一个或多个名称 - @Bean方法的入参默认被@AutoWire注解,可以注入其它bean
- initMethod属性:类似@PostConstruct
- destroyMethod属性:类似@PreDestroy
- 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
版权声明:本文标题:@Bean 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1687280243h84509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论