Spring初识、新建工程
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初识、新建工程的更多相关文章
- 菜鸟学习Spring——初识Spring
一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...
- Spring初识(通过小实例清晰认识Spring)
1.spring架构: spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,iba ...
- spring初识
Spring是一个开源框架,它是为了解决企业应用开发的复杂性而创建的.Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益. Sp ...
- Java使用Spring初识
1.首先是引用了,然后pom.xml如下: <dependency> <groupId>org.springframework</groupId> <arti ...
- [Spring]初识Spring-Spring的基础使用-如何通过Bean来实例化?
Spring框架的基础使用 XML配置信息,Bean的不同实例化方式,注入 实例化 XML文件中的参数设置 1.通过构造器进行实例化(重点,常用方式) <bean name="aCls ...
- [Spring]初识Spring-Spring是什么?如何实例化一个Spring容器?
关于Spring入门的基础知识点 Spring简介 Spring是由Rod Johnson创建的轻量型容器,目的在于简化企业级开发.是一种容器框架 a.降低侵入性 b.提供了IOC(控制反转)和AOP ...
- Day2 Spring初识(二)
Bean的实例化 bean实例化方式有3种:默认构造.静态工厂.实例工厂 默认构造 调用无参构造, 属性+setter User.java package entity; public class U ...
- Day1 Spring初识(一)
在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://www.cnblogs.com/xdp-gacl/p/3707631.html和http://www.cnblogs.com/ ...
- Spring 初识
一.Spring是什么? 首先可以进入Spring官网 https://spring.io/ 看一下相关介绍. Spring为开发者提供了一站式的轻量级应用开发平台.简单来说,Spring为开发者提供 ...
随机推荐
- 小白学 Python 爬虫(32):异步请求库 AIOHTTP 基础入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- openlayers中实现点的拖拽(modify),在layer中增加修改删除point。
最近忙着整地图,都忘记了总结来沉淀自己,自我检讨一下. 总结一下最近使用openlayer时学习的内容,先说下我的业务逻辑吧,在室内地图中 1,点击新增在地图上新增一个可以拖拽的点,拖拽完成后确定位置 ...
- 基于 HTML5 WebGL 与 WebVR 3D 虚实现实的可视化培训系统
前言 2019 年 VR, AR, XR, 5G, 工业互联网等名词频繁出现在我们的视野中,信息的分享与虚实的结合已经成为大势所趋,5G 是新一代信息通信技术升级的重要方向,工业互联网是制造业转型升级 ...
- Spring中常见的设计模式——代理模式
一.代理模式的应用场景 生活中的中介,黄牛,等一系列帮助甲方做事的行为,都是代理模式的体现.代理模式(Proxy Pattern)是指为题对象提供一种代理,以控制对这个对象的访问.代理对象在客户端和目 ...
- 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 端口扫描器--利用socket协议
#!/usr/bin/env python # -*- coding:UTF-8 -*- import optparse import socket import threading # 用法 pyt ...
- DFT与IDFT
[转]https://blog.csdn.net/mingzhuo_126/article/details/88044390 二.编程实现考滤到DFT和IDFT算法过程中有部分相似,可以把它们合成到一 ...
- Qt 下QMessageBox下中文乱码问题
Qt版本 Qt Creator 2.4.1 Based on Qt 4.8.0 (64 bit) 现象 QMessageBox调用setText()一直是乱码 解决方法 QTextCodec::set ...
- Redis系列之----Redis的两种持久化机制(RDB和AOF)
Redis的两种持久化机制(RDB和AOF) 什么是持久化 Redis的数据是存储在内存中的,内存中的数据随着服务器的重启或者宕机便会不复存在,在生产环境,服务器宕机更是屡见不鲜,所以,我们希望 ...
- 如何在ArcGIS中恢复MapGIS制图表达信息
1.输出符号信息 Map2Shp软件中提供了图示表达转换功能,提供对MapGIS图形特征可视表达信息的跨平台支持.若要使用该功能,必须在转换时,"图元参数输出方式"选定为[图元参数 ...