Spring的控制反转

Spring的依赖注入

  多种注入方式

  多种属性的注入方式

<bean id="userDao" class="dao.UserDaoImpl"></bean>

<!-- 构造方法的方式注入属性 -->
<bean id="car" class="entity.Car">
<constructor-arg name="name" value="保时捷"></constructor-arg>
<constructor-arg name="price" value="1000000"></constructor-arg>
</bean> <!-- set方法的方式注入属性 -->
<bean id="car2" class="entity.Car2">
<property name="name" value="奇瑞QQ"></property>
<property name="price" value="40000"></property>
</bean> <!-- 注入对象类型的注入 -->
<bean id="person" class="entity.Person">
<property name="name" value="张三"></property>
<property name="car" ref="car"></property>
</bean> <!-- Spring的复杂类型的注入 -->
<bean id="collectionBean" class="entity.CollectionBean">
<!-- 数组类型的属性 -->
<property name="array">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property> <!-- 注入List集合的数据 -->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property> <!-- 注入Map集合 -->
<property name="map">
<map>
<entry key="aaa" value="111"></entry>
<entry key="bbb" value="222"></entry>
<entry key="ccc" value="333"></entry>
</map>
</property>
</bean>
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.UserDao;
import entity.Car;
import entity.Car2;
import entity.CollectionBean;
import entity.Person; public class Test1 { @Test
public void demo(){
//创建Spring的工厂类:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂解析XML获取Bean的实例.
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello(); Car car = (Car) applicationContext.getBean("car");
System.out.println(car.getName());
System.out.println(car.getPrice()); System.out.println("---------------------------"); Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2.getName());
System.out.println(car2.getPrice()); System.out.println("---------------------------");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person.getName());
System.out.println(person.getCar().getName()); System.out.println("---------------------------");
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(collectionBean.getArray().length);
System.out.println(collectionBean.getList().toString());
System.out.println(collectionBean.getMap().toString());
}
}

SpringDay01的更多相关文章

  1. Spring day01

    1 实例化Spring容器 新建springday01项目1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下2.First.java测试如何启动容 ...

  2. Unit01: Spring简介 、 Spring容器 、 Spring IOC

    Unit01: Spring简介 . Spring容器 . Spring IOC Spring (1)Spring是什么? Spring是一个开源的用来简化应用开发的框架. (2)Spring的特点? ...

  3. Spring知识点小结(一)

    一.Spring的简介 1.spring是一个full-stack轻量级开源框架    2.spring的两大核心        IoC: inverse of control  控制反转:反转是对象 ...

随机推荐

  1. ArcMap AddIn之下载ArcGIS Server地图服务中的数据

    涉及到开发知识点1.ArcGIS Server地图服务 2.C# web请求获取数据 3.AddIN开发技术 工具界面: 具体涉及到的代码之后有空贴出来.先上工具 AddIn插件下载地址:点击这里下载 ...

  2. 百度地图API 自定义标注图标

    通过Icon类可实现自定义标注的图标,下面示例通过参数MarkerOptions的icon属性进行设置, 也可以使用marker.setIcon()方法. <script type=" ...

  3. 吴恩达机器学习笔记56-多元高斯分布及其在误差检测中的应用(Multivariate Gaussian Distribution & Anomaly Detection using the Multivariate Gaussian Distribution)

    一.多元高斯分布简介 假使我们有两个相关的特征,而且这两个特征的值域范围比较宽,这种情况下,一般的高斯分布模型可能不能很好地识别异常数据.其原因在于,一般的高斯分布模型尝试的是去同时抓住两个特征的偏差 ...

  4. [Swift]LeetCode252.会议室 $ Meeting Rooms

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [Swift]LeetCode514. 自由之路 | Freedom Trail

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  6. [Swift]LeetCode897. 递增顺序查找树 | Increasing Order Search Tree

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  7. WebWorker与WebSocket实现前端消息总线

    Web Worker让JS有了多线程的能力,可以将复杂耗时的操作都交付给Worker线程处理.WebSocket让web端与服务端维持一个有效的长连接,实现服务端主动推送数据.将二者一结合,业务系统信 ...

  8. 【Spark篇】---Spark初始

    一.前述 Spark是基于内存的计算框架,性能要优于Mapreduce,可以实现hadoop生态圈中的多个组件,是一个非常优秀的大数据框架,是Apache的顶级项目.One stack  rule  ...

  9. Python内置函数(41)——max

    英文文档: max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) Return the largest item in an i ...

  10. Python内置函数(43)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...