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也是轻量级的. 轻 ...
随机推荐
- 自定义一个简单的SegmentedControl
先大概看下我们想实现简单的效果 源码 // // DSegmentedControl.swift // IOS学习之自定义UISegmentedControl // // Created by din ...
- 4-CSS规范
4.1 命名规范:4.1.1 css文件命名 reset.css 重置样式,重置元素默认样式,使得页面在所有浏览器中有统一的外观 global.css 全局样式,全站公用,定义页面基础样式(常见的公共 ...
- grep显示前后几行信息
显示foo及前5行 1 grep -B 5 foo file 显示foo及后5行 1 大专栏 grep显示前后几行信息ode"> grep -A 5 foo file 显示 file ...
- mysql中tinyint、smallint、int和bigint类型的用法区别
mysql中tinyint.smallint.int和bigint类型的用法区别: 在MySQL的数据类型中,Tinyint的取值范围是:带符号的范围是-128到127.无符号的范围是0到255(见官 ...
- GRE阅读
界面和托福差不多,就是反一反 GRE先读文章!因为出题顺序不一致.另外,不能跳读!!每一句都要读,即使不是观点. 考察能力: 1 三秒版本 边读边概括 解决前面的抗遗忘能力 2 句间关系 取同 取反 ...
- prometheus服务发现机制
一. Prometheus与服务发现 1.1 目前支持的服务发现方式 二. 案例 2.1 基于文件的服务发现 2.2 基于Consul的服务发现 三.本地测试 3.1 基于文件的服务发现 1.测试环境 ...
- JavaScript判断一个对象是否为空
本文介绍了判断一个对象是否为空的几种方法 测试用例 test1 = 1; test2 = {}; test3 = {a:1,b:2} 1. 判断Object.keys()的长度 function _i ...
- "长辈牌"电子产品:有一种评论朋友圈叫给你打电话
一.长辈们使用电子产品的姿势集合 先问你一个问题:「怎么下载搜狗输入法?」 (非广告) 摁?看到这篇文章的你可能都有点懵,不就下载安装就完了吗?但是,真的就只是这样吗? 前一段时间,当家里的长辈问到我 ...
- redis环境搭建及一主二从三哨兵模式配置
一.单机redis环境搭建 1.安装: OS:linux redhat6.5 下载redis 官网下载链接:https://redis.io/download 把安装包上传到服务器,进行解压 [roo ...
- 06 EntityManager和EntityTransaction
EntityManager 在 JPA 规范中, EntityManager是完成持久化操作的核心对象.实体类作为普通 java对象,只有在调用 EntityManager将其持久化后才会变成持久化对 ...