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

@Configuration 和 @Bean 注解

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

 package org.wzh.di.demo1.congiration;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
 package org.wzh.di.demo1.congiration;

 public class HelloWorld {

     private String message;

     public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} }
 package org.wzh.di.demo1.congiration;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
String msg = helloWorld.getMessage();
System.out.println(msg);
} }

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

 package org.wzh.di.demo1.congiration;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class Test2 {

     public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(HelloWorldConfig.class);
ctx.refresh();
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World 2!");
String msg = helloWorld.getMessage();
System.out.println(msg);
} }

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. 解决:hudson.plugins.git.GitException: Could not init

    解决:

  2. ASP.NET Core搭建多层网站架构【10-使用JWT进行授权验证】

    2020/01/31, ASP.NET Core 3.1, VS2019, Microsoft.AspNetCore.Authentication.JwtBearer 3.1.1 摘要:基于ASP.N ...

  3. next_permutation的使用-Hdu1027

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  4. 「JSOI2012」玄武密码

    「JSOI2012」玄武密码 传送门 题目是要求多个串在母串上的最长匹配长度. 考虑 \(\text{AC}\) 自动机,我们建出 \(\text{Trie}\) 图然后用母串来在上面跑. 每一个能匹 ...

  5. centos 6.5 防火墙通过 80 和 3306 端口

    vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPU ...

  6. SQL 查询每组的第一条记录

    CREATE TABLE [dbo].[test1]( [program_id] [int] NULL, [person_id] [int] NULL ) ON [PRIMARY] /*查询每组分组中 ...

  7. Codeforces Round #589 (Div. 2)D(思维,构造)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;vector<int>adj[10 ...

  8. httpclient使用-get-post-传参

    转自:https://www.jianshu.com/p/375be5929bed 一.HttpClient使用详解与实战一:普通的GET和POST请求 简介 HttpClient是Apache Ja ...

  9. nyoj 24

    素数距离问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度 ...

  10. jemter-plugins-maven dependency -WIiki用法配置介绍

    1.先介绍下jmeter 的maven中央仓库地址,有兴趣自己看下 https://mvnrepository.com/artifact/org.apache.jmeter 2.Wiki github ...