@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);

}

最新文章

  1. gulp插件(gulp-jmbuild),用于WEB前端构建
  2. ubuntu中的Wine详解
  3. [原]Android开发环境搭建
  4. How to change a product dropdown attribute to a multiselect in Magento
  5. Codeforces Round #248 (Div. 1)——Ryouko's Memory Note
  6. 模仿jQuery的filter方法
  7. jquery_api事件(二)
  8. Spring学习---JPA配置和使用
  9. Android jni 编程4(对基本类型二维整型数组的操作)
  10. OV摄像头SCCB通信协议
  11. 简单爬虫 -- 以爬取NASA AOD数据(TIFF文件)为例
  12. CentOS7.4下的 JDK1.8 安装
  13. numpy数据去重
  14. S0.3 直方图
  15. 20165306 Exp4 恶意代码分析
  16. SpringBoot之静态资源放行
  17. 20165206 2017-2018-2《Java程序设计》课程总结
  18. 4依赖倒转原则DIP
  19. JRebel 代理激活
  20. tarjan 缩点(模板)

热门文章

  1. 杭电ACM2084--数塔
  2. 如何将Log4Net 日志保存到mongodb数据库之实践
  3. hadoop分布式安装过程
  4. 关于iOS6应用中第三方类库不支持armv7s的问题解决
  5. xv6实验环境搭建
  6. 1093. Count PAT's (25)
  7. ActiveMQ之selector的用法
  8. 用Python作GIS之一:介入STARS
  9. linux下bus,device,driver三者关系
  10. UIViewController没有随着设备一起旋转的原因