@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. The-ith-Element

    Brief: the-ith-element,given a array A with n element , return the i-th element of A.  A(n,i) this p ...

  2. ServletContext的用途

    安装在一个服务器中的一个特定URL名字空间(比如,/myapplication)下的所有Servlet,JSP,JavaBean等Web部件的集合构成了一个Web的应用,每一个Web应用(同一JVM) ...

  3. 《DNS的正向反向解析》RHEL6

    DNS的正向解析: Iptables –F Setenforce 0 安装DNS服务器的软件包: 启动DNS服务器: 修改DNS的配置文件:vim /etc/named.conf 修改DNS的配置:( ...

  4. Linux磁盘与文件系统概念理解

    磁盘级别概念     这里讲的主要是网上所谓的老式磁盘,它是由一个个盘片组成的,我们先从个盘片结构讲起.如图1所示,图中的一圈圈灰色同心圆为一条条磁道,从圆心向外画直线,可以将磁道划分为若干个弧段,每 ...

  5. 设置datagridview中button按钮的背景颜色

    问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...

  6. 关于LINQ一个简单例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  7. Use Sandcastle Help File Builder to generate help document

    http://shfb.codeplex.com/ Note: If the the help file contains the text "[Missing <param> ...

  8. javac。java版本切换

    如果安装有多个Java版本时(有时候有些软件自行安装),怎样方便的进行切换呢.除了常见的设置环境变量外,今天学到了一种新的切换方法: update-alternatives --config java ...

  9. Legacy安装win7和Ubuntu14.04双系统

    Legacy安装win7和Ubuntu14.04双系统 安装环境 Legacy启动模式(传统引导) 笔记本已安装win7 硬盘启动顺序为: U盘 硬盘 光驱 安装方法 制作U盘启动盘 在Ubuntu官 ...

  10. 高级php面试题及部分答案

    在网上看到一些高级php 的面试题目.. 闲来无事,搞了一些答案...可能不是很全面,留这以后备用吧. 一. 基本知识点1.1 HTTP协议中几个状态码的含义:503 500 401 403 404 ...