title: 【初识Spring】对象(Bean)实例化及属性注入(xml方式)

date: 2018-08-29 17:35:15

tags: [Java,Web,Spring]

#初识Spring之Bean实例化及属性注入

1.通过xml配置的方式进行实例化。

  • 配置文件中bean标签的常用属性
  • 通过无参构造(常用)
  • 使用静态工厂进行创建
  • 使用实例工厂进行创建

2.属性注入。

  • 使用有参数构造方法注入属性
  • 使用set方法注入属性(常用)
  • 注入对象类型属性
  • 注入复杂类型属性

xml配置的方式进行实例化

  • 配置文件中bean标签的属性

(1)id属性:起名称,id属性值名称任意命名

  • id属性值,不能包含特殊符号
  • 根据id值得到配置对象

(2)class属性:创建对象所在类的全路径

(3)name属性:功能和id属性一样的,id属性值不能包含特殊符号,但是在name属性值里面可以包含特殊符号

(4)scope属性

  • singleton:默认值,单例

  • prototype:多例

  • 无参构造实例化对象

//phone类:
package com.test.vo;
public class Phone {
public void printTest() {
System.out.print("Phone.......");
}
}
<!--applicationContext.xml配置文件-->
<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="phone" class="com.test.vo.Phone"></bean>
</beans>
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
//加载配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到配置创建的对象
Phone Phone = (Phone) context.getBean("phone");
//调用对象方法
Phone.printTest();
}
}

注:java类中默认有无参构造方法,若类中已声明了有参构造,则需手动声明无参构造方法。

  • 使用静态工厂进行创建
//静态工厂类
package com.test.utils;
import com.test.vo.Phone; public class BeanFactory {
//静态方法,返回Phone对象
public static Phone getPhone() {
return new Phone();
} }
//创建的对象为Phone类对象不变
//配置文件改为:
<!--applicationContext.xml配置文件-->
<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class为静态工厂的路径,factory-method为工厂的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone"></bean>
</beans>
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}
  • 使用实例工厂进行创建
//实列工厂类:
import com.test.vo.Phone; public class BeanUFactory {
//普通方法,返回Phone对象
public Phone getPhone() {
return new Phone();
}
}
	配置文件修改:
<!-- 1.先创建工厂对象 -->
<!-- 2.再创建Phone对象 -->
<bean id="BeanUFactory" class="com.test.utils.BeanUFactory"></bean>
<bean id="phoneUFa" factory-bean="BeanUFactory" factory-method="getPhone"></bean>
//测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneUFa");
Phone.printTest();
}
}

属性注入

  • 使用有参数构造方法注入属性:

Phone类改写为:

public class Phone {
private String name;
//显示声明无参构造
public Phone() {}
//有参构造
public Phone(String name) {
this.name=name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}

applicationContext.xml配置文件修改为:

<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class为静态工厂的路径,factory-method为工厂的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
<!--name为构造方法的参数名,value为要将其设置的值-->
<constructor-arg name="name" value="诺基亚"></constructor-arg>
</bean>
</beans>

测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}

结果:

诺基亚Phone.......
  • 使用set方法注入属性:

Phone类改写为:

public class Phone {
private String name;
//set方法
public void setName(String name) {
this.name = name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}

applicationContext.xml配置文件修改为:

<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class为静态工厂的路径,factory-method为工厂的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
<!--name为要注入的属性的名称,value为要将其设置的值-->
<property name="name" value="三星"></property>
</bean>
</beans>

结果:

三星Phone.......
  • 注入对象类型属性

新建Person类:

public class Person {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}

Phone类修改为:

package com.test.vo;

public class Phone {
private String name;
private Person person; //set方法
public void setName(String name) {
this.name = name;
} public void setPerson(Person person) {
this.person = person;
} public void printTest() {
System.out.print(person.getName()+"::"+person.getAge()+"::"+person.getSex());
}
}

配置文件作如下修改:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.test.vo.Person" scope="prototype">
<property name="name" value="小王"></property>
<property name="sex" value="man"></property>
<property name="age" value="11"></property>
</bean>
<bean id="phone" class="com.test.vo.Phone">
<!-- 因注入的是对象写ref属性 -->
<property name="person" ref="person"></property>
</bean>
</beans>

测试方法不变,结果为:

小王::11::man
  • 注入复杂类型属性

Phone类修改为:

package com.test.vo;

import java.util.Arrays;
import java.util.List;
import java.util.Map; public class Phone {
private String arr[];
private List<Integer> list;
private Map<String,String> map; //set方法
public void setArr(String[] arr) {
this.arr = arr;
} public void setList(List<Integer> list) {
this.list = list;
} public void setMap(Map<String, String> map) {
this.map = map;
}
public void printTest() {
System.out.println("arr:"+Arrays.toString(arr));
System.out.println("list:"+list);
System.out.println("map:"+map);
}
}

配置文件作如下修改:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="phone" class="com.test.vo.Phone">
<!-- 数组 -->
<property name="arr">
<list>
<value>小米</value>
<value>中兴</value>
<value>华为</value>
</list>
</property>
<!-- list集合 -->
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="bob"></entry>
<entry key="cc" value="jerry"></entry>
</map>
</property>
</bean>
</beans>

结果如下:

arr:[小米, 中兴, 华为]
list:[1, 2, 3]
map:{aa=lucy, bb=bob, cc=jerry}

【初识Spring】对象(Bean)实例化及属性注入(xml方式)的更多相关文章

