[转载]Spring Java Based Configuration
@Configuration & @Bean Annotations
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.
Here is the content of HelloWorldConfig.java file:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
} }
Here is the content of HelloWorld.java file: |
package com.tutorialspoint; private String message; public void setMessage(String message){ } public void getMessage(){ } } |
Following is the content of the MainApp.java file: |
package com.tutorialspoint; import org.springframework.context.annotation.*; public class MainApp { ApplicationContext ctx = HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Hello World!"); helloWorld.getMessage(); } |
Once you are done with creating all the source filesand adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:
Your Message : Hello World!
ge com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
} }
Here is the content of TextEditor.java file: package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
Injecting Bean Dependencies
When @Beans have dependencies on one another, expressing that dependency is as simple as having one bean method calling another as follow
Here is the content of TextEditorConfig.java file:
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
}
@Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
} }
Here is the content of TextEditor.java file: package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker; public void spellCheck(){ } } |
Following is the content of another dependent class file SpellChecker.java: |
package com.tutorialspoint; public class SpellChecker { System.out.println("Inside SpellChecker constructor." ); |
} public void checkSpelling(){ } } |
Following is the content of the MainApp.java file: |
package com.tutorialspoint; import org.springframework.context.annotation.*; public class MainApp { ApplicationContext ctx = TextEditor te = ctx.getBean(TextEditor.class); } } |
Once you are done with creating all the source files and adding required additional libraries, let us run the application. You should note that there is no configuration file required. If everything is fine with your application, this will print the following message:
Inside SpellChecker constructor. |
Inside TextEditor constructor. |
The @Import Annotation
The @Import annotation allows for loading @Bean definitions from another configuration class. Consider a ConfigA class as follows: |
@Configuration public class ConfigA { |
public A a() {
return new A();
} }
You can import above Bean declaration in another Bean Declaration as follows:
@Configuration @Bean public B a() { } |
} |
Now, rather than needing to specify both ConfigA.class and ConfigB.class when instantiating the context, only ConfigB needs to be supplied as follows:
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}
[转载]Spring Java Based Configuration的更多相关文章
- [转] Spring - Java Based Configuration
PS: Spring boot注解,Configuration是生成一个config对象,@Bean指定对应的函数返回的是Bean对象,相当于XML定义,ConfigurationProperties ...
- [转载]Spring Annotation Based Configuration
Annotation injection is performed before XML injection, thus the latter configuration will override ...
- Spring 3 Java Based Configuration with @Value
Springsource has released the Javaconfig Framework as a core component of Spring 3.0. There is a tre ...
- spring Annotation based configuration
spring 注解相关 https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s11.html
- 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案
JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...
- 你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode
Full @Configuration和lite @Bean mode 是 Spring Java Config 中两个非常有意思的概念. 先来看一下官方文档关于这两者的相关内容: The @Bean ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Spring 4 Ehcache Configuration Example with @Cacheable Annotation
http://www.concretepage.com/spring-4/spring-4-ehcache-configuration-example-with-cacheable-annotatio ...
- spring java config 初探
Java Config 注解 spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中 大量使用,今天我们来看下用到的主要注解有 ...
随机推荐
- CXF调用wsdl2java生成客户端异常
用cxf生成java客户端代码的时候出现异常: undefined element declaration 's:schema' 解决办法:1.删除 2.替换 参考资料: http:/ ...
- wiegand 问题
在向门控器发送信号的时候,播放声音和通过GPIO向wiegand发送信号的时候,由于wiegand的资源优先级别不够和声音的播放可能发生了冲突,有时向GPIO发送信号的时候,发送失败. static ...
- 这是html5中WebGL的演示
这是html5中WebGL的演示,让我们与他人分享爱您发送短消息.每次你进入它使用不同的位置,新的爱情点被添加到全球.让世界更明亮的地方与你的朋友分享! 源文件:部分代码:<!DOCTYPE h ...
- Hibernate中的对象状态,及自动更新原因
Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...
- window store app 附件读取
public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName) { ...
- 【Qt】命令行编译Qt程序(nmake)【转】
简述 前两节讲解了如何在Visual Studio和Qt Creator中搭建Qt开发环境,并分享了我们第一个小程序-Hello World. 下面分享如何使用命令行来编译Qt程序.当然,MSVC和M ...
- 一款非常炫酷的jQuery动态随机背景滚动特效
一款非常炫酷的jQuery动态随机背景滚动特效 图片背景会不停息的滚动,带有那种漂浮的视觉效果,小圈圈飘动. 更好的是还兼容IE6浏览器,大伙可以好好研究研究. 适用浏览器:IE6.IE7.IE8.3 ...
- ASP 连接 MySQL 数据库两种方法
一般都是用myodbc来连接.首先,在系统中安装 Mysql 的ODBC数据库驱动.如安装稳定版本是3.51.下载地址是:http://dev.mysql.com/downloads/connecto ...
- php新手:XAMMP打开开源php代码
1.启动XAMPP 打开XAMPP启动 Apache 和 MySql 如果发现默认的80端口被IIS占用了 请参考 这个 如何改变apache被占用的端口 2.将源代码复制到 磁盘(XAMPP安装目 ...
- 在 SQL Server 中的网络数据库文件的支持说明
其实就是一个学员问SQL Server 是否能存放的于NAS(UAC 的路径下). 官方的回答简略版本为:可以,需要满足一些强制性的硬件要求.但需要考虑一系列的性能的问题. http://suppor ...