Spring基于XML的IoC
Maven导入Spring依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
</dependency>
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="account" class="com.ttpfx.domain.Account"/>
</beans>
BeanFactory 和 ApplicationContext 的区别
BeanFactory:在使用对象时创建对象
ApplicationContext:在加载配置文件时创建对象(默认情况下)
使用BeanFactory:
Resource resource = new ClassPathResource("Beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Account account = beanFactory.getBean(Account.class);
System.out.println(account);
使用ApplicationContext:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("Beans.xml");
Account account = applicationContext.getBean("account", Account.class);
System.out.println(account);
创建Bean的三种方式
- 通过构造方法创建对象
<bean id="account" class="com.ttpfx.domain.Account"/>
- 通过普通工厂类的实例方法创建实例:
<bean id="instanceFactory" class="com.ttpfx.factory.InstanceFactory"/>
<bean id="account" factory-bean="instanceFactory" factory-method="getAccount"/>
- 通过静态工厂类的静态方法创建实例:
<bean id="account" class="com.ttpfx.factory.StaticFactory" factory-method="getAccount"/>
Bean的作用范围
通过bean标签的scope属性指定其作用域
<bean id="account" class="com.ttpfx.domain.Account" scope="prototype"/>
- singleton:在Spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值
- prototype:每次调用getBean时创建一个新的实例
- request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
- session:同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境
- global-session:一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext环境
Spring基于XML的IoC的更多相关文章
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_3 spring基于XML的IOC环境搭建和入门
创建新项目 修改为jar包的方式 把上一个工程内的代码 java下的com复制过来 由于配置文件没有,所以一运行就会报错 factory文件夹整个删除 dao的实现类 这里删除 测试类保留这两行代码 ...
- Spring基于XML的IOC环境搭建及入门
一.使用Maven构建Java项目 * 项目目录结构 1. 在sun.service包下创建UserDao接口和接口实现类: UserDao接口: package sun.service; /** * ...
- spring基于xml的IOC环境搭建和入门
配置pom.xml的依赖 <packaging>jar</packaging> <dependencies> <dependency> <grou ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- 01Spring基于xml的IOC配置--入门
01Spring基于xml的IOC配置 1.创建一个普通的maven工程 1.1 选择maven,不用骨架,点击下一步. 1.2 填写GroupId.ArtifactId.Version.填完点击下一 ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring 基于xml的申明式AspectH中的后置通知的返回值获取
spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...
- spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
随机推荐
- hdu4801 PocketCube 2阶魔方
http://acm.hdu.edu.cn/showproblem.php?pid=4801 1. 题目描述给定一个2×2×22×2×2的魔方,当某个面上的4个小块颜色均相同时,称这个面为comple ...
- 卧槽,sql注入竟然把我们的系统搞挂了
前言 最近我在整理安全漏洞相关问题,准备在公司做一次分享.恰好,这段时间团队发现了一个sql注入漏洞:在一个公共的分页功能中,排序字段作为入参,前端页面可以自定义.在分页sql的mybatis map ...
- 设置chromium的默认搜索引擎为Bing
设置 -> 搜索 -> 管理搜索引擎 第三项中添加: http://cn.bing.com/search?q=%s 即可.
- 前端水印方案 All In One
前端水印方案 All In One base64 用户名 图片水印 <div id="wm" style="pointer-events: none; width: ...
- Battery API All In One
Battery API All In One https://caniuse.com/?search=Battery navigator.getBattery() /* Promise {<pe ...
- macOS & PostgreSQL
macOS & PostgreSQL macOS 上安装 PostgreSQL 后为什么会自动创建一个系统用户账号 https://get.enterprisedb.com/postgresq ...
- 法兰西金融专访SPC空投重磅来袭
最近,法兰西金融日报联合德意志财经等知名金融媒体就SPC这一话题进行了专访. 法兰西金融日报记者德维尔斯问到,之前2020年的BGV项目等市场反响异常火爆,2021年已经来到,NGK目前有何新的大动作 ...
- 从微信小程序到鸿蒙js开发【06】——swiper&animator&marquee
目录: 1.swiper轮播图 2.image-animator幻灯片 3.marquee跑马灯 4.nginx动静分离 1.swiper轮播图 微信小程序的swiper组件中只能放置swiper-i ...
- [JAVA学习笔记]JAVA基本程序设计结构
一个简单的Java应用程序 public class FirstSample { public static void main(String[] args) { System.out.println ...
- java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
一.Scanner类 1.api简介: 应用程序编程接口 2.Scanner类: 作用:获取键盘输入的数据 位置: java.util.Scanner. 使用:使用成员方法nextInt() 和 ne ...