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. Oracle:WITH AS () Merge ?

    WITH AS 语法在SQL SERVER 和ORACLE数据库上均支持,主要用于子查询.语法如下: WITH expression_name [ ( column_name [,...n] ) ] ...

  2. 实战深度学习OpenCV(二):读取并播放本地或者摄像头的视频

    一.读取并播放的代码如下: #include "pch.h" #include <iostream> #include <opencv2/core/core.hp ...

  3. 【RL-TCPnet网络教程】第18章 BSD Sockets基础知识

    第18章      BSD Sockets基础知识 本章节为大家讲解BSD Sockets,需要大家对BSD Sockets有个基础的认识,方便后面章节Socket实战操作. (本章的知识点主要整理自 ...

  4. Python函数声明以及与其他编程语言数据类型的比较

    1.函数声明 与其它大多数语言一样 Python 有函数,但是它没有像 C++ 一样的独立的头文件:或者像 Pascal 一样的分离的  interface / implementation 段.在需 ...

  5. jQuery ajax如何传多个值到后台页面,举例:

    一.js代码 <script type="text/JavaScript">$("#save_change_<{$aff.Id}>"). ...

  6. [Swift]LeetCode274.H指数 | H-Index

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  7. [Swift]LeetCode289. 生命游戏 | Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  8. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [Swift]LeetCode457. 环形数组循环 | Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  10. django启动server报错Error: That port is already in use.

    这种一般是端口错误,一般是要把端口关掉,这里提供了两种方法. 方法一:直接命令: sudo lsof -t -i tcp:8000 | xargs kill -9 方法二:脚本:名字manage.py ...