【Spring】---属性注入
一、Spring注入属性(有参构造和【set方法】)

注意:在Spring框架中只支持set方法、有参构造方法这两种方法。
使用有参数构造方法注入属性(用的不多,但需要知道):
实体类
package com.tyzr.property;
public class PropertyDemo1 {
private String username;
public PropertyDemo1(String username) {
this.username = username;
}
public void test1(){
System.out.println("demo1-------------->"+username);
}
}
配置文件
<bean id="demo1" class="com.tyzr.property.PropertyDemo1">
<!-- 使用有参数注入 name就是属性名字 -->
<constructor-arg name="username" value="小小旺旺"></constructor-arg>
</bean>
测试类
@Test
public void testUser(){
//加载核心配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到我们配置的对象
//<bean id="user" class="com.tyzr.ioc.User"></bean>
PropertyDemo1 propertyDemo1 = (PropertyDemo1)context.getBean("demo1");
propertyDemo1.test1();
}
使用set方法注入属性(重点:这个方法用的最多):
实体类
public class Book {
private String bookName;
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void demobook(){
System.out.println("book-------->"+bookName);
}
}
配置文件
<bean id="demo2_book" class="com.tyzr.property.Book">
<!-- 注入属性值 -->
<property name="bookName" value="Java程序设计"></property>
</bean>
测试类
@Test
public void testUser(){
//加载核心配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到我们配置的对象
//<bean id="user" class="com.tyzr.ioc.User"></bean>
Book book = (Book)context.getBean("demo2_book");
book.demobook();
}
二、Spring注入对象类型属性(重点)
在工作中,action中要new Service,而Service中要new Dao。所以我们现在把new的过程如何实现。
场景:
创建Service和Dao,在Service中得到Dao对象。
实现过程:
1、在Service里面把dao作为类型属性
2、生成dao类型属性的set方法
public class UserSerivce {
//定义dao类型属性
private UserDao userDao;
//生成set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("serivce-------------");
//之前的做法
//UserDao userdao = new UserDao();
//userdao.add();
//现在我们要把上面这个new的过程交给spring处理
userDao.add();
}
}
3、注入对象属性
配置文件
<!-- 注入对象类型属性 -->
<!-- 1 配置service和dao对象 -->
<bean id="userdao" class="com.tyzr.ioc.UserDao"></bean>
<bean id="userService" class="com.tyzr.ioc.UserSerivce">
<!--
在这里注入dao对象
name:service类里面属性的名称
现在不能写value属性,上一个例子是字符串,现在是一个对象
需要写ref:值是dao配置bean标签id的值
-->
<property name="userDao" ref="userdao"></property>
</bean>
测试类
@Test
public void testUser(){
//加载核心配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到我们配置的对象
//<bean id="user" class="com.tyzr.ioc.User"></bean>
UserSerivce userSerivce = (UserSerivce)context.getBean("userService");
userSerivce.add();
}
三、Spring注入复杂数据
- 数据
- LIST
- MAP
- Properties类型
public class Person {
private String pname;
private String [] arrs;
private List<String> list;
private Map<String,String> map;
private Properties properties;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setPname(String pname) {
this.pname = pname;
}
public void test1(){
//System.out.println("pname="+pname);
System.out.println("arrs="+arrs);
System.out.println("list="+list);
System.out.println("map="+map);
System.out.println("properties="+properties);
}
}
配置文件
<bean id="person" class="com.tyzr.property.Person">
<!-- 数组 name:数组的对象名称 -->
<property name="arrs">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
<value>5</value>
</list>
</property>
<!-- List -->
<property name="list">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
<value>444</value>
<value>555</value>
</list>
</property>
<!-- Map -->
<property name="map">
<map>
<entry key="a" value="aa"></entry>
<entry key="b" value="bb"></entry>
<entry key="c" value="cc"></entry>
<entry key="d" value="dd"></entry>
</map>
</property>
<!-- properties -->
<property name="properties">
<props>
<prop key="jdbcdirver">com.mysql.jdbc.Driver</prop>
<prop key="jdbcurl">jdbc:mysql:///test</prop>
</props>
</property>
</bean>
【Spring】---属性注入的更多相关文章
- Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用
Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...
- Spring 属性注入(二)BeanWrapper 结构
Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...
- Spring 属性注入(三)AbstractNestablePropertyAccessor
Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...
- Spring 属性注入(四)属性键值对 - PropertyValue
Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...
- spring 属性注入
Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
- spring属性注入
1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...
- java spring属性注入
一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法 用constr ...
- spring属性注入DI
spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...
随机推荐
- $.getJSON同步和异步
$.ajaxSettings.async = false; $.getJSON(url, data, function(data){ }); $.getJSON(url, data, function ...
- 彻底解决 TypeScript 报错:“无法重新声明块范围变量”的问题
背景 当使用 TypeScript + TSlint + Babel + Jest 搭建开发环境时,在开发过程中偶尔会被 IDE 提示「无法重新声明块范围变量」,从而导致编译出错,报错图示如下: 相关 ...
- 上传图片,正在加载,loading
https://blog.csdn.net/yansong_8686/article/details/50361573
- JDK的二进制安装
JDK的二进制安装步骤 1. Jdk1.8二进制包下载路径http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- 费用流 Dijkstra 原始对偶方法(primal-dual method)
简单叙述用Dijkstra求费用流 Dijkstra不能求有负权边的最短路. 类似于Johnson算法,我们也可以设计一个势函数,以满足在与原图等价的新图中的边权非负. 但是这个算法并不能处理有负圈的 ...
- docker python3环境搭建
1.使用镜像为daocloud的python镜像 docker run -it --rm --name my-running-script -v "$PWD":/usr/src/m ...
- CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)
链接: https://vjudge.net/problem/CodeForces-721D 题意: Recently Maxim has found an array of n integers, ...
- 【ZJOJ5186】【NOIP2017提高组模拟6.30】tty's home
题目 分析 如果直接求方案数很麻烦. 但是,我们可以反过来做:先求出所有的方案数,在减去不包含的方案数. 由于所有的路径连在一起, 于是\(设f[i]表示以i为根的子树中,连接到i的方案数\) 则\( ...
- 在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了
在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了 解决方法: 1.运行:输入services.msc进入服务 2.找到(前提你的P ...
- 一张图解释Linux文件系统中硬链接和软链接的区别
如图所示,硬链接与原始文件共用一个inode,但inode是不跨文件系统的(Ext3.Ext4),每个文件系统都有自己的inode列表.因此,硬链接是没办法跨文件系统的 而软链接不同,软链接相当于重新 ...