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为开发者提供 ...
随机推荐
- .NET 半天搭建Jenkins持续集成与自动化部署系统
前言 相信每一位程序员都经历过深夜加班上线的痛苦!而作为一个加班上线如家常便饭的码农,更是深感其痛.由于我们所做的系统业务复杂,系统庞大,设计到多个系统之间的合作,而核心系统更是采用分布式系统架构,由 ...
- Python基础复习面向对象篇
目录 类与对象的概念 实例方法 实例变量 初始化方法 析构方法 常用内置方法 继承 类方法与静态方法 动态扩展类与实例 @property装饰器 概述 面向对象是当前流行的程序设计方法,其以人类习惯的 ...
- Redis-NoSQL入门和概述(一)
NoSQL简史及定义 NoSQL 这个术语最早是在 1998 年被Carlo Strozzi命名在他的轻量的,开源的关系型数据库上的,但是该数据库没有提供标准的SQL接口:在2009 年再次被Eric ...
- .Net PE
// ConsoleApplication26.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...
- mysql 执行计划查看
使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈.explain执行计划包含的信息 其中最重要的字段为:id ...
- JS中如何比较两个数组,取得数组二相对于数组一新增和去除的元素
//数组二相对于数组一所新增的数据 function add_msg(a,b){ return a.filter(function(i){ return b.indexOf(i) === -1 }) ...
- poj 2689 区间素数筛
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- sg函数的变形 - 可以将一堆石子分开
Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...
- 解决.net core读取appSetting.json文件中文字符乱码
如上所诉 vs菜单栏中 :工具 =>自定义 => 命令 =>添加命令 =>文件 =>找到高级保存选项点击 然后关闭,这时在visual studio界面就会有高级保存选 ...
- IO系统-文件与目录操作
1.文件内核数据结构 一个打开的文件在内核中使用三种数据结构表示: (1)文件描述符表 文件描述符标志 文件表项指针 (2)文件表项: 文件状态标志:读.写.追加.同步和非阻塞等状态标志 当前文件偏移 ...