@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;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
this.message = message;

}

   public void getMessage(){
System.out.println("Your Message : " + message);

} }

 

Following is the content of the MainApp.java file:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
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();
}

}

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(){
spellChecker.checkSpelling();

} }

Following is the content of another dependent class file SpellChecker.java:

package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );

}

   public void checkSpelling(){
System.out.println("Inside checkSpelling." );

} }

Following is the content of the MainApp.java file:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
      ApplicationContext ctx =
new AnnotationConfigApplicationContext(TextEditorConfig.class);
      TextEditor te = ctx.getBean(TextEditor.class);
te.spellCheck();

} }

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.
Inside checkSpelling.

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 {
@Bean
 public A a() {
return new A();

} }

You can import above Bean declaration in another Bean Declaration as follows:

@Configuration
@Import(ConfigA.class)
public class ConfigB {

@Bean

   public B a() {
return new 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的更多相关文章

  1. [转] Spring - Java Based Configuration

    PS: Spring boot注解,Configuration是生成一个config对象,@Bean指定对应的函数返回的是Bean对象,相当于XML定义,ConfigurationProperties ...

  2. [转载]Spring Annotation Based Configuration

    Annotation injection is performed before XML injection, thus the latter configuration will override ...

  3. 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 ...

  4. spring Annotation based configuration

    spring 注解相关 https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s11.html

  5. 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案

    JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...

  6. 你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode

    Full @Configuration和lite @Bean mode 是 Spring Java Config 中两个非常有意思的概念. 先来看一下官方文档关于这两者的相关内容: The @Bean ...

  7. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  8. Spring 4 Ehcache Configuration Example with @Cacheable Annotation

    http://www.concretepage.com/spring-4/spring-4-ehcache-configuration-example-with-cacheable-annotatio ...

  9. spring java config 初探

    Java Config 注解 spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中 大量使用,今天我们来看下用到的主要注解有 ...

随机推荐

  1. Install GDAL in OpenSUSE 12.3 Linux

    Runtime Enviroment:Open SUSE Linux *i385 Notice:if any command disavliable ,you can copy the paramet ...

  2. server2008R2服务器开启远程

    通常我们开启远程桌面只需要在我的电脑那边把远程的打开就可以了,然后允许访问 上面说的这些其实晚上很多都有的,自己找一下,这边是自己遇到的一点问题,就是弄了很久,按网上说的弄很久,发现问题原来是在,路由 ...

  3. LINQ to XML(1)

    LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...

  4. silverlight 文本框只能输入汉字

    private void txtName_KeyDown(object sender, KeyEventArgs e) { Regex rg = new Regex("^[\u4e00-\u ...

  5. $(document).height()、$("body").height()、$(window).height()区别和联系

    前言:在此以高度为示例,宽度问题可类推.在移动端开发中,经常遇到需要把一块内容定位于底部的情况,当页面内容不满一屏时,需要设为fixed,而超过 一屏时,需要设为static随页面顶到底部,此时就需要 ...

  6. 一款jQuery立体感动态下拉导航菜单特效

    一款jQuery立体感动态下拉导航菜单特效,鼠标经过,在菜单栏上方下拉出一个背景图片,效果十分不错的一款jquery特效. 对IE6都是兼容的,希望大家好好研究研究. 适用浏览器:IE6.IE7.IE ...

  7. 简单的下拉刷新以及优化--SwipeRefreshLayout

    代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...

  8. robots.txt用法

    主要作用是告诉蜘蛛爬虫该网站下哪些内容能抓取,哪些内容不能抓取.虽然可以没有robots.txt这个文件,默认就抓取该网站的所有文件,对搜索引擎爬虫没有任何的影响,但是如果你想控制蜘蛛的检索间隔,你就 ...

  9. python autopy

    今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...

  10. oracle中的记录类型

    单词RECORD有“记录”的意思,因此RECORD也称为“记录类型”,使用该类型的变量可以存储由多个列值组成的一行数据. 在声明记录类型变量之前,首先需要定义记录类型,然后才可以声明记录类型的变量. ...