Spring笔记04(DI(给属性赋值),自动装配(autowire))
给不同数据类型注入值:
- <?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-3.2.xsd">
- <bean id="entity" class="entity.TestEntity">
- <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
- <property name="specialCharacter1">
- <value><![CDATA[P&G]]></value>
- </property>
- <!-- 把XML特殊字符替换为实体引用 -->
- <property name="specialCharacter2">
- <value>P&G</value>
- </property>
- <!-- 定义内部Bean -->
- <property name="innerBean">
- <bean class="entity.User">
- <property name="username">
- <value>Mr. Inner</value>
- </property>
- </bean>
- </property>
- <!-- 注入List类型 -->
- <property name="list">
- <list>
- <!-- 定义List中的元素 -->
- <value>足球</value>
- <value>篮球</value>
- </list>
- </property>
- <!-- 注入数组类型 -->
- <property name="array">
- <arry>
- <!-- 定义数组中的元素 -->
- <value>足球</value>
- <value>篮球</value>
- </arry>
- </property>
- <!-- 注入Set类型 -->
- <property name="set">
- <set>
- <!-- 定义Set或数组中的元素 -->
- <value>足球</value>
- <value>篮球</value>
- </set>
- </property>
- <!-- 注入Map类型 -->
- <property name="map">
- <map>
- <!-- 定义Map中的键值对 -->
- <entry>
- <key>
- <value>football</value>
- </key>
- <value>足球</value>
- </entry>
- <entry>
- <key>
- <value>basketball</value>
- </key>
- <value>篮球</value>
- </entry>
- </map>
- </property>
- <!-- 注入Properties类型 -->
- <property name="props">
- <props>
- <!-- 定义Properties中的键值对 -->
- <prop key="football">足球</prop>
- <prop key="basketball">篮球</prop>
- </props>
- </property>
- <!-- 注入空字符串值 -->
- <property name="emptyValue">
- <value></value>
- </property>
- <!-- 注入null值 -->
- <property name="nullValue">
- <null/>
- </property>
- </bean>
- </beans>
1.DI(给属性赋值)的四种方式:
01.Student实体类:
- package cn.pb.bean;
- /**
- * 学生实体类
- */
- public class Student {
- private String name; //姓名
- private Integer age; //年龄
- private Grade grade; //年级
- @Override
- public String toString() {
- return "Student [name=" + name + ", age=" + age + ", grade=" + grade
- + "]";
- }
- // p 注入的时候 必须要有无参构造
- public Student() {
- super();
- }
- // c 注入的时候 必须要有带参构造
- public Student(String name, Integer age, Grade grade) {
- super();
- this.name = name;
- this.age = age;
- this.grade = grade;
- }
- //p注入的时候 必须要有set()
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Grade getGrade() {
- return grade;
- }
- public void setGrade(Grade grade) {
- this.grade = grade;
- }
- }
02.Grade实体类:
- package cn.pb.bean;
- /**
- * 年级实体类
- */
- public class Grade {
- private String name; //年级名称
- @Override
- public String toString() {
- return "Grade [name=" + name + "]";
- }
- public Grade() {
- super();
- }
- public Grade(String name) {
- super();
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
03.DI注入的方式:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
- 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">
- <!-- 配置年级对应的bean-->
- <bean id="grade" class="com.xdf.bean.Grade">
- <!--01.设值注入 (推荐使用,便于阅读) 在对应的类中必须有set方法,因为底层执行反射机制查询类中对应的setXxx(DI) -->
- <property name="gradeId" value="1"/>
- <property name="name" value="一年级"/>
- </bean>
- <!-- 配置学生对应的bean 02.p命名空间赋值 必须有set()和无参构造
- <bean id="student" class="com.xdf.bean.Student"
- p:age="18" p:name="小黑" p:grade-ref="grade"/>-->
- <!--03.通过构造方法给属性赋值 前提是 必须有对应的带参构造方法 不需要set和get,无参构造也不需要
- <bean id="student" class="com.xdf.bean.Student">
- 001:使用参数的下标
- <constructor-arg index="0" value="xiaohei"/>
- <constructor-arg index="1" value="19"/>
- <constructor-arg index="2" ref="grade"/>
- 002:使用参数的名称
- <constructor-arg name="name" value="xiaohei"/>
- <constructor-arg name="age" value="19"/>
- <constructor-arg name="grade" ref="grade"/>
- 003:使用参数的默认顺序
- <constructor-arg value="xiaohei"/>
- <constructor-arg value="19"/>
- <constructor-arg ref="grade"/>
- </bean>-->
- <!--04.通过c命名空间(构造方法)给属性赋值 前提是 必须有对应的带参构造方法-->
- <bean id="student" class="com.xdf.bean.Student"
- c:age="18" c:name="xiaobai" c:grade-ref="grade"/>
- </beans>
04.测试代码:
- package cn.pb;
- import cn.pb.bean.Student;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class StudentTest {
- @Test
- public void test01(){
- ApplicationContext context=
- new ClassPathXmlApplicationContext("applicationContext.xml");
- /*
- * 获取容器给我们创建的Student对象 ioc的体现
- * 本身由自身创建对象的过程,把创建对象的权利移交给了spring容器! IOC 控制反转
- *
- * 之前 Student student=new Student();
- * 现在context.getBean("student");
- * context容器来创建对象了
- *
- */
- Student student=(Student) context.getBean("student");
- System.out.println(student);
- }
- }
2.自动装配(autowire):
01.主人实体类:
- /**
- * 主人类
- */
- public class Person {
- private String name; //姓名
- private int age; //年龄
- private Dog dog; //主人的宠物
- private Cat cat; //主人的宠物
- public Cat getCat() {
- return cat;
- }
- public void setCat(Cat cat) {
- this.cat = cat;
- }
- public Dog getDog() {
- return dog;
- }
- public void setDog(Dog dog) {
- this.dog = dog;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", dog=" + dog +
- ", cat=" + cat +
- '}';
- }
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public Person() {
- }
- }
02.宠物狗实体类:
- /**
- * 宠物狗类
- */
- public class Dog {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- '}';
- }
- }
03.宠物狗的子类小狗实体类:
- /**
*继承了宠物狗类 所以宠物狗的非私有的东西 他全有
*/
public class SmallDog extends Dog {- }
04.宠物猫实体类:
- /**
- * 小猫咪类
- */
- public class Cat {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- '}';
- }
- }
05.applicationContext.xml配置文件(自动装配):
- <?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">
- <!-- autowire 自动装配对象 有两种方式
- 01.byName
- spring会根据实体类中的属性名,去找xml文件中id为属性名的bean进行装配!
- 02.byType
- spring会根据实体类中的属性类型,去找xml文件中找一个class是 属性类型的类进行装配,
- 如果有多个 会报错!
- -->
- <!--配置的主人bean-->
- <bean id="person" class="cn.pb.bean.Person" autowire="byName">
- <property name="age" value="20"/>
- <property name="name" value="xiaohei"/>
- <!--直接引用 autowire属性没有效果 <property name="dog" ref="dog"/>-->
- </bean>
- <!--配置 宠物狗bean-->
- <bean id="dog" class="cn.pb.bean.Dog">
- <property name="name" value="哈士奇"/>
- </bean>
- <!--配置 小狗bean-->
- <bean id="smallDog" class="cn.pb.bean.SmallDog">
- <!--SmallDog中没有任何属性 ,但是继承父类的name-->
- <property name="name" value="小狗哈士奇"/>
- </bean>
- <!--主人的第二个宠物 猫咪-->
- <bean id="cat" class="cn.pb.bean.Cat">
- <property name="name" value="机器锚"/>
- </bean>
- </beans>
06.测试代码:
- public class PersonDemo {
- public static void main(String[] args) {
- ApplicationContext context=new ClassPathXmlApplicationContext
- ("applicationContext.xml");
- //获取主人信息
- Person person = (Person) context.getBean("person");
- System.out.println(person);
- }
- }
Spring笔记04(DI(给属性赋值),自动装配(autowire))的更多相关文章
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- spring(四):spring中给bean的属性赋值
spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- Spring点滴十:Spring自动装配(Autowire)
在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入.Spring 容器也可以 ...
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- Spring(二)scope、集合注入、自动装配、生命周期
原文链接:http://www.orlion.ga/189/ 一.scope bean的scope属性中常用的有两种:singleton(单例,默认)和prototype(原型,每次创建新对象) 例: ...
- 大厂面试官问你META-INF/spring.factories要怎么实现自动扫描、自动装配?
大厂面试官问你META-INF/spring.factories要怎么实现自动扫描.自动装配? 很多程序员想面试进互联网大厂,但是也有很多人不知道进入大厂需要具备哪些条件,以及面试官会问哪些问题, ...
- Spring注解 - 生命周期、属性赋值、自动装配
一.Bean的生命周期 流程 Bean创建 -- 初始化 -- 销毁 创建: 单实例:在容器启动时创建对象 多实例:每次调用时创建对象 初始化: 都是在对象创建完成后,调用初始化方法 销毁: 单实例: ...
随机推荐
- Lua学习九----------Lua字符串
© 版权声明:本文为博主原创文章,转载请注明出处 1.Lua字符串 - ''单引号间的一串字符 - ""双引号之间的一串字符 - [[]]之间的一串字符 2.Lua转义字符 3.字 ...
- 通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明。
错误原因: xml文件中,本来是要配置成下面这样的: http://www.springframework.org/schema/context http://www.springframework. ...
- C#如何遍历数组?
// 一维数组 int[] arr = { 1, 2, 3, 4, 5 }; foreach (int i in arr) { Console.WriteLine(i.ToString() + &qu ...
- antd引入普通html使用,将ant Design本地化
一直想着能本地化antd的,不用npm以及dva那么复杂的配置环境来开发,并且本地化以后对以后链接flask的模板渲染机制也能很好的结合.下面是具体的实现方法: 1.将react的相关链接引入: &l ...
- Android发送验证码的倒计时button
1 直接上图 2 原理 原理非常easy,就是把对应的倒计时逻辑等封装到一个控件中,并向外部提供接口. 3 代码 import java.util.Timer; import java.util.Ti ...
- iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题
本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37 segmentfault-博客原文 http://segm ...
- EasyNVR如何实现跨域鉴权
EasyNVR提供简单的登录鉴权,客户端通过用户名密码登录成功后,服务端返回认证token的cookie, 后续的接口访问, 服务端从cookie读取token进行校验. 但是, 在与客户系统集成时, ...
- 基于Darwin实现的分布式流媒体直播服务器系统
各位EasyDarwin开源项目的爱好者,您好,这篇博客的年限有点老了,目前EasyDarwin已经采用全新的云平台架构,详细可以参考博客:http://blog.csdn.net/xiejiashu ...
- WCF基础之设计和实现服务协定
本来前面还有一个章节“WCF概述”,这章都是些文字概述,就不“复制”了,直接从第二章开始. 当然学习WCF还是要些基础的.https://msdn.microsoft.com/zh-cn/hh1482 ...
- windows系统下nodejs、npm、express的下载和安装教程——2016.11.09
1. node.js下载 首先进入http://nodejs.org/dist/,这里面的版本呢,几乎每个月都出几个新的,建议大家下载最新版本,看看自己的电脑是多少位的,别下错了. 下载完解压到你想放 ...