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也是轻量级的. 轻 ...
随机推荐
- 向MyEclipse的项目中导入js文件时,出现小红叉
这个问题困扰我很久.刚开始时,也没有解决,因此也在网上寻找解决方法,还是没能解决.最近做项目时再一次出现了这样的问题,于是决定还是再找找办法.在此,分享一下自己的解决方法,给正处于痛苦中的童鞋们带来解 ...
- 关于Docker清理
在Docker的日常使用中,我们或许偶尔遇到下面这些情况: 12345678 $ docker-compose ps[27142] INTERNAL ERROR: cannot create temp ...
- 使用Win10自带的虚拟机安装Linux
Win10自带了一款虚拟机,感觉不错,可以试用一下: 安装:http://news.mydrivers.com/1/650/650018.htm 主要思路就是在BIOS里面开启CPU虚拟化,然后在Wi ...
- 让git push命令不再需要密码
最近利用jekyll写博客,为的就是博客管理方便,但是在上传博客的时候使用git push命令每次都得输入github帐号和密码特别的不方便,于是就搜了一下. 在这篇文章里提到,GitHub获得远程库 ...
- selenium之浏览器、元素、鼠标等操作总结
1 控制浏览器 Selenium 主要提供的是操作页面上各种元素的方法,但它也提供了操作浏览器本身的方法,比如浏览器的大小以及浏览器后退.前进按钮等. 1.1 控制浏览器窗口大小 在不同的浏览 ...
- hexo及next主题修改
通过npm uninstall <package>命令,你可以将node_modules目录下的某个依赖包移除: 1 npm uninstall 包名 要从package.json文件的依 ...
- 最通俗易懂的 Java 11 新特性讲解
大多数开发者还是沉浸在 Java 8 中,而 Java 14 将要在 2020 年 3 月 17 日发布了,而我还在写着 Java 11 的新特性.Java 11 是 Java 8 之后的第一个 LT ...
- 连接器巨头Molex莫仕大裁员,CEO更迭
序言:中美贸易战的大环境下,美国多方面限制对华出口电子科技,其中影响最大的莫过于限制芯片出口,中国本土芯片和电子产业也在蓬勃的发展.根据正能量电子了解连接器巨头MOLEX莫仕公司收入的1/3是来自于对 ...
- 统计 Django 项目的测试覆盖率
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 我们完成了对 blog 应用和 comment 应用这两个核心 app 的测试.现在 ...
- jquery 的animate 的transform
$(function(){ var t = 1000; $("#id").animate( {borderSpacing:180}, //180 指旋转度数 { step: fun ...