xml配置方式

首先我们创建一个实体类Person

public class Person {
private String name;
private Integer age;
private String nickName;
// 省略getter and setter ,tostring ,Constructor
1}

以往,我们在spring的配置文件中注册一个bean,采用以下写法;

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="person1" class="com.atguigu.bean.Person" >
<property name="age" value="24"></property>
<property name="name" value="zhangsan"></property>
</bean> </beans>

写个测试类

public class MainTest {

	@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person bean = (Person) applicationContext.getBean("person1");
System.out.println(bean); } }

控制台打印结果:

Person [name=zhangsan, age=24, nickName=null]

nickName我们没有注入,所以为null,

可以看到我们已经成功在spring的ioc容器中注入了这个Person对象

采用javaConfig的方式注入

创建一个配置类,作用等同于spring的配置文件

@Configuration  //告诉Spring这是一个配置类
public class MainConfig { //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
} }

写一个测试类

public class MainTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// Person bean = (Person) applicationContext.getBean("person");
// System.out.println(bean); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person bean = applicationContext.getBean(Person.class); // 获取Bean,注意这里的容器是AnnotationConfigApplicationContext
System.out.println(bean); // 输出Bean //获取类型为Person.class的容器中所有的bean,
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
// 遍历
for (String name : namesForType) {
System.out.println(name);
} } }

控制台输出结果,发现已经成功注入了

Person [name=lisi, age=20, nickName=null]
person

通过配置类的形式注入bean,那bean的名字默认为方法名的小写,即person01,如何手动指定bean的名字呢?

@Bean("person") // 指定bean的名字为person,如果不写为方法名的小写。
// 以下两种写法也是可以的
// @Bean(value ="person")
// @Bean(name = "person")

最新文章

  1. java 正则匹配括号对以及其他成对出现的模式
  2. Extjs Panel
  3. ORA-06502:PL/SQL :numberic or value error: character string buffer too small
  4. apt-get install *** 出现 软件包***没有提供可供安装的候选者
  5. Medical Image Processing Conference and Journal 医学图像处理会议与期刊
  6. CSS那些事儿-阅读随笔1(CSS简介与选择符)
  7. 再次记录老K站点的工作策略
  8. java多态的理解----部分非原创
  9. iOS10 CAAnimationDelegate的适配
  10. 青年菜君与小农女送菜商业模式PK
  11. C# 曲线上的点(二) 获取距离最近的点
  12. 在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样
  13. edu9E. Thief in a Shop
  14. jquery 设置cookie、删除cookie、获取cookie
  15. Visual Studio进行Web性能测试- Part II
  16. Android 网络请求超时处理方案
  17. iOS开发-NSDictionary
  18. ADF backing Bean中常用的代码
  19. 一名优秀的UI设计师应该具备哪些条件?
  20. 再看Spring Could微服务的关键组件

热门文章

  1. TED演讲:别不信,你只需20个小时,就能学会任何事情!
  2. EFK架构图
  3. create系列创建节点的方法
  4. maven install
  5. telegraf 学习三 telegra inputs.net_response + smtp2http+ grafana 进行tcp服务状态监控
  6. “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 绝地求生(battleground)
  7. 用户画像(User Profile)
  8. URL的作用是什么?它由几部分组成?
  9. IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
  10. 【算法编程 C++ Python】二维数组查找