一、创建实例

工程的结构如下图

1、创建接口

public interface Person {

	public void setName(String name);

	public String say();

}

  

2、创建接口实现

public class PersonImpl implements Person {

	private String name;

	public void setName(String name) {
this.name = name; } public String say() { if(null == name){
return "Nick";
}
return "Hello," + name;
} }

3、引入commons-logging.jar 和spring-asm.jar

4、创建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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="person" class="com.example.spring.PersonImpl">
<property name="name" value="Tom" />
</bean>
</beans>

5、创建测试类

public class SpringTest {

	public static void main(String[] args) {

		ClassPathResource resource = new ClassPathResource("com/example/spring/beans.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(resource); Person person = (Person)factory.getBean("person"); System.out.println(person.say()); }
}

6、总结:  

1、Spring的bean实际上是缓存咋CurrentHashMap对象中

2、在创建bean之前,首先需要将该Bean的创建标识定好,表示该bean已经或是即将被创建,为的是增强缓存的效率。

3、根据bean的scope属性来确定是singleton还是propotype等范围,然后创建相应的bean对象

4、通过Java反射来创建Bean的实例,在创建之前检查访问修饰符,如果不是public,则调用setAccessibile(true)来突破Java的语法限制,使得可以通过如私有的构造方法来创建对象。

5、接下来,寻找bean的属性值,完成属性的注入。

6、将所创建出的singleton对象添加到缓存当中,供下次调用使用。

创建加载bean的实例的更多相关文章

  1. Spring多种加载Bean方式简析

    1 定义bean的方式 常见的定义Bean的方式有: 通过xml的方式,例如: <bean id="dictionaryRelMap" class="java.ut ...

  2. 【死磕 Spring】----- IOC 之 加载 Bean

    原文出自:http://cmsblogs.com 先看一段熟悉的代码: ClassPathResource resource = new ClassPathResource("bean.xm ...

  3. spring加载bean流程解析

    spring作为目前我们开发的基础框架,每天的开发工作基本和他形影不离,作为管理bean的最经典.优秀的框架,它的复杂程度往往令人望而却步.不过作为朝夕相处的框架,我们必须得明白一个问题就是sprin ...

  4. IoC 之加载 Bean:总结

    上文中我们将bean已经加载到了IOC容器中,接下来我们将把IOC加载Bean出来进行代码解析 备注:(有些解释是参考别个博客的相关解释 )一起探讨请加我QQ:1051980588 bean 的初始化 ...

  5. Bean 注解(Annotation)配置(1)- 通过注解加载Bean

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  6. Bean XML 配置(1)- 通过XML配置加载Bean

    系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...

  7. 字节码加载和class实例的顺序问题

    刷头条的时候看到了这个:  你做会错的一道Java面试题:字节码加载和class实例的顺序问题 以前也看到过,应该是阿里的校招笔试题,当时懒得理这种工作中毫无意义的东西. 今天突然来了兴趣,就想看看能 ...

  8. sping加载bean都发生了些什么

    问题描述:使用@Autowired注入的类,没有实例化 //Controller @RequestMapping(value="/deepblue") @Controller pu ...

  9. spring Boot加载bean

    1.SpringBoot中加载bean,可以使用注解@compenent直接加载到applicationContext容器中 2.在直接类@Configuration中,手动注册bean,如:

随机推荐

  1. mybatis中如何将多个表的查询结果,放入结果集中返回

    1.首先需要将resultMap进行改造,为了避免对其他sql的影响建议另外定义一个resultMapExtral,避免id相同, 2.然后在resultMapExtral中添加其它表的字段,若多个表 ...

  2. ansible之基础篇(三)

    setup ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible_date_tim ...

  3. Java Decompiler反编译Jar文件

    1.重新编译已经打包的Jar包,使用 Java Decompiler 打开需要重新编译的jar包,找到自己需要自己修改的Class文件 ,修改之后电子保存文件 ,保存的时候编译工具自动将class文件 ...

  4. helm笔记

    一.注意事项 1.values.yaml   中可以使用'#'号注释行,而/templates 下的文件不能用#号,如果要注释可以使用 {{/*  context  */}} 2.{{-    #忽略 ...

  5. HTML&CSS基础-边框简写属性

    HTML&CSS基础-边框简写属性 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> ...

  6. 铁力项目mysql异常处理过程记录

    地区:铁力 故障:2019-06-26 10:19:34 139921514837760 [ERROR] mysqld: Error writing file 'mysql-bin' (errno: ...

  7. pyserial 挺强大的

    Ref: https://pythonhosted.org/pyserial/ pyserial写的很规范,无论安装和使用都非常的容易,目前使用下来非常好. 没有使用它做过压力测试,不知道表现如何. ...

  8. Celebrate it, this is my first time on this blog.

    After hovered around the technology edge many years(so ashamed), I want to collected all the points ...

  9. java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer

    mybatis配置的jdbaType类型要是大写的,否则就会出现此种异常 原因是在xml中配置的 jdbcType中有小写字母

  10. JavaScript 进阶问题列表

    https://github.com/lydiahallie/javascript-questions/blob/master/zh-CN/README-zh_CN.md 很考基本功