博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml配置文件的形式 VS 配置类的形式
阅读量:2162 次
发布时间:2019-05-01

本文共 598 字,大约阅读时间需要 1 分钟。

基于xml的形式定义Bean的信息

去容器中读取Bean

public static void main( String[] args ){ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");System.out.println(ctx.getBean("person"));}

基于读取配置类的形式定义Bean信息

@Configurationpublic class MainConfig {	@Bean	public Person person(){		return new Person();	}}

注意: 通过@Bean的形式是使用的话, bean的默认名称是方法名,若@Bean(value="bean的名称")那么bean的名称是指定的

去容器中读取Bean的信息(传入配置类)

public static void main( String[] args ){	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);	System.out.println(ctx.getBean("person"));}

 

转载地址:http://mzfzb.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>