1、BeanFactory接口

BeanFactory接口为Spring的原始接口,功能较为单一,其特点是,每次在获得对象的时候才会创建对象,而ApplicationContext 是事先创建对象,需要的时候直接从容器中去即可。BeanFactory是一个工厂,用于生成任意bean。

2、ApplicationContext 类

(1)介绍:

每次容器创建的时候就会创建容器中所配置的对象,并且功能有很大的增强。但是,ApplicationContext较为浪费资源,因为它将所有的对象一次全部创建出来,而BeanFactory是在调用和对象的时候才会去创建对象。

(2)ApplicationContext 获取配置文件的方法:

从类路径下获取配置文件:

 ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象

从硬件绝对路径下获取配置文件(指定盘符下的xml):

FileSystemXmlApplicationContext("路径")

3、ApplicationContext 类与BeanFactory接口的对比

(1)ApplicationContext 类:

StudentServiceImpl:
public class StudentServiceImpl {
private StudentDao studentDao; public StudentServiceImpl() {
System.out.println("service的实现类被创建了!!");
} public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void addStudent(){
System.out.println("StudentService的实现类的Add方法!!");
studentDao.addStudent();
}
}

测试类:

  public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
StudentServiceImpl studentService=(StudentServiceImpl) applicationContext.getBean("studentService");
studentService.addStudent();
}

测试结果:

service的实现类被创建了!!
StudentService的实现类的Add方法!!
StudentDao的实现类的Add方法!!

(2)BeanFactory接口

   public static void main(String[] args) {
BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
StudentServiceImpl studentService=(StudentServiceImpl) beanFactory.getBean("studentService");
studentService.addStudent();
}

在调用getBean的时候才会去创建对象

Spring初识、新建工程的更多相关文章

  1. 菜鸟学习Spring——初识Spring

    一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...

  2. Spring初识(通过小实例清晰认识Spring)

    1.spring架构: spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,iba ...

  3. spring初识

    Spring是一个开源框架,它是为了解决企业应用开发的复杂性而创建的.Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益. Sp ...

  4. Java使用Spring初识

    1.首先是引用了,然后pom.xml如下: <dependency> <groupId>org.springframework</groupId> <arti ...

  5. [Spring]初识Spring-Spring的基础使用-如何通过Bean来实例化?

    Spring框架的基础使用 XML配置信息,Bean的不同实例化方式,注入 实例化 XML文件中的参数设置 1.通过构造器进行实例化(重点,常用方式) <bean name="aCls ...

  6. [Spring]初识Spring-Spring是什么?如何实例化一个Spring容器?

    关于Spring入门的基础知识点 Spring简介 Spring是由Rod Johnson创建的轻量型容器,目的在于简化企业级开发.是一种容器框架 a.降低侵入性 b.提供了IOC(控制反转)和AOP ...

  7. Day2 Spring初识(二)

    Bean的实例化 bean实例化方式有3种:默认构造.静态工厂.实例工厂 默认构造 调用无参构造, 属性+setter User.java package entity; public class U ...

  8. Day1 Spring初识(一)

    在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://www.cnblogs.com/xdp-gacl/p/3707631.html和http://www.cnblogs.com/ ...

  9. Spring 初识

    一.Spring是什么? 首先可以进入Spring官网 https://spring.io/ 看一下相关介绍. Spring为开发者提供了一站式的轻量级应用开发平台.简单来说,Spring为开发者提供 ...

随机推荐

  1. 016 Ceph的集群管理_2

    一.Ceph集群的运行状态 集群状态:HEALTH_OK,HEALTH_WARN,HEALTH_ERR 1.1 常用查寻状态指令 [root@ceph2 ~]#    ceph health deta ...

  2. 20191107-8 beta week 2/2 Scrum立会报告+燃尽图 07

    此作业要求参见 http://edu.cnblogs.com/campus/nenu/2019fall/homework/9960 小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王歆瑶,梅 ...

  3. Tangram: Optimized Coarse-Grained Dataflow for Scalable NN Accelerators 阅读笔记

    目录 Tangram: Optimized Coarse-Grained Dataflow for Scalable NN Accelerators 1.Abstract 2.Introduction ...

  4. 洛谷P2657 [SCOI2009]windy数 题解 数位DP

    题目链接:https://www.luogu.com.cn/problem/P2657 题目大意:找区间 \([A,B]\) 范围内 不含前导零 且 相邻两个数字之差至少为2 的正整数的个数. 题目分 ...

  5. 02_css3.0 前端长度单位 px em rem vm vh vm pc pt in 你真的懂了吗?

    1:废话不多说,直接看如下图表: 2:px就不过多介绍了,就是像素点的大小,加入您的屏幕分辨率为1920,则每一个相当于每一个有横着的1920个像素点: 3:em 为相对单位,一般以 body 内的 ...

  6. C#反射与特性(五):类型成员操作

    目录 1,MemberInfo 1.1 练习-获取类型的成员以及输出信息 1.2 MemberType 枚举 1.3 MemberInfo 获取成员方法并且调用 1.4 获取继承中方法的信息(Decl ...

  7. vue文字间歇无缝向上滚动

    公司的管理系统中有"文字间歇无缝向上滚动"的需求,现在这种需求基本在项目开发中已经消失了,没什么新颖的,但架不住公司高层喜欢这种玩意儿,所以,作为开发人员,即使你有一百个不乐意,谁 ...

  8. Spring Boot 2.X(十九):集成 mybatis-plus 高效开发

    前言 之前介绍了 SpringBoot 整合 Mybatis 实现数据库的增删改查操作,分别给出了 xml 和注解两种实现 mapper 接口的方式:虽然注解方式干掉了 xml 文件,但是使用起来并不 ...

  9. [apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?

    前段时间写一个传递文件句柄的小 demo,有 server 端.有 client 端,之间通过 Unix Domain Socket 通讯. 在普通模式下,双方可以正常建立连接,当server端作为d ...

  10. 【转】.NET 在云原生时代的蜕变,让我在云时代脱颖而出

    原创:张善友 原文:https://www.cnblogs.com/shanyou/p/12198741.html .NET 生态系统是一个不断变化的生态圈,我相信它正在朝着一个伟大的方向发展.有了开 ...