[转载]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中 大量使用,今天我们来看下用到的主要注解有 ...
随机推荐
- Integer类型值相等或不等分析
看到博客园一位博友写的面试问题,其中一题是 Integer a = 1; Integer b = 1 ; (a == b)?true :false; 当时我一看,这不是明显的true 嘛, 看到评论 ...
- 洛谷 P3374 【模板】树状数组 1
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...
- C++ 四则运算简单设计
如果说要用C++写一个简单的四则运算的程序,相信难不到人,这还不简单吗?然后用不了五分钟,三下五除二,就出了下面的代码,一调试,没问题..... #include <iostream> u ...
- iOS开发零基础教程之生成git所需的SSH keys
在我们github看到了一个不错的第三方库时,可能我们想把他git clone到本地,我们需要复制他的SSH URL,如下图: 复制完地址之后,我们需要打开终端,然后输入命令: git clone + ...
- IE8浏览器跨域接口访问异常的解决办法
IE8版本以下浏览器绝对是一个神奇的存在,忙碌好久,万事具备,居然在ajax调用接口的时候直接爆炸 陈述一下问题 首先是有这样一个接口,请求类型POST,入参JSON,出参JSON,jQuery aj ...
- Materialized Views 物化视图 -基础篇
Materialized Views 物化视图 -基础篇 http://blog.csdn.net/elimago/article/details/5404019
- SOAP 介绍
简介 SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议.此协议规范由 IBM.Microso ...
- iOS 点击return或者点击屏幕键盘消失
//定义两个文本框 UITextField *textName; UITextField *textSummary; //点击return 按钮 去掉 -(BOOL)textFieldShouldRe ...
- php新手:XAMMP打开开源php代码
1.启动XAMPP 打开XAMPP启动 Apache 和 MySql 如果发现默认的80端口被IIS占用了 请参考 这个 如何改变apache被占用的端口 2.将源代码复制到 磁盘(XAMPP安装目 ...
- hdu 2837 Calculation 指数循环节套路题
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...