@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}

@Configuration底层是含有@Component ,所以@Configuration 具有和 @Component 的作用。

@Configuration可理解为用spring的时候xml里面的<beans>标签。

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

package com.dsx.demo;

import org.springframework.context.annotation.Configuration;

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

相当于:

<?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>

@Bean可理解为用spring的时候xml里面的<bean>标签。

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

package com.dsx.demo;

public class TestBean {

    private String username;
private String url;
private String password; 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.dsx.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; @Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration容器启动初始化。。。");
} // @Bean注解注册bean,同时可以指定初始化和销毁方法
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}

上述操作相当于实例化TestBean ,并交给spring管理。

注: 
(1)、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同; 
(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域; 
(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Repository等注解注册bean(在需要注册的类上加注解),当然需要配置@ComponentScan注解进行自动扫描。

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
@AliasFor("name")
String[] value() default {}; @AliasFor("value")
String[] name() default {}; Autowire autowire() default Autowire.NO; String initMethod() default ""; String destroyMethod() default "(inferred)";
}

使用@Bean注解时,可以配置initMethod()和destoryMethod方法,分别在实例化和销毁的时候执行。

或者使用通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 。

Spring Boot不是spring的加强版,所以@Configuration和@Bean同样可以用在普通的spring项目中。

springboot--常用注解--@configration、@Bean的更多相关文章

  1. SpringBoot 常用注解(持续更新)

    SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...

  2. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  3. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...

  4. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

  5. SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解

    SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...

  6. 菜鸟的springboot常用注解总结

    菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...

  7. SpringBoot(14)—注解装配Bean

    SpringBoot(14)-注解装配Bean SpringBoot装配Bean方式主要有两种 通过Java配置文件@Bean的方式定义Bean. 通过注解扫描的方式@Component/@Compo ...

  8. SpringBoot常用注解的介绍及使用 - 转载

    常用注解 @springBootApplication 系统启动类注解,此注解是个组合注解,包括了:@SpringBootConfiguration,@EnableAutoConfiguration, ...

  9. 结合参数接收响应转换原理讲解SpringBoot常用注解

    一.常用注解回顾 1.1 @RequestBody与@ResponseBody //注意并不要求@RequestBody与@ResponseBody成对使用. public @ResponseBody ...

  10. SSM和SpringBoot常用注解

    SpringBoot的重要注解 @SpringBootApplication 创建 SpringBoot 项目之后会默认在主类加上 我们可以把 @SpringBootApplication看作是 @C ...

随机推荐

  1. nginx rewrite规则last与break的区别

    概要:break和last都能阻止继续执行后面的rewrite指令,last如果在location下的话,对于重写后的URI会重新匹配location,而break不会重新匹配location. 区别 ...

  2. java中元注解

    java中元注解有四个: @Retention @Target @Document @Inherited:  @Retention:注解的保留位置 @Retention(RetentionPolicy ...

  3. linux 常用命令总结(三)

    1. setup       // 进入相应配置界面,按空格键选择相关功能 2. ll       // 列出当前目录下详细内容 :等价与ls -all 3. clear        // 清理当前 ...

  4. [CLR via C#读后整理]-1.CLR的执行模型

    公共语言运行时(Common Language Runtime,CLR)是一个可由多种编程语言使用的"运行时".他主要提供的功能有:程序集加载,内存管理,,安全性,异常处理,线程同 ...

  5. PHP SQL写法 积累(注:PHPSQL与LINQ SQL相似)

    1: $data ['parentid'] = $pid; M('menu')->where($data)->order(' id asc  ')-> select();   // ...

  6. CSS Border(边框)

    CSS Border(边框) 一.CSS 边框属性 CSS边框属性允许你指定一个元素边框的样式和颜色. 示例效果: 二.边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来 ...

  7. 20145216史婧瑶《Java程序设计》第10周学习总结

    20145216 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 一.网络概述 网络编程就是两个或多个设备(程序)之间的数据交换. 识别网络上的每个设备:①IP地址②域名 ...

  8. [LOJ6145]Easy

    题意给出一棵树,每次询问一个点$x$到编号在$[l,r]$中的点的距离的最小值.$n,q\le 10^5$ 大概是最简单的动态点分治了,注意开大数组即可,如果改成求最大值这道题会有意思很多 代码: # ...

  9. 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem

    2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...

  10. asp.net core开发注意事项

    1.类库的创建尽量选择.net standard. 如果选择.net core 则.net framework不能调用该类库, .net core和.net framework都可以调用.net st ...