@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项目中。

最新文章

  1. centos7 dokcer fastdfs
  2. BaseAction的一般写法
  3. php连接数据库
  4. React如何性能调优
  5. Oracle Patch Bundle Update
  6. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
  7. ImageView及其子类(三)
  8. 故障排查实战案例——某电器ERP系统日志暴增
  9. 【Unity编程】Unity中的欧拉旋转
  10. storage在IE8下的兼容性写法
  11. 验证插件——jquery.validate.js
  12. 基于event 实现的线程安全的优先队列(python实现)
  13. 请求ajax失败的原因(进入到error)
  14. Android Studio删除依赖
  15. 浅谈如何获取机器的memory和CPU信息
  16. git备忘 &amp; ProGit笔记
  17. Could not transfer artifact org.springframework
  18. Luogu 1603 - 斯诺登的密码 - [简单字符串操作]
  19. C/C++ -- Gui编程 -- Qt库的使用 -- 组件大杂烩
  20. 云备份厂商Rubrik再获2.61亿美元融资,估值高达33亿美元 转自中国存储网

热门文章

  1. C++ char float int string 之间的转换
  2. iOS 总结APP间跳转的常用以及非常用需求 APP跳转Safari APP跳转APP
  3. 金融即服务(FaaS),将开启场景化金融新格局
  4. Cloudera Manager安装之时间服务器和时间客户端(二)
  5. GIT使用—补丁与钩子
  6. 翻翻git之---可用作课程表/排班表的自定义table库ScrollTableView
  7. SaltStack应用grains和jinja模板-第四篇
  8. Elasticsearch之分词器的作用
  9. springMVC注解的入门案例
  10. IDEA.环境