Spring_用Spring容器创建对象并给属性赋值
创建spring配置文件:spring-config.xml。对象的创建和赋值都在这里进行配置。
创建实体类,设置属性
import java.util.List;
import java.util.Map; public class BookBean { private Long bookId;
private String bookName;
private Float price;
private List<String> fzb;
private Map<String,String> fields;
private CategoryBean categoryBean; //引用另外一个实体类
//注意:
//如果有构造方法,创建对象时配置文件则会你读重写的构造的方法,如果用setter方法赋值,则必须要重载一个空的构造方法。
public BookBean() {
}
public BookBean(Long bookId, String bookName, Float price) {
this.bookId = bookId;
this.bookName = bookName;
this.price = price;
}
}
1.创建单例对象:在整个项目运行期间,这个对象只有一个。
<bean class="com.lanou.demo.bean.BookBean" id="book1"/>
2.创建多例对象:每次取出的对象都是一个新的对象,
<bean class="com.lanou.demo.bean.BookBean" id="book1" scope="prototype"/>
3.创建对象是为对象属性赋值,前提时属性必须要有setter方法
<bean class="com.lanou.demo.bean.BookBean" id="book2">
<property name="bookId" value="1"/>
<property name="bookName" value="西游记"/>
<property name="price" value="34.4"/>
<!--为对象中的集合类型的属性赋值-->
<property name="fzb">
<list>
<value>李忠仁</value>
<value>马琳</value>
<value>陈颖媛</value>
</list>
</property>
<!--为对象中的map类型的属性赋值: -->
<!--第一种方式:-->
<property name="fields">
<map>
<entry key="1" value="陈新元的诞生"/>
<entry key="2" value="陈新元的灭亡"/>
</map>
</property>
<!--第二种方式:-->
<property name="fields">
<props>
<prop key="1">陈新元的复活</prop>
<prop key="1">陈新元的崛起</prop>
</props>
</property>
<!--在一个bean中引用另外一个bean的对象,使用ref赋值-->
<property name="categoryBean" ref="category1"/>
</bean>
4.用构造方法给属性赋值。
<bean class="com.lanou.demo.bean.BookBean" id="book3">
<!--为构造方法中bookId这个参数赋值-->
<constructor-arg name="bookId" value="2"/>
<constructor-arg name="bookName" value="西游记"/>
<constructor-arg name="price" value="45.5"/>
</bean>
Spring_用Spring容器创建对象并给属性赋值的更多相关文章
- spring(四):spring中给bean的属性赋值
spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...
- Spring注解 - 生命周期、属性赋值、自动装配
一.Bean的生命周期 流程 Bean创建 -- 初始化 -- 销毁 创建: 单实例:在容器启动时创建对象 多实例:每次调用时创建对象 初始化: 都是在对象创建完成后,调用初始化方法 销毁: 单实例: ...
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- Spring配置Bean,为属性赋值
SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...
- spring容器IOC创建对象<二>
问题?spring是如何创建对象的?什么时候创建对象?有几种创建方式?测试对象是单例的还是多例的 ?对象的初始化和销毁? 下面的四大模块IOC的内容了!需要深刻理解 SpringIOC定义:把对象的创 ...
- Spring的核心之IoC容器创建对象
Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...
- Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)
目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...
- Spring IoC 属性赋值阶段
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 容 ...
- 使用Spring容器动态注册和获取Bean
有时候需要在运行时动态注册Bean到Spring容器,并根据名称获取注册的Bean.比如我们自己的SAAS架构的系统需要调用ThingsBoard API和Thingsboard交互,就可以通过Thi ...
随机推荐
- thinkphp url大小写
系统默认的规范是根据URL里面的模块名.控制器名来定位到具体的控制器类的,从而执行控制器类的操作方法. 以URL访问 http://serverName/index.php/Home/Index/in ...
- bsgs+求数列通项——bzoj3122(进阶指南模板该进)
/* 已知递推数列 F[i]=a*F[i-1]+b (%c) 解方程F[x]=t an+1 = b*an + c an+1 + c/(b-1) = b(an + c/(b-1)) an+1 + c/( ...
- html5 js 监听网络在线与离线
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- (转)ActiveMQ 使用场景
转:http://306963591.iteye.com/blog/1044166 ActiveMQ 安装测试就不做介绍了,下面我说说ActiveMQ 使用场景. 1.非均匀应用集成 ActiveMQ ...
- Linux unzip解压多个文件
假设当前目录下有多个zip文件 data.zip invoices.zip pictures.zip visit.zip, 直接 unzip *.zip 等价于 unzip data.zip invo ...
- Web开发-Servlet&HTTP&Request
<!doctype html>02 - JavaEE - Servlet&HTTP&Request figure:first-child { margin-top: -20 ...
- MapReduce的体系结构
- Linux 下 Nand Flash 调用关系
Nand Flash 设备添加时数据结构包含关系 struct mtd_partition partition_info[] --> struct s3c2410_nand_set ...
- art-template官方文档
http://aui.github.io/art-template/zh-cn/docs/
- mysql主从跳过错误
mysql主从复制,经常会遇到错误而导致slave端复制中断,这个时候一般就需要人工干预,跳过错误才能继续 跳过错误有两种方式: 1.跳过指定数量的事务 mysql>stop slave; m ...