spring教程笔记4
通过构造函数注入属性值
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相关推荐
- 动力节点—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 基于 ...
- Java基础知识 廖雪峰教程笔记
Java基础知识 Java基础知识 java简介 名词解释 运行Java程序 Java基本数据类型 模块 包装类型 记录类 异常处理 Java异常 使用Commons Logging 使用log4j ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- Spring读书笔记——bean创建(下)
有关Spring加载bean系列,今天这是最后一篇了,主要接上篇对于从Spring容器中获取Bean的一些细节实现的补充. <Spring读书笔记--bean加载>--Spring如何加载 ...
- Spring学习笔记(三) AOP_annotation,AOP_XML
在学习课程以前,听说AOP有种很神秘的感觉,好像很好深的技术.其实原理很简单,使用动态代理的方式给程序增加逻辑.与此相似的有struts2中的filter拦截器. 再讲AOP之前先把需求说一下: 同S ...
- 台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix逆矩阵法)
台湾国立大学郭彦甫Matlab教程笔记(22) Cramer's method(Inverse matrix) matrix left division左除:\ or mldivide() solvi ...
- 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)
台湾国立大学郭彦甫Matlab教程笔记(21) today: linear equation 线性方程 linear system 线性系统 我们先看第一部分 linear equation 假定一个 ...
- 台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric)
台湾国立大学郭彦甫Matlab教程笔记(20) root finding(numeric) symbolic vs. numeric符号法和数值法的区别对比 symbolic 1)advantages ...
- 台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration
台湾国立大学郭彦甫Matlab教程笔记(17)numerical integration 数值积分 calculating the numerical value of a definite inte ...
- 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation
台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numeric differentiation 复习:diff()函数用来计算vector前后 entry的差异 数值微分继续 various ...
最新文章
- 新视界,你好!_只愿与一人十指紧扣_新浪博客
- Java NIO之Selector(选择器)
- Emerged strategy 涌现战略
- 外挂学习之路(14)--- 游戏中的二叉树
- lwIP ARP协议分析
- (三)SpringMVC实现
- 做风控的你,GPS数据有没有这样用?
- [USACO08NOV]奶牛混合起来Mixed Up Cows
- 2014520420145212信息安全系统实验三报告
- 终端如何查看虚拟环境_Python版本管理工具和虚拟环境
- 关于腾讯云学生服务器搭建个人网站——配置web开发环境详细步骤
- 未能联接game center服务器,苹果game center无法连接服务器怎么办呢?
- 计算机操作系统学习(七)作业管理
- 各种表格扫描件OCR识别为电子表格的技术
- stm32f407zgt6与stm32f407vet6的通用io口差别
- 使用natapp实现内网穿透详细教程
- 电脑打不开浏览器--解决方法
- Salome_Meca 2021 安装教程(Centos)
- 钱多多第二阶段冲刺03
- SwitchHosts安装使用