1、@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)

package com.test.spring.support.configuration;

@Configuration
public class TestConfiguration {
public TestConfiguration(){
System.out.println("spring容器启动初始化。。。");
}
}

相当于:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> </beans>

主方法进行测试:

package com.test.spring.support.configuration;

public class TestMain {
public static void main(String[] args) { //@Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); //如果加载spring-context.xml文件:
//ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
}
}

从运行主方法结果可以看出,spring容器已经启动了:

八月 11, 2016 12:04:11 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@203e25d3: startup date [Thu Aug 11 12:04:11 CST 2016]; root of context hierarchy
spring容器启动初始化。。。

2、@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>,作用为:注册bean对象

bean类:

package com.test.spring.support.configuration;

public class TestBean {

    public void sayHello(){
System.out.println("TestBean sayHello...");
} public String toString(){
return "username:"+this.username+",url:"+this.url+",password:"+this.password;
} public void start(){
System.out.println("TestBean 初始化。。。");
} public void cleanUp(){
System.out.println("TestBean 销毁。。。");
}
}

配置类:

package com.test.spring.support.configuration;

@Configuration
public class TestConfiguration {
public TestConfiguration(){
System.out.println("spring容器启动初始化。。。");
} //@Bean注解注册bean,同时可以指定初始化和销毁方法
//@Bean(name="testNean",initMethod="start",destroyMethod="cleanUp")
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}

主方法测试类:

package com.test.spring.support.configuration;

public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
//获取bean
TestBean tb = context.getBean("testBean");
tb.sayHello();
}
}

注: 
(1)、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同;

(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域;

(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。

bean类:

package com.test.spring.support.configuration;

//添加注册bean的注解
@Component
public class TestBean { public void sayHello(){
System.out.println("TestBean sayHello...");
} public String toString(){
return "username:"+this.username+",url:"+this.url+",password:"+this.password;
}
}

配置类:

  • @Configuration
    
    //添加自动扫描注解,basePackages为TestBean包路径
    @ComponentScan(basePackages = "com.test.spring.support.configuration")
    public class TestConfiguration {
    public TestConfiguration(){
    System.out.println("spring容器启动初始化。。。");
    } //取消@Bean注解注册bean的方式
    //@Bean
    //@Scope("prototype")
    //public TestBean testBean() {
    // return new TestBean();
    //}
    }

主方法测试获取bean对象:

public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
//获取bean
TestBean tb = context.getBean("testBean");
tb.sayHello();
}
}

sayHello()方法都被正常调用。

(原文地址:http://www.cnblogs.com/ilinuxer/p/6503161.html)

[转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域的更多相关文章

  1. Spring和SpringMVC的常用注解

    Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...

  2. Spring案例1出纯注解开机

    配置QueryRunner对象:注解说明 package cn.mepu.config; import org.apache.commons.dbutils.QueryRunner; import o ...

  3. Spring中Configuration的理解

    基本用途从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigAp ...

  4. Spring 中初始化一个Bean对象时依赖其他Bean对象空指针异常

    1. Bean依赖关系 一个配置类的Bean,一个实例Bean: 实例Bean初始化时需要依赖配置类的Bean: 1.1 配置类Bean @ConfigurationProperties(prefix ...

  5. Spring -- Bean自己主动装配&amp;Bean之间关系&amp;Bean的作用域

    对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 ...

  6. 五 Spring的配置:Bean的配置,生命周期和作用范围

    Bean相关的配置: <bean>标签的id和name的配置: id:使用了约束中的唯一约束,里面不能出现特殊字符 name:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现 ...

  7. Spring框架学习笔记(3)——配置bean

    1.属性注入 (1)根据setter方法属性注入,这里使用的是property标签.需要bean属性提供对应的setter方法,比如笔记(1)里的 HelloWorld使用的就是这种方法. <! ...

  8. Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

    基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...

  9. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  10. Spring中的@Bean注解、@Configuration注解、@Value

    1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...

随机推荐

  1. Asp.Net 之 禁用TextBox的记忆功能

    IE提供了一个自动完成功能可以记忆我们的输入内容(如登录帐号等),方便下一次快速地录入类似资料.这确实是一个非常友好的功能,在操作时只需用鼠标双击文本框或输入前几个字符,系统会自动列出以前的录入历史供 ...

  2. Centos7中安装Python3

    环境:CentOS 7 1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...

  3. top高级技能

    默认输入top命令后进入的是交互模式,默认显示的TOP程序界面如下图所示: 我们注意到进程的内存区域数据很长一串,主要是因为这里用的单位是k表示的,我们可以修改显示的单位,在界面上直接输入E可以改变显 ...

  4. Maven让资源文件处理插件能够解析资源文件中的Maven属性

    <build> <resources> <resource> <directory>${project.basedir}/src/main/resour ...

  5. Android网络编程(一)HTTP协议原理

    相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley使用方法全解析 A ...

  6. 基于SQL和PYTHON的数据库数据查询select语句

    #xiaodeng#python3#基于SQL和PYTHON的数据库数据查询语句import pymysql #1.基本用法cur.execute("select * from biao&q ...

  7. python str方法之ljust、rjust、center

    # -*- coding: cp936 -*- #python 27 #xiaodeng #str方法之ljust.rjust.center #http://www.runoob.com/python ...

  8. java 属性封装

    //封装性 //封装概念: //1.是面向对象思想的三大特征之一 //2.封装细节是隐藏的,仅对外提供访问接口 //3.封装分类: //属性封装 //方法封装 //类的封装 //组件的封装 //模块化 ...

  9. Oracle Tuxedo工作站客户端与服务端的样例程序

    服务端代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cty ...

  10. (原)luarocks install 提示 failed fetching manifest

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6400169.html 参考网址: https://github.com/torch/torch7/is ...