1,@Configuration与@Bean
 
@Configuration:
告诉Spring这是一个配置类,配置类==配置文件。

  • @Configuration==beans.xml

@Bean:

  • 给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id。
  • @Bean 等价于 <bean></bean>
  • 可以给@Bean设置value来修改id,比如@Bean("personAlias")。
  • 可以在@bean中指定初始化和销毁方法
@Bean(value = "beanLife",initMethod = "init", destroyMethod = "destroy")

等价于

<bean id="person" class="com.java.model.Person"  init-method="init" destroy-method="destroy"></bean>

2,配置对比与验证

通常的xml配置方式如下,beans.xml

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- use-default-filters若想自定义过滤器生效,则必须将该值设为false-->
<!--<context:component-scan base-package="com.java" ></context:component-scan>-->
<!--注入bean-->
<beans>
<bean id="person" class="com.java.model.Person">
<property name="id" value="11111"></property>
<property name="name" value="vhsj"></property>
</bean>
</beans>
</beans>

采用@Configuration注入方式如下

@Configuration

public class BeanConfig {

    //@Bean

    @Bean("personAlias")

    public Person person2(){

        return new Person("2222","lisi");

    }

}

测试代码:

public class BeanDependencyInjectionTest {

    public static void main(String[] args) {

        System.out.println("-----------------------------xml 方式注入-----------------------------------");

        //xml 方式注入

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        Person person = (Person) applicationContext.getBean("person");

        System.out.println("person---->"+person);

        //获取所有注入Bean的名称

        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();

        for (String name : beanDefinitionNames) {

            System.out.println(name);

        }

        System.out.println("-----------------------------annotation 注解方式注入-----------------------------------");

        //annotation 注解方式注入

        ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(BeanConfig.class);

        Person person1 = applicationContext1.getBean(Person.class);

        System.out.println("person1---->"+person1);

        //获取所有注入Bean的名称

        String[] beanDefinitionNames1 = applicationContext1.getBeanDefinitionNames();

        for (String name : beanDefinitionNames1) {

            System.out.println(name);

        }

    }

}

测试结果:

-----------------------------xml 方式注入-----------------------------------

person---->Person{id='11111', name='vhsj'}

person

-----------------------------annotation 注解方式注入-----------------------------------

person1---->Person{id='2222', name='lisi'}

org.springframework.context.annotation.internalConfigurationAnnotationProcessor

org.springframework.context.annotation.internalAutowiredAnnotationProcessor

org.springframework.context.annotation.internalCommonAnnotationProcessor

org.springframework.context.event.internalEventListenerProcessor

org.springframework.context.event.internalEventListenerFactory

beanConfig

personAlias
 

@Configuration与@Bean的更多相关文章

  1. @Configuration 和 @Bean

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

  2. Springboot@Configuration和@Bean详解

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

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

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

  4. @Configuration与@Bean作用

    Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...

  5. Spring @Configuration 和 @Bean 注解

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

  6. @Configuration和@Bean的用法和理解

    spring Boot提倡约定优于配置,如何将类的生命周期交给spring 1.第一种自己写的类,Controller,Service. 用@controller @service即可 2.第二种,集 ...

  7. @Configuration和@Bean

    @Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...

  8. 0006SpringBoot中@Configuration与@Bean联合使用

    需求:将某个普通类做为组件注册到容器中,可通过如下办法 1.定义HelloService类 package springboot_test.springboot_test.service; publi ...

  9. @Configuration结合@Bean实现对象的配置

    @Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...

随机推荐

  1. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

    DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...

  2. CentOS7部署Tomcat服务器

    1. 软件 存放路径:/usr/local/src apache-tomcat-9.0.22.tar.gz openjdk-12_linux-x64_bin.tar.gz 2.事先配置 启动后关闭防火 ...

  3. springboot配置文件之yml的语法学习

    springboot配置文件(.yml/.yaml.properties) YAML(YAML Ain't Markup Language) YAML A Markup Language:是一个标记语 ...

  4. Vue报错:Property or method "XXX" is not defined on the instance but referenced during render. Make sure that this property is reactive...

    在Vue中定义方法或者属性时,因为粗心疏忽可以能会报该错误 [Vue warn]: Property or method "search" is not defined on th ...

  5. 多线程编程-- part5.1 互斥锁之公平锁-获取锁

    基本概念 1.AQS:AbstractQueuedSynchronizer类 AQS是java中管理“锁”的抽象类,锁的许多公共方法都是在这个类中实现.AQS是独占锁(例如,ReentrantLock ...

  6. vue + mixin混入对象使用

    vue提供的混入对象mixin,类似于一个公共的组件,其他任何组件都可以使用它.我更经常的是把它当成一个公共方法来使用 在项目中有些多次使用的data数据,method方法,或者自定义的vue指令都可 ...

  7. jquery做的滑动按钮开关

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 2.(基础)tornado的请求与响应

    之前我们介绍了tornado 的基础流程,但是还遗留了一些问题,今天我们就来解决这些遗留问题并学习新的内容 settings,使用tornado.web.Application(handler, ** ...

  9. 微服务架构:构建PHP微服务生态

    微服务架构:构建PHP微服务生态   Linux系统技术交流QQ群(1675603)验证问题答案:刘遄 导读 诞生于 2014 年的“微服务架构”,其思想经由 Martin Fowler 阐述后,在近 ...

  10. osworkflow 入门基础2

    [quote]前篇我引入了互联网上找来的一篇文章,接着上篇讲:osworkflow 工作流是非常轻量级的,虽然2006就停止活动了,互联网上的资料也不是很多,官网也没过多的说明,比起jbpm 和act ...