通过构造函数注入属性值

package com.hsp.constructor;public class Employee {private String name;private int age;public Employee(){}public Employee(String name, int age) {System.out.println("Employee(String name, int age) 函数被调用..");this.name = name;this.age = age;}public Employee(String name){this.name=name;}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;}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置一个雇员对象 -->
<bean id="employee" class="com.hsp.constructor.Employee">
<!-- 通过构造函数来注入属性值 -->
<constructor-arg index="0" type="java.lang.String" value="大明风华" />
<constructor-arg index="1" type="int" value="22"/>
</bean>
</beans>
package com.hsp.constructor;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/constructor/beans.xml");Employee ee=(Employee) ac.getBean("employee");System.out.println(ee.getName());}}

运行结果
2020-3-2 12:30:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Mon Mar 02 12:30:52 CST 2020]; root of context hierarchy
2020-3-2 12:30:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/hsp/constructor/beans.xml]
2020-3-2 12:30:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@290fbc
2020-3-2 12:30:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@290fbc: defining beans [employee]; root of factory hierarchy
Employee(String name, int age) 函数被调用…
大明风华

自动装配bean的属性值

①byName

package com.hsp.autowire;public class Dog {private String name;private int age;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;}
}
package com.hsp.autowire;public class Master {private String name;private Dog dog;public String getName() {return name;}public void setName(String name) {this.name = name;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" ><!-- 配置一个master对象 -->
<bean id="master" class="com.hsp.autowire.Master" autowire="byName">
<property name="name">
<value>二哈的主人</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog"  class="com.hsp.autowire.Dog">
<property name="name" value="大黄"/>
<property name="age" value="3"/>
</bean>
</beans>
package com.hsp.autowire;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App1 {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/autowire/beans.xml");//获取Master master=(Master) ac.getBean("master");System.out.println(master.getName()+" 养了条"+master.getDog().getName());}}

运行结果:
2020-3-2 14:35:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Mon Mar 02 14:35:51 CST 2020]; root of context hierarchy
2020-3-2 14:35:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/hsp/autowire/beans.xml]
2020-3-2 14:35:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7
2020-3-2 14:35:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7: defining beans [master,dog]; root of factory hierarchy
二哈的主人 养了条大黄

②byType

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" ><!-- 配置一个master对象 -->
<bean id="master" class="com.hsp.autowire.Master" autowire="byType">
<property name="name">
<value>二哈的主人</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog11"  class="com.hsp.autowire.Dog">
<property name="name" value="大黄"/>
<property name="age" value="3"/>
</bean>
</beans>

运行结果
2020-3-2 12:45:33 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Mon Mar 02 12:45:33 CST 2020]; root of context hierarchy
2020-3-2 12:45:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/hsp/autowire/beans.xml]
2020-3-2 12:45:34 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7
2020-3-2 12:45:34 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7: defining beans [master,dog11]; root of factory hierarchy
二哈的主人 养了条大黄

<bean id="master" class="com.hsp.autowire.Master" autowire="constructor">

package com.hsp.autowire;public class Master {private String name;private Dog dog;public Master(Dog dog){this.dog=dog;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
<!-- 配置一个master对象 -->
<bean id="master" class="com.hsp.autowire.Master" autowire="constructor">
<property name="name">
<value>二哈的主人</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog11"  class="com.hsp.autowire.Dog">
<property name="name" value="大黄"/>
<property name="age" value="3"/>
</bean>
</beans>

运行结果:
2020-3-2 12:47:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]; startup date [Mon Mar 02 12:47:49 CST 2020]; root of context hierarchy
2020-3-2 12:47:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/hsp/autowire/beans.xml]
2020-3-2 12:47:50 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ef9f1d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7
2020-3-2 12:47:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b8d6f7: defining beans [master,dog11]; root of factory hierarchy
二哈的主人 养了条大黄


spring教程笔记4相关推荐

  1. 动力节点—2020最新Spring教程笔记(上)

    文章目录 1 Spring 概述 2 IOC 控制反转 2.1 基于XML的DI 2.1.1 注入分类 2.1.1.1 set注入 2.1.1.2 构造注入(了解) 2.1.2 自动注入 2.2 基于 ...

  2. Java基础知识 廖雪峰教程笔记

    Java基础知识 Java基础知识 java简介 名词解释 运行Java程序 Java基本数据类型 模块 包装类型 记录类 异常处理 Java异常 使用Commons Logging 使用log4j ...

  3. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  4. Spring读书笔记——bean创建(下)

    有关Spring加载bean系列,今天这是最后一篇了,主要接上篇对于从Spring容器中获取Bean的一些细节实现的补充. <Spring读书笔记--bean加载>--Spring如何加载 ...

  5. Spring学习笔记(三) AOP_annotation,AOP_XML

    在学习课程以前,听说AOP有种很神秘的感觉,好像很好深的技术.其实原理很简单,使用动态代理的方式给程序增加逻辑.与此相似的有struts2中的filter拦截器. 再讲AOP之前先把需求说一下: 同S ...

  6. 台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix逆矩阵法)

    台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix) matrix left division左除:\ or mldivide() solvi ...

  7. 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)

    台湾国立大学郭彦甫Matlab教程笔记(21) today: linear equation 线性方程 linear system 线性系统 我们先看第一部分 linear equation 假定一个 ...

  8. 台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric)

    台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric) symbolic vs. numeric符号法和数值法的区别对比 symbolic 1)advantages ...

  9. 台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration

    台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration 数值积分 calculating the numerical value of a definite inte ...

  10. 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation

    台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numeric differentiation 复习:diff()函数用来计算vector前后 entry的差异 数值微分继续 various ...

最新文章

  1. 新视界,你好!_只愿与一人十指紧扣_新浪博客
  2. Java NIO之Selector(选择器)
  3. Emerged strategy 涌现战略
  4. 外挂学习之路(14)--- 游戏中的二叉树
  5. lwIP ARP协议分析
  6. (三)SpringMVC实现
  7. 做风控的你,GPS数据有没有这样用?
  8. [USACO08NOV]奶牛混合起来Mixed Up Cows
  9. 2014520420145212信息安全系统实验三报告
  10. 终端如何查看虚拟环境_Python版本管理工具和虚拟环境
  11. 关于腾讯云学生服务器搭建个人网站——配置web开发环境详细步骤
  12. 未能联接game center服务器,苹果game center无法连接服务器怎么办呢?
  13. 计算机操作系统学习(七)作业管理
  14. 各种表格扫描件OCR识别为电子表格的技术
  15. stm32f407zgt6与stm32f407vet6的通用io口差别
  16. 使用natapp实现内网穿透详细教程
  17. 电脑打不开浏览器--解决方法
  18. Salome_Meca 2021 安装教程(Centos)
  19. 钱多多第二阶段冲刺03
  20. SwitchHosts安装使用

热门文章

  1. javascript中replace的正则表达式语法
  2. mysql使用innodb需要注意的情况
  3. pooling层如何反向传播? 很简单
  4. 提升精度或者训练损失不继续下降办法汇总,(进一步提升效果)
  5. STL中的序列式容器——vector(向量)
  6. 交互设计中的“所见即所得”原则
  7. 从dist到es:发一个NPM库,我蜕了一层皮
  8. 导入虚拟机vmware,此主机支持Intel VT-x,但Intel VT-x处于禁用状态和黑屏
  9. 云计算的2.0进化体现?区块链分化处理能力掀全球去中心化热潮
  10. UI_UISlider控件