【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 ...
随机推荐
- ceph分布式存储简介
一.Ceph简介: 什么是分布式存储? 我在一个环境当中,有很多很多的服务器,服务器上也有它自己很多的硬盘,我通过软件的形式把若干服务器都收集起来,部署成一个软件,在这个逻辑的软件里可以同时看到我若干 ...
- spring中spEL常用应用场景
spEL表达式表示:#{} 一.基本类型值运算操作 {}可以放置数字,字符串,布尔型,表达式(运算,正则,逻辑).这个应用场景和EL表达式一样的,实际中用的也不多. 注意:在XML中表示==,> ...
- Linux日常之命令sed
一. 命令sed简介 利用命令sed能够同时处理多个文件多行的内容,可以不对原文件改动,仅把匹配的内容显示在屏幕上,也可以对原文件进行改动,但是不会在屏幕上返回结果,若想查看改动后的文件,可以使用命令 ...
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...
- robotframework调用外部python多次运行拿到的都是同一个值
外部python是一个爬虫,爬取的内容的定义没有放入函数中.导致一次爬取多次使用的情况出现. 第一版函数如下: 改版后:
- map1
map2.insert(make_pair("sale", 1)); map2.count("development");//key为development的数 ...
- Java基本的程序结构设计 数组
声明数组: int[] a; int a[]; 两种方式.一般选择第一种,比较一目了然. 初始化,必须指定长度: int[] a = new int[10]; int[] a = {1,2,3}; 初 ...
- electron-vue 图片加载失败后使用默认头像
<img :src="item.headUrl" alt="" class="contact-head" :onerror=" ...
- 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...
- jackson 完整Jar包
Jackson fasterxml和codehaus的区别: 他们是Jackson的两大分支.也是两个版本的不同包名.Jackson从2.0开始改用新的包名fasterxml: 1.x版本的包名是co ...