SpringDay01
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的更多相关文章
- Spring day01
1 实例化Spring容器 新建springday01项目1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下2.First.java测试如何启动容 ...
- Unit01: Spring简介 、 Spring容器 、 Spring IOC
Unit01: Spring简介 . Spring容器 . Spring IOC Spring (1)Spring是什么? Spring是一个开源的用来简化应用开发的框架. (2)Spring的特点? ...
- Spring知识点小结(一)
一.Spring的简介 1.spring是一个full-stack轻量级开源框架 2.spring的两大核心 IoC: inverse of control 控制反转:反转是对象 ...
随机推荐
- class A<T> where T:new()
class A<T> where T:new() 这是类型参数约束,where表明了对类型变量T的约束关系.where T:A 表示类型变量是继承于A的,或者是A本身.where T: n ...
- SDKmanager的位置
最近学习Android Studio 因为配置的问题,需要查找SDKmanager的位置 一下是查找方法: 查找到啦~
- IOS - 上APPSTORE为何因IPv6被拒?
http://blog.csdn.net/wanglixin1999/article/details/52182001
- react-native模拟机调试步骤详解 ——亲测有效!!!!
步骤 1 下载安装夜神模拟器,去夜神官网下载即可!然后安装完成!进入到初始化项目的目录,打开cmd命令,运行adb connect 127.0.0.1:62001 链接模拟器 2 链接完成之后,运行安 ...
- [Swift]LeetCode796. 旋转字符串 | Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [Swift]LeetCode982. 按位与为零的三元组 | Triples with Bitwise AND Equal To Zero
Given an array of integers A, find the number of triples of indices (i, j, k) such that: 0 <= i & ...
- linux系统安全设置策略
1.检查是否设置口令长度至少8位,并包括数字,小写字符.大写字符和特殊符号4类中至少2类. 在文件/etc/login.defs中设置 PASS_MIN_LEN 不小于标准值 修改/etc/pam.d ...
- 『sumdiv 数学推导 分治』
sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...
- Presto 常用配置及操作
一.介绍 Presto是一个开源的分布式SQL查询引擎,适用于交互式分析查询,数据量支持GB到PB字节. Presto的设计和编写完全是为了解决像Facebook这样规模的商业数据仓库的交互式分析和处 ...
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌图片:自己的图片控件
代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ...