@Configuration与@Bean
- @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的更多相关文章
- @Configuration 和 @Bean
1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...
- Springboot@Configuration和@Bean详解
Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- @Configuration与@Bean作用
Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...
- Spring @Configuration 和 @Bean 注解
@Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...
- @Configuration和@Bean的用法和理解
spring Boot提倡约定优于配置,如何将类的生命周期交给spring 1.第一种自己写的类,Controller,Service. 用@controller @service即可 2.第二种,集 ...
- @Configuration和@Bean
@Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...
- 0006SpringBoot中@Configuration与@Bean联合使用
需求:将某个普通类做为组件注册到容器中,可通过如下办法 1.定义HelloService类 package springboot_test.springboot_test.service; publi ...
- @Configuration结合@Bean实现对象的配置
@Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...
随机推荐
- js、jQuery各种高度
height.js ```$(document).height(); //整个网页的高度 $(window).height(); //浏览器可视窗口的高度 $(window).scrollTop(); ...
- Eclipce远程调试
1.注意: root权限启动的进程不支持远程调试,也有很多隐患,支持最高权限,应用普通用户启动,又原先安装Tomcat是使用的root权限,普通用户对root安装的软件没有执行权限,可以修改相应权限, ...
- CF407D Largest Submatrix 3
cf luogu 被自己菜到自闭了/kk 既然是子矩阵,那么惯用套路为枚举矩阵上下边界,然后\(O(n)\)扫描求解.这题里要从左往右枚举右端点,然后看左端点最多能放到哪,那就对于每个数求出在上下边界 ...
- Flask与微信小程序登录(后端)
开发微信小程序时,接入小程序的授权登录可以快速实现用户注册登录的步骤,是快速建立用户体系的重要一步.这篇文章将介绍 python + flask + 微信小程序实现用户快速注册登录方案(本文主要进行后 ...
- 欧拉系统-登陆 SSH 出现 Access Denied 错误
1./home 权限问题如果 /home 只支持 root 访问,那么不妨试一下 /tmp ,然后用 mv 命令再转移 2./etc/ssh/sshd_config 配置问题 vi /etc ...
- 人脸识别之Python DLib库进行人脸关键点识别
一.首先安装DLib模块 这里只介绍linux安装的过程,windows安装过程请自行百度 1.首先,安装dlib.skimage前:先安装libboost sudo apt-get install ...
- JS异步上传文件
直接调用Upload(option)方法,即可上传文件,不需要额外的插件辅助,采用原生js编写. /* *异步上传文件 *option参数 **url:上传路径 **data:上传的其他数据{id:& ...
- 记录一次维护weblogic集群的问题
[问题描述] weblogic 集群子服务节点启动,启动完毕后状态改为 ADMIN,正常情况是 RUNNING 在启动脚本添加如下配置即可 -DDomainRegistrationEnabled=tr ...
- nginx_rtmp
rtmp { server { listen ; chunk_size ; max_connections ; #音视频流上传和播放地址都是 rtmp://你的IP/live/streamName # ...
- QT 给工程添加图片
先打开如图的打开方式 然后我们看到以下的画面,选择下面的 然后我们选择如下:,这里我们要注意我们的图片资源有一定要和QRC资源在同一个文件夹中 之后我们通过在stylesheet里面设置来使用我们添加 ...