  1. spring中bean实例化时机以及整个运转方式

    接上一篇文章,一般在servlet获取到请求之后 在service方法中就可以完成所有的请求处理以及返回,但是我们会采用更高级的MVC框架来做.也就是说所有的MVC框架入口就是serlvet中的ser ...

  2. 【初识Spring】对象(Bean)实例化及属性注入(注解方式)

    通过xml的方式进行对象的实列化或属性注入或许有一些繁琐,所以在开发中常用的方式更多是通过注解的方式实现对象实例化和属性注入的. 开始之前 1.导入相关的包(除了导入基本的包还要导入aop的包): 创 ...

  3. 这一次搞懂Spring的Bean实例化原理

    文章目录 前言 正文 环境准备 两个重要的Processor 注册BeanPostProcessor对象 Bean对象的创建 createBeanInstance addSingletonFactor ...

  4. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

  5. Spring中bean的四种注入方式

    一.前言   最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...

  6. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  7. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  8. Spring IoC(一)bean实例化和依赖注入

    1.IoC容器概述 IoC 全称为 Inversion of Control,翻译为 “控制反转”,它还有一个别名为 DI(Dependency Injection),即依赖注入. 所谓 IOC ,就 ...

  9. Spring中bean实例化的三种方式

    之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...

随机推荐

  1. win7个性化不能换界面:此页面上的一个或多个设置已被系统管理员禁用,关机里的切换用户和锁定为灰色

    win7个性化不能换界面:此页面上的一个或多个设置已被系统管理员禁用,关机里的切换用户和锁定为灰色 找到注册表 cmd-regedit HKEY_CURRENT_USER\Software\Micro ...

  2. c# 第29节 类

    本节内容: 1:类是什么 2:声明类 3:类的使用 1:类是什么 2:声明类 在生产上的声明:如下操作   或者快捷操作 ctrl+shift+a 键 出现如下界面: 3:类的使用 using Sys ...

  3. 【Ribbon篇四】Ribbon介绍(1)

    Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法, ...

  4. 20191031 Codeforces Round #539 (Div. 1) - Virtual Participation

    这场怎么全是数据结构题...

  5. Koa 中间件的执行顺序

    中间件工作原理 初始化koa实例后,我们会用use方法来加载中间件(middleware),会有一个数组来存储中间件,use调用顺序会决定中间件的执行顺序. 每个中间件都是一个函数(不是函数将报错), ...

  6. Java调试平台体系JPDA

    Java 平台调试体系(Java Platform Debugger Architecture,JPDA)定义了一个完整独立的体系,它由三个相对独立的层次共同组成,而且规定了它们三者之间的交互方式,或 ...

  7. CodeForces - 545CWoodcutters

    传送门 题目大意:n棵树(10^5),坐标xi,高度hi,把这棵树砍到,可以向右倒[xi,xi+hi]被占, 向左倒[xi-hi,xi]被占,必须要倒的坐标没有被占才能倒,不砍倒就xi被占,问最多砍几 ...

  8. CSP-S 2019 AFO记

    DAY -1 上午并没有改出题,然而调出了动态$dp$,于是顺便$AC$了保卫王国,于是就很愉悦. 下午考前自闭赛,把会的题和原题写了出来,然后就$rank1$了,感觉自己$rp--$. 晚上发现$T ...

  9. 国内Java面试总是问StringBuffer,StringBuilder区别是啥?档次为什么这么低?

    GitHub 6.6k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 6.6k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 6.6k Star 的 ...

  10. Java连载1-概述&常用的dos命令

    本想写完那两个再开始新的,然而客观条件不允许,之前从未接触过Java,从零开始吧​!!! 一.概述 C盘下​:programme file 一般为64位程序安装的目录,programme file(X ...