@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 ...
随机推荐
- Docker 容器简介与部署
关于Docker容器技术 参考文献:<docker 从入门到精通> Docker容器简介 Docker的构想是要实现 "Build,Ship and Run Any App,An ...
- Leetcode简单题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- selenium自动化测试工具模拟登陆爬取当当网top500畅销书单
selenium自动化测试工具可谓是爬虫的利器,基本动态加载的网页都能抓取,当然随着大型网站的更新,也出现针对selenium的反爬,有些网站可以识别你是否用的是selenium访问,然后对你加以限制 ...
- 02 Linux常用基本命令(二)
1.Linux的文件系统格式 1.以 / 为根目录,成树状结构分布 2.查看根目录下有什么 ls / 3./下有超级用户root的家目录(root),还有普通用户的家目录(/home) 4.常用文件夹 ...
- wampserver You don't have permission to access / on this server. 解决方法
最近在安装最近版wampserver 2.2 d时发现安装好后启动服务器,访问localhost显示You don't have permission to access / on this serv ...
- shell脚本if判断语句报错[: too many arguments的两种原因
shell脚本,if判断语句报错[: too many arguments 我遇到过两种情况: 1.第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断 ...
- 月薪 30K Java 程序员,需要掌握哪些技术?
转载自:Java3y 1-5年的Java程序员,薪资区间大致是在15-25K左右,那有没有可能提前达到30K的薪资呢?有人说这只能是大企业或者互联网企业工程师才能拿到.也许是的,小公司或者非互联网企业 ...
- 单节点oracle、ASM 详细安装步骤
目录 1.安装环境 2.系统要求 2.1 Linux安装Oracle系统要求 1.查看RAM和交换空间以及磁盘大小 2.检查所需软件包 3.配置host和主机名 2.2修改操作系统核心参数 1.创建相 ...
- svn 权限设置
/***********************************************************/ //SVNSubversion 用户权限管理 //资料来源:网络.总结 // ...
- Python数据驱动DDT的应用
在开始之前,我们先来明确一下什么是数据驱动,在百度百科中数据驱动的解释是:数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行 ...