目录
使用@configuration注释类表示,spring ioc容器可以将该类用作bean定义的源。@bean注释告诉spring,用@bean注释的方法将返回一个应该在spring应用程序上下文中注册为bean的对象。最简单的@configuration类如下所示:
package com.tutorialspoint; import org.springframework.context.annotation.*; @configuration public class helloworldconfig { @bean public helloworld helloworld(){ return new helloworld(); } }
它和以下的xml方式定义的是等价的:
<beans> <bean id = "helloworld" class = "com.tutorialspoint.helloworld" /> </beans>
带@bean的方法名作为bean id注释,他创建并返回实际的bean。一个配置类可以拥有多个bean的声明。一旦定义了配置类,你可以通过 annotationconfigapplicationcontex
加载并获取
他们。
public static void main(string[] args) { applicationcontext ctx = new annotationconfigapplicationcontext(helloworldconfig.class); helloworld helloworld = ctx.getbean(helloworld.class); helloworld.setmessage("hello world!"); helloworld.getmessage(); }
也可以获取加载不同的configuration
public static void main(string[] args) { annotationconfigapplicationcontext ctx = new annotationconfigapplicationcontext(); ctx.register(appconfig.class, otherconfig.class); ctx.register(additionalconfig.class); ctx.refresh(); myservice myservice = ctx.getbean(myservice.class); myservice.dostuff(); }
helloworldconfig.java
@configuration public class helloworldconfig { @bean public helloworld helloworld(){ return new helloworld(); } }
helloworld.java
public class helloworld { private string message; public void setmessage(string message){ this.message = message; } public void getmessage(){ system.out.println("your message : " + message); } }
mainapp.java
public class mainapp { public static void main(string[] args) { applicationcontext ctx = new annotationconfigapplicationcontext(helloworldconfig.class); helloworld helloworld = ctx.getbean(helloworld.class); helloworld.setmessage("hello world!"); helloworld.getmessage(); } }
输出:
your message : hello world!
当@ bean相互依赖时,表示依赖关系就像让一个bean方法调用另一个bean一样简单,如下所示
@configuration public class appconfig { @bean public foo foo() { return new foo(bar()); } @bean public bar bar() { return new bar(); } }
foo bean通过构造函数注入接收到bar的引用
texteditorconfig.java
@configuration public class texteditorconfig { @bean public texteditor texteditor(){ return new texteditor( spellchecker() ); } @bean public spellchecker spellchecker(){ return new spellchecker( ); } }
texteditor.java
public class texteditor { private spellchecker spellchecker; public texteditor(spellchecker spellchecker){ system.out.println("inside texteditor constructor." ); this.spellchecker = spellchecker; } public void spellcheck(){ spellchecker.checkspelling(); } }
spellchecker.java
public class spellchecker { public spellchecker(){ system.out.println("inside spellchecker constructor." ); } public void checkspelling(){ system.out.println("inside checkspelling." ); } }
mainapp.java
public class mainapp { public static void main(string[] args) { applicationcontext ctx = new annotationconfigapplicationcontext(texteditorconfig.class); texteditor te = ctx.getbean(texteditor.class); te.spellcheck(); } }
输出:
inside spellchecker constructor. inside texteditor constructor. inside checkspelling.
@import
注解允许在一个configuration中导入另外一个配置类。
@configuration public class configa { @bean public a a() { return new a(); } }
@configuration @import(configa.class) public class configb { @bean public b a() { return new a(); } }
这样,只需要加载configb,则可以加载a,b两个配置文件,而不需要一样加载两次.
@bean注释支持指定任意的初始化和销毁回调方法,就像spring xml的init方法和销毁方法。
public class foo { public void init() { // initialization logic } public void cleanup() { // destruction logic } } @configuration public class appconfig { @bean(initmethod = "init", destroymethod = "cleanup" ) public foo foo() { return new foo(); } }
默认作用域是singleton,可以通过以下方法重写:
@configuration public class appconfig { @bean @scope("prototype") public foo foo() { return new foo(); } }
如对本文有疑问, 点击进行留言回复!!
Android 4.0使用Kotlin调用C语言以及汇编语言
Java Class.forName()用法和newInstance()方法原理解析
网友评论