Spring框架——IOC 自动装载
IOC自动装载有两种形式
<?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"> <!-- ByName --> <bean id="person" class="com.sunjian.entity.Person" autowire="byName"> <property name="id" value="1"></property> <property name="name" value="张三疯"></property> </bean> <bean id="car" class="com.sunjian.entity.Car"> <property name="id" value="1"></property> <property name="brand" value="野马"></property> </bean> </beans>
<?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"> <!-- ByType --> <bean id="person" class="com.sunjian.entity.Person" autowire="byType"> <property name="id" value="1"></property> <property name="name" value="欧阳锋"></property> </bean> <bean id="car2" class="com.sunjian.entity.Car"> <property name="id" value="2"></property> <property name="brand" value="法拉利"></property> </bean> </beans>
class
package com.sunjian.entity; public class Car { private Integer id; private String brand; public void setId(Integer id) { this.id = id; } public void setBrand(String brand) { this.brand = brand; } public Car(){ System.out.println("创建了Car对象"); } public Car(Integer id, String brand) { this.id = id; this.brand = brand; } @Override public String toString() { return "Car{" + "id=" + id + ", brand='" + brand + '\'' + '}'; } }
package com.sunjian.entity; public class Person { private Integer id; private String name; private Car car; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", car=" + car + '}'; } }
test class
package com.sunjian.test; import com.sunjian.entity.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author sunjian * @date 2020/3/14 15:17 */ public class Test3 { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowired.xml"); Person person = (Person) applicationContext.getBean("person"); System.out.println(person); applicationContext = new ClassPathXmlApplicationContext("autowired2.xml"); Person person2 = (Person) applicationContext.getBean("person"); System.out.println(person2); } }
创建了Car对象 Person{id=1, name='张三疯', car=Car{id=1, brand='野马'}} 创建了Car对象 Person{id=1, name='欧阳锋', car=Car{id=2, brand='法拉利'}}
OK.
Spring框架——IOC 自动装载的更多相关文章
- Spring框架IOC容器和AOP解析 非常 有用
Spring框架IOC容器和AOP解析 主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...
- 自己动手写Spring框架--IOC、MVC
对于一名Java开发人员,我相信没有人不知道 Spring 框架,而且也能够轻松就说出 Spring 的特性-- IOC.MVC.AOP.ORM(batis). 下面我想简单介绍一下我写的轻量级的 S ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- spring框架--IOC容器,依赖注入
思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action 多个 [维护成员变量] service 一个 [不需要维护公共变量] dao 一个 [不需要维护 ...
- spring Bean类自动装载实现
先贴spring的开发文档,有助于大家学习http://shouce.jb51.net/spring/beans.html#beans-factory-class 一直想研究一下spring bean ...
- Spring框架IOC,DI概念理解
1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...
- Spring框架-IOC和AOP简单总结
参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...
- Spring框架 IOC注解
Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring ...
- Spring框架IOC和AOP介绍
说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...
随机推荐
- vm文件的优点
vm文件的优点 相较于内容写在jsp 文件: 1.在网页上上浏览和下载的内容用的是同一套,也就是说只需要维护一套内容,页面上看到的和下载得到的是一致的. 2.版本控制较为简便, 实现了页面内容和jsp ...
- unittest(11)- get_data自定义取某几条测试数据
在get_data中定义取全部用例和取部分用例两种模式 # 1. http_request.py import requests class HttpRequest: def http_request ...
- JavaScript 中事件对象参数:clientX、clientY、offsetX、offsetY、screenX、screenY
JavaScript 中一些概念理解 :clientX.clientY.offsetX.offsetY.screenX.screenY clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x ...
- python基础修改haproxy配置文件
1.通过eval(),可以将字符串转为字典类型. 2.Encode过程,是把python对象转换成json对象的一个过程,常用的两个函数是dumps和dump函数.两个函数的唯一区别就是dump把py ...
- 4K手机能拯救索尼手机吗?
智能手机屏幕分辨率究竟达到多少才是极限,一直是业内争论不休的问题.从低分辨率一路走来,直到iPhone 4搭载视网膜屏,业内才有了一个较为统一的认知:屏幕起码要在合适距离下看不到文字.图像虚影,才称得 ...
- git 学习 3
远程仓库 添加远程库 GitHub 注册账号并建立 repository,Clone with SSH 1 $ ssh-keygen -t rsa -C "youremail@example ...
- linux常用命令使用指南
<软件自动化测试开发>出版啦 1 系统相关 useradd/userdel 添加用户/删除用户 su 切换用户命令 ls 用于查看所有文件夹的命令 列出目录内容 ...
- 一个异步访问redis的内存问题
| 分类 redis | 遇到一个redis实例突然内存飙高的案例, 具体症状如下: 客户端使用异步访问模式 单个请求的回包很大,hgetall一个8M的key 由于访问量比较大,已经登录不上red ...
- springboot java web开发工程师效率
基础好工具 idea iterm2 和 oh-my-zsh git 热加载 java web项目每次重启时间成本太大. 编程有一个过程很重要, 就是试验, 在一次次试验中探索, 积累素材优化调整程序模 ...
- Redis(3)——分布式锁深入探究
一.分布式锁简介 锁 是一种用来解决多个执行线程 访问共享资源 错误或数据不一致问题的工具. 如果 把一台服务器比作一个房子,那么 线程就好比里面的住户,当他们想要共同访问一个共享资源,例如厕所的时候 ...