参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html

@Configuration 和 @Bean 注解

带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。最简单可行的 @Configuration 类如下所示:

  1. package org.wzh.di.demo1.congiration;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class HelloWorldConfig {
  8. @Bean
  9. public HelloWorld helloWorld() {
  10. return new HelloWorld();
  11. }
  12. }
  1. package org.wzh.di.demo1.congiration;
  2.  
  3. public class HelloWorld {
  4.  
  5. private String message;
  6.  
  7. public String getMessage() {
  8. return message;
  9. }
  10.  
  11. public void setMessage(String message) {
  12. this.message = message;
  13. }
  14.  
  15. }
  1. package org.wzh.di.demo1.congiration;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. public class Test {
  7.  
  8. public static void main(String[] args) {
  9. ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
  10. HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
  11. helloWorld.setMessage("Hello World!");
  12. String msg = helloWorld.getMessage();
  13. System.out.println(msg);
  14. }
  15.  
  16. }

也可以加载其他配置类,这里只写用法

  1. package org.wzh.di.demo1.congiration;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class Test2 {
  6.  
  7. public static void main(String[] args) {
  8. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  9. ctx.register(HelloWorldConfig.class);
  10. ctx.refresh();
  11. HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
  12. helloWorld.setMessage("Hello World 2!");
  13. String msg = helloWorld.getMessage();
  14. System.out.println(msg);
  15. }
  16.  
  17. }

Spring--@configuration 和 @Bean的更多相关文章

  1. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

  2. Spring的Java配置方式—@Configuration和@Bean实现Java配置

    Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...

  3. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  4. @Configuration 和 @Bean

    1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...

  5. Spring自动化装配bean

    1. 场景 用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入). 如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这 ...

  6. Spring高级装配bean

    目录 spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表达式语言 一.环境与profile 配置profile  bean 在软件开发的时候,有一个 ...

  7. Spring系列(二) Bean装配

    创建应用对象之间协作关系的行为称为装配(wiring), 这也是DI的本质. Spring中装配Bean的方式 Spring提供了三种装配Bean的方式. 隐式的Bean发现机制和自动装配 Java ...

  8. Spring Configuration注解使用

    @Configuration是spring.xml的注解版. @ComponentScan是<context:component-scan base-package="com.cosh ...

  9. Springboot@Configuration和@Bean详解

    Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  10. Spring标签之Bean @Scope

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

随机推荐

  1. python csv 读写操作

    import csv def read_csvList(path="./datasets/test.csv")->list: """return ...

  2. VBA 学习笔记 - 运算符

    学习资料:https://www.yiibai.com/vba/vba_operators.html 算术运算符 加减乘除模指,这个没啥特别的. 比较运算符 和 Lua 相比,判断相等变成了一个等于号 ...

  3. .NET中的字符串(1):字符串 - 特殊的引用类型

    C# string 特殊的引用类型 .Net 框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一个引用类型,也就是说线程上的堆栈上不会驻留有任何字符串.(译 ...

  4. Java 11 New Features

    前言 北京时间 2018年9 月 26 日,Oracle 官方宣布 Java 11 正式发布.这是 Java 大版本周期变化后的第一个长期支持版本,非常值得关注.从官网即可下载, 最新发布的 Java ...

  5. MQTT.js browser node 均支持

    npm - mqtt 官网手册 https://www.npmjs.com/package/mqtt#weapp 简书用户 使用笔记 https://www.jianshu.com/p/4fd95ca ...

  6. XC1263 签到题(哇 ,写得我怀疑人生啊!!!@!@)

    1263: 签到题 时间限制: 1 Sec  内存限制: 128 MB提交: 174  解决: 17 标签提交统计讨论版 题目描述 大家刚过完寒假,肯定还没有进入状态,特意出了一道签到题给各位dala ...

  7. 如何确认 fastboot unlock 解锁成功,如何确认DM-verity 已关闭

    如何确认 fastboot unlock 解锁成功 1.fastboot 模式下按音量上键后是否提示 Unlock Pass...return to fastboot in 3s 2.重启后界面是否显 ...

  8. iOS之Xcode提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package

    https://www.jianshu.com/p/6d465a0ea58e 这句英文翻译过来就是: 无法继续交付:现有的传输程序实例目前正在上载此包 原因:上传的动作被记录在UploadToken中 ...

  9. vue父孙组件传值($attr及$listeners)的使用

    父组件 <template> <div> <!-- 将值传给子组件 子组件可以获取自己想要的值 也可以忽视掉需要传给孙子组件的值 --> <!-- 同时获取通 ...

  10. 小程序使用scroll-view横向滑动时,flex布局失效问题

    最近在完善以前项目,类目增多,需要进行横向滑动 实现方法1 可以在外盒子scroll-view使用white-space: nowrap来禁止子盒子换行,子盒子使用display: inline-bl ...