春季:@Bean仍然可以在没有@Configuration的情况下工作 [英] Spring: @Bean can still work without @Configuration

查看:124
本文介绍了春季:@Bean仍然可以在没有@Configuration的情况下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 https://projects.spring.io/spring-framework/一个Spring框架hellpworld程序.我删除了注释@Configuration.但是该程序仍可以像以前一样运行.为什么? @Configuration在这里是什么角色?

From https://projects.spring.io/spring-framework/ I have a spring framework hellpworld program. I delete the annotation @Configuration. However the program could still run as before. Why? What is @Configuration role here?

推荐答案

您仍然可以使用@Component标记该类,以使@Bean实例可用于编程.当您执行此操作时,它称为精简模式.在这种模式下,您不能使用"bean引用",即通过方法引用其他实例.

You can still mark the class with @Component for @Bean instances to be available to program. when you do that it is called lite mode. In this mode you can not use 'inter-bean references' , means referring other instance via methods.

另一方面,类@Configuration的@Bean被包装在侧面cglib包装器中,在该包装器中,可以拦截对此bean方法的任何调用,并且可以从上下文返回bean实例.意味着您可以使用"bean间引用".

On the the other hand @Bean with class @Configuration is wrapped in side cglib wrapper where any calls to this bean methods can be intercepted and bean instances can be return from context. Means you can use 'inter-bean references'.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html

  1. @Configuration类中的@Bean方法

通常,@ Bean方法在@Configuration类中声明. 在这种情况下,bean方法可以在 通过直接调用它们来实现相同的类.这样可以确保引用 Bean之间的类型是强类型且可导航的.所谓的 保证"bean间引用"尊重作用域和AOP 语义,就像getBean()查找一样.这些是语义 从原始的"Spring JavaConfig"项目中知道,需要 运行时每个此类配置类的CGLIB子类化.作为一个 因此,@ Configuration类及其工厂方法不得 在此模式下被标记为最终或私有.

Typically, @Bean methods are declared within @Configuration classes. In this case, bean methods may reference other @Bean methods in the same class by calling them directly. This ensures that references between beans are strongly typed and navigable. Such so-called 'inter-bean references' are guaranteed to respect scoping and AOP semantics, just like getBean() lookups would. These are the semantics known from the original 'Spring JavaConfig' project which require CGLIB subclassing of each such configuration class at runtime. As a consequence, @Configuration classes and their factory methods must not be marked as final or private in this mode.

  • @Bean Lite模式
  • @Bean方法也可以在非以下类中声明 用@Configuration注释.例如,bean方法可能是 在@Component类中甚至在普通的旧类中声明.在这样的 在这种情况下,@ Bean方法将以所谓的精简"模式进行处理.

    @Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

    Bean方法将被视为普通工厂方法 容器(类似于XML中的工厂方法声明), 作用域和生命周期回调已正确应用.包含类 在这种情况下仍保持不变,并且没有异常约束 包含类或工厂方法.

    Bean methods in lite mode will be treated as plain factory methods by the container (similar to factory-method declarations in XML), with scoping and lifecycle callbacks properly applied. The containing class remains unmodified in this case, and there are no unusual constraints for the containing class or the factory methods.

    与@Configuration中的bean方法的语义相反 lite模式不支持类间引用"类. 相反,当一个@Bean方法调用lite中的另一个@Bean方法时 模式下,该调用是标准的Java方法调用;春天确实 不能通过CGLIB代理拦截调用.

    In contrast to the semantics for bean methods in @Configuration classes, 'inter-bean references' are not supported in lite mode. Instead, when one @Bean-method invokes another @Bean-method in lite mode, the invocation is a standard Java method invocation; Spring does not intercept the invocation via a CGLIB proxy.

    然后 @Configuration使您能够结合其他注释使用许多其他功能

    And, @Configuration gives you the ability use many other features with conjunction of other annotations

    导入其他配置 @Import(DatabaseConfig.class)

    Importing other config @Import(DatabaseConfig.class)

    资源导入@PropertySource("classpath:config.properties")

    resource import @PropertySource("classpath:config.properties")

    启用组件扫描@ComponentScan(basePackages = {"com.sample.*"})

    enable component scan @ComponentScan(basePackages = { "com.sample.*" })

    标记配置文件@Profile(生产")

    marking profile @Profile("production")

    启用功能@​​Enablexxxx

    To enable features @Enablexxxx

    查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