フレンドリーヒント:
パス:org.springframework.context.annotation.Configurationの
バージョン:5.2.0.BUILD
公式のノートでは説明しています。
1は、@ Configurationクラスは、1つまたは複数のBeanメソッドを宣言することができ、運転春のコンテナで必要なときにBeanを定義し、サービス要求を生成してもよいです。たとえば、次のように:
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
//实例化,配置和返回对应bean
}
}
2、
@ConfigurationクラスはベースAnnotationConfigApplicationContext、またはWebベースAnnotationConfigWebApplicationContext変更機能を使用して導くことができます。たとえば、次の例:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean(MyBean.class);
//use myBean ...处理业务逻辑
ポリシーリマインダー:
AnnotationConfigApplicationContext使用して、アプリケーション・コンテキスト
サーブレットコンテナWeb設定環境をAnnotationConfigWebApplicationContext
図3に示すように、XMLファイルのSpring Beanを使用して
併用レジメンを@Configurationクラスとしてやり方をしてAnnotationConfigApplicationContext @Configurationを置き換えるも直接春定義されたXMLファイルで直接登録することができます。
<beans>
<context:annotation-config/>
<bean class="com.acme.AppConfig"/>
</beans>
在上述的例子中,
<context:annotation-config/>
是为了来开启ConfigurationClassPostProcessor
和其他注解相关操作来方便操作@Configuration类
図4に示すように、走査アセンブリ
@Configuration使用spring组件@Component进行元注解,
这样spring开启{ <context: Component -scan/>}扫描注解的时候,凡是应用注解@Configuration的类,
都可以被相当于正常的bean实例,通过@Autowired 或者@Inject注入。
如果只有一个构造函数,那么默认自动注入该构造函数,比如如下例子:默认SomeBean就自动注入了bean实例。因为只有这一个构造函数。
@Configuration
public class AppConfig {
private final SomeBean someBean;
public AppConfig(SomeBean someBean) {
this.someBean = someBean;
}
// @Bean definition using "SomeBean"
}
@Configurationクラスはスキャンアセンブリによって導かれ、構成要素は注釈がブートをスキャン使用して構成することができるだけでなく、使用することができ@ComponentScan
@Configuration
@ComponentScan("com.xiaochengxinyizhan.services")
public class AppConfig {
// 其他的 @Bean definitions ...
}
备注:com.xiaochengxinyizhan.services是AppConfig的包目路径目录
図5は、外部パラメータを処理し、APIを使用する環境をサポートしています。
org.springframework.core.env.Environment春のコアパッケージは、クラス@Configuration @Autowiredによってクラスに注入することができます。次の例は、例えば:
@Configuration
public class AppConfig {
@Autowired
Environment env;
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setName(env.getProperty("bean.name"));
return myBean;
}
}
{環境}属性を解析してソースオブジェクト{環境}に割り当て{@PropertySource}アノテーションストレージ属性用いて1つ以上の「プロパティソース」オブジェクト、{@設定}クラスに置かれ
、次のように例:
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Inject
Environment env;
@Bean
public MyBean myBean() {
return new MyBean(env.getProperty("bean.name"));
}
}
6、@valueアノテーション用いて
外部パラメータは@value @Configurationクラスアノテーションを使用して注入してもよいです。
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Value("${bean.name}")
String beanName;
@Bean
public MyBean myBean() {
return new MyBean(beanName);
}
}
この方法は、多くの場合、PropertySourcesPlaceholderConfigurerはによってXML構成ファイルから自動的に、春のPropertySourcesPlaceholderConfigurerクラスと組み合わせて使用されています
<context:property-placeholder/>
特定の使用@Bean変性得るために静的メソッドで取得または@Configurationクラスは@Bean BeanFactoryPostProcessorリターンの方法を参照することができます。
指定しない場合はもちろん、その後、春には、デフォルト値を指定し、独自のカスタムにそれを使用しない場合は、その後の使用PropertySourcesPlaceholderConfigurerの公式サイトを見てください。
@import 7、クラスで注釈@Configurationクラスを改善する
ような例を次のように
@Configuration
public class DatabaseConfig {
@Bean
public DataSource dataSource() {
//实例化,配置和返回DataSource;
}
}
@Configuration
@Import(DatabaseConfig.class)
public class AppConfig {
private final DatabaseConfig dataConfig;
public AppConfig(DatabaseConfig dataConfig) {
this.dataConfig = dataConfig;
}
@Bean
public MyBean myBean() {
// 引用 dataSource() bean 方法
return new MyBean(dataConfig.dataSource());
}
}
今{DatabaseConfig}の{}のAppConfigを導入して案内することができる
だけ登録{}のAppConfig Springコンテキストに
new AnnotationConfigApplicationContext(AppConfig.class);
8、注釈@Profileの使用は
@ConfigurationクラスはコンポーネントSpringコンテナとして使用されているので、我々は、次の例のように、クラスが使用される@Profile注釈環境を区別するために、この構成を使用することができます。
@Profile("development")
@Configuration
public class EmbeddedDatabaseConfig {
@Bean
public DataSource dataSource() {
// instantiate, configure and return embedded DataSource
}
}
@Profile("production")
@Configuration
public class ProductionDatabaseConfig {
@Bean
public DataSource dataSource() {
// instantiate, configure and return production DataSource
}
}
今、あなたはクラス宣言を設定することができ、その後、我々は(より多くのあなたが公式@Profileとorg.springframework.core.env.Environmentを見ることができます学ぶために)メソッドレベルの設定@Beanを宣言することができます
@Configuration
public class ProfileDatabaseConfig {
@Bean("dataSource")
@Profile("development")
public DataSource embeddedDatabase() { ... }
@Bean("dataSource")
@Profile("production")
public DataSource productionDatabase() { ... }
}
図9は、SpringXMLの@ImportResourceの導入を使用して文書注釈
前述のように、@のConfigurationクラスは、正式なSpringXMLのBean定義ファイルを宣言することができます。また、@ImportResource @Configurationコンフィギュレーションクラスのアノテーションによって導入することができます。ビーンは、注射に依存することができ、同じXMLファイルで定義されています。次の例は、例えば:
@Configuration
@ImportResource("classpath:/com/acme/database-config.xml")
public class AppConfig {
@Inject DataSource dataSource; // 在XML中定义的
@Bean
public MyBean myBean() {
// 注入xml中定义的bean
return new MyBean(this.dataSource);
}
}
10、@構成が入れ子にすることができ、構成クラス(無ドミナント利点は知られていない場合には直接に注釈することができ、不要な環境説明を低減する)
、次の例のように:
@Configuration
public class AppConfig {
@Inject DataSource dataSource;
@Bean
public MyBean myBean() {
return new MyBean(dataSource);
}
@Configuration
static class DatabaseConfig {
@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder().build();
}
}
}
11、コンフィギュレーション遅延初期化の
デフォルトでは、コンテナのビーンブーツの初期化方法@、起きてからこれを避けるために、@の設定アノテーションは遅延ロードにその初期化を使用して@Lazy注釈を宣言することができます。@Lazyコースまたのみ別の方法で使用され@Bean
12、@テスト支援の構成タイプ
スプリングテストモジュールのバネコンテナコンテキストフレームワークをテストは、注釈に許容される@ContextConfiguration @Configurationクラスクラスオブジェクトクラスを提供して配置
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppConfig.class, DatabaseConfig.class})
public class MyTests {
@Autowired
MyBean myBean;
@Autowired
DataSource dataSource;
@Test
public void test() {
// assertions against myBean ...
}
}
図13に示すように、特性スプリング開口@Enable構築アノテーションを使用してコンフィギュレーションクラス@Configurationに
、非同期メソッド、タイミングタスクの実行、駆動注釈イベントとしてばね特性をも春MVCを構成して起動することができます。
org.springframework.scheduling.annotation.EnableAsync @EnableAsync
org.springframework.scheduling.annotation.EnableScheduling @EnableScheduling
org.springframework.transaction.annotation.EnableTransactionManagement @EnableTransactionManagement
org.springframework.context.annotation.EnableAspectJAutoProxy @EnableAspectJAutoProxy
org.springframework.web.servlet.config.annotation.EnableWebMvc @EnableWebMvc
14、コンフィギュレーションの設定クラス@条項
- クラス(ファクトリメソッドが戻るのではない例として)構成されている必要がありサブクラスによって生成されたときに拡張操作を可能にする、カテゴリとして提供されます。
- proxyBeanMethods以外サブクラスは、()許容サブクラスを生成するために、falseに設定した場合の動作を可能にするように修飾された非最終的なクラス構成でなければなりません。
- Configurationクラスは、(メソッドで宣言されていない)非ローカルでなければなりません
- 彼らはコンフィギュレーションノートを確認することができない場合@Bean方法は、構成クラスを作成することはできません、ビーンの任意のインスタンスは、正常として扱われます。
ソースは以下のとおりです。
@Target(ElementType.TYPE)
//元素类型
@Retention(RetentionPolicy.RUNTIME)
//运行期间使用
@Documented
//文档化生成
@Component
//作为spring容器的组件可以被注入
public @interface Configuration {
//如果不指定名字,则自动生成默认的配置类文件名称,要求是与@Configuration配套使用
@AliasFor(annotation = Component.class)
String value() default "";
//从5.2开始,指定是否方法应该被大力为了强制bean生命周期行为例如有时候返回共享的单例bean实例再使用@Bean方法调用的时候,
这个特性需要方法拦截,通过运行时生成的CGLIB 子类实现,该类带有一些限制,
比如配置类及其方法不允许声明{final}。
默认值是{true},允许在配置类内的“bean间引用”,以及对该配置的 {@Bean}方法的外部调用,例如来自
另一个配置类。如果不需要,因为这个特定配置的每个{@Bean}方法都是自包含的,
并且被设计为容器使用的普通工厂方法,将这个标志切换到{false},以避免CGLIB子类处理。
关闭bean方法拦截有效地单独处理{ @Bean}方法,
就像在非{@Configuration}类上声明时一样,“@Bean Lite模式”(参考{@Bean的javadoc})。
因此,它在行为上等同于删除{@Configuration}原型。
boolean proxyBeanMethods() default true;
}