@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. AR模式

    AR模式 在ThinkPHP框架中,一共存在两种操作模式:ORM模式与AR模式 ORM模式:① 实例化模型 ② 创建数据对象组装数组 ③ 调用相关方法执行相关操作 AR模式:① 实例化模型 ② 把数据 ...

  2. Codevs 2894 Txx考试

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description Txx是一个成绩很差的人,考试便成了他的噩梦.于是他常在考试时睡觉以打发时间.今天 ...

  3. Linux下OpenCV的环境搭建

    OpenCV is the most popular and advanced code library for Computer Vision related applications today, ...

  4. 关于CORS

    前几天碰到CORS问题,只要在“Access-Control-Allow-Origin”响应头中添加对应域名即可. 今天做一个上传文件的demo,利用XMLHttpRequest向服务器发送post请 ...

  5. python+selenium环境配置(windows7环境)

    下载python[python开发环境] http://python.org/getit/ 下载setuptools[python的基础包工具] http://pypi.python.org/pypi ...

  6. js设计模式(8)---享元模式

    0.前言 今天总结了四种设计模式,到现在有点精疲力尽了,但是还是有不少收获,很开心自己有掌握了新的东西,今天变得有了价值. 1.使用条件 1.1.网页中使用了大量资源密集型的对象: 1.2.这些对象中 ...

  7. Js操作Select大全(取值、设置选中)

    Js操作Select是很常见的,也是比较实用的. jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<s ...

  8. Android的Handler与Activity线程同步

    假设这里有同一个Runnable对象r. 可能采用的方法有: 第一种: handler.post(r); 实际上这种方法并没有调用线程someThread的start方法,而是直接调用了Runaabl ...

  9. C# 访问控制:public、private、protected和internal

    平日工作时最常用的访问控制符是public和private,当看到prism里面大量使用protected的时候,觉得还是不太理解为啥. 所以就静下心来查找并理解了一下,这里记录下,以便回顾和交流. ...

  10. 内部技术分享的 PPT

    本文的基础是搞了一次内部的技术分享,在此也分享一下本次的PPT的一些内容.先列一下大概内容吧. EF-Code First API(WCF.WebAPI) Xaml MVVM AOP Xamarin. ...