一、Bean实例化的三种方式:

(1)使用类的无参构造创建

(2)使用静态工厂创建

(3)使用实例工厂创建

代码实例:

(1)项目结构:

(2)在pom.xml中导入spring的核心jar包依赖:

(3)applicationContext.xml的配置:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 1.使用类的无参构造函数创建 --><bean id="user" class="com.zwp.domain.User"></bean><!-- 2.使用静态工厂进行创建 --><!-- class的值不是写User对象的全路径,而是写静态工厂的全路径 --><!-- factory-method的值写要调用的方法 --><bean id="user2" class="com.zwp.domain.StaticFactory" factory-method="getUser"></bean><!-- 3.使用实例工厂进行创建 --><!-- 需要先创建beanFactory对象,再通过beanFactory对象进行调用 --><bean id="beanFactory" class="com.zwp.domain.BeanFactory"></bean><bean id="user3" factory-bean="beanFactory" factory-method="getUser"></bean>
</beans>

(4)domain类的代码:

public class User {public void add(){System.out.println("创建了一个User对象.....");}
}
//静态工厂调用:
public class StaticFactory {//静态的方法,返回User对象:public static User getUser(){return new User();}
}
//实例工厂
public class BeanFactory {//普通的方法,返回User对象//不能通过类名调用,需要通过对象调用。public User getUser(){return new User();}
}

(5)测试类:

public class Test1 {@Testpublic void test(){//1.加载spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到无参构造函数创建的对象:User user =(User) context.getBean("user");//得到静态工厂创建的对象:User user2 =(User) context.getBean("user2");//得到实例工厂创建的对象:User user3=(User) context.getBean("user3");System.out.println("无参构造函数创建的对象:"+user);System.out.println("静态工厂创建的对象:"+user2);System.out.println("实例工厂创建的对象:"+user3);}
}

(6)测试结果:每种方式都创建了一个对象

二、Bean的属性注入:

1、属性注入的三种方式:(spring里面只支持前两种)

(1)使用有参构造注入

(2)使用set()方法注入

(3)使用接口注入

2、代码测试:

(1)applicationContext.xml的配置

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 属性注入的方式:start --><!-- 1.有参构造属性注入 --><bean id="construct" class="com.zwp.domain.Book1"><constructor-arg name="bookname" value="这是Book1的name"></constructor-arg></bean><!-- 2.set方法属性注入 --><bean id="setproperty" class="com.zwp.domain.Book2"><property name="bookname" value="这是Book2的name"></property></bean><!-- 属性注入的方式:end -->
</beans>

(2)domain类的代码:

//有参构造注入:
public class Book1 {private String bookname;public Book1(String bookname) {this.bookname = bookname;}public void text(){System.out.println("有参构造注入:"+bookname);}
}
//set方法注入属性:
public class Book2 {private String bookname;public void setBookname(String bookname) {this.bookname = bookname;}public void text(){System.out.println("set方法注入属性:"+bookname);}
}

(3)测试类:

public class Test2 {@Testpublic void test(){//1.加载spring配置文件,ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");//2.得到加载的对象Book1 book1 = (Book1) context.getBean("construct");Book2 book2 = (Book2) context.getBean("setproperty");book1.text();book2.text();}
}

(4)输出结果:

三、对象注入:

(1)set()方法注入;

(2)构造器注入:①通过index设置参数的位置;②通过type设置参数类型;

(3)静态工厂注入;

(4)实例工厂;

详细内容请参考这篇文章:Spring中bean的注入方式

四、复杂注入:数组、list集合、map集合、properties

(1)相关类代码:

//复杂类型属性注入:
public class ComplexType {//第一步:private String[] arrs;private List<String> list;private Map<String,String> map;private Properties properties;//第二步:set方法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 show(){System.out.println("arrs:"+arrs);System.out.println("list:"+list);System.out.println("map:"+map);System.out.println("properties:"+properties);}
}

(2)applicationContext.xml文件的配置

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 复杂属性的注入 --><bean id="complextype" class="com.zwp.object.ComplexType"><!-- 1.数组 --><property name="arrs"><list><value>arr1</value><value>arr2</value><value>arr3</value></list></property><!-- 2.list集合 --><property name="list"><list><value>list1</value><value>list2</value><value>list3</value></list></property><!-- 3.map集合 --><property name="map"><map><entry key="1" value="map1"></entry><entry key="2" value="map2"></entry><entry key="3" value="map3"></entry></map>      </property><!-- 4.properties --><property name="properties"><props><prop key="a">properties-a</prop><prop key="b">properties-b</prop><prop key="c">properties-c</prop></props></property></bean>
</beans>

(3)测试类

public class Test2 {@Testpublic void test3(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");ComplexType complexType = (ComplexType) context.getBean("complextype");complexType.show();}
}

(4)运行结果:

附:项目结构

Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)相关推荐

  1. Spring框架学习笔记09:基于XML配置方式搭建SSM框架实现用户登录

    文章目录 一.采用MVC架构 二.用户登录运行效果 三.基于XML配置方式搭建SSM框架实现用户登录 (一)创建数据库与表 - simonshop (t_user) 1.创建数据库 - simonsh ...

  2. Spring框架学习笔记07:基于XML配置方式使用Spring MVC

    文章目录 一.Spring MVC概述 1.MVC架构 2.Spring MVC 3.使用Spring MVC的两种方式 二.基于XML配置与注解的方式使用Spring MVC (一)创建Spring ...

  3. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculato ...

  4. Spring框架学习笔记10:基于XML配置方式SSM框架西蒙购物网

    文章目录 一.网站功能需求 二.网站设计思路 1.设计模式 2.网站前台 3.网站后台 4.购物流程图 三.网站运行效果 四.网站实现步骤 (一)创建数据库与表 1.创建数据库 - simonshop ...

  5. Spring框架中集合属性为对象的注入方法

    Spring框架中集合属性为对象的注入方法 前言 创建基础类 创建`Course`类 编写XML配置文件 创建测试类 执行结果 前言 在集合的属性注入中,如果注入属性为普通类型(String.int) ...

  6. 实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293

    实例化Bean的方法(基于xml配置) 标签: spring framework 2015-09-01 13:43 918人阅读 评论(0) 收藏 举报  分类: Spring FrameWork(7 ...

  7. Spring中Bean创建完成后执行指定代码的几种实现方式

    Spring中Bean创建完成后执行指定代码的几种实现方式 1. 实现ApplicationListener接口 2. 实现InitializingBean接口 3. 使用@PostConstruct ...

  8. Spring-IOC—基于XML配置Bean

    Spring-IOC-基于XML配置Bean 1.Spring 配置/管理 bean 介绍 1.Bean 管理包括两方面 1.创建bean对象 2.给bean注入属性 2.Bean配置方式 1.基于x ...

  9. SSM框架笔记06:初探Spring——采用XML配置方式

    初探Spring--采用XML配置方式   Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓解决方案就是可以基于Spring解决Java EE开发的所有问题.Spring框架主要提供了I ...

最新文章

  1. 一代偶像终将老去,总有AI正在年轻
  2. BZOJ4350: 括号序列再战猪猪侠
  3. vim替换某行到某行的命令方法
  4. 做事情的价值和看待方式
  5. centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复...
  6. Hive体系结构(一)架构与基本组成
  7. CRM, C4C和SAP Hybris的数据库层设计
  8. 计算机组成与架构综述学习报告
  9. php的正则表达式函数,php中常用的正则表达式函数
  10. apache ignite_从In Memory Data Grid,Apache Ignite快速入门
  11. PL/SQL Developer中文版下载以及使用图解(绿色版)
  12. rabbit和mysql事务_分布式事务原理及SpringBoot整合RabbitMQ实现可靠事件,TCC事务模型及接口幂等性...
  13. WEB编程学习之Windows安装运行Tomcat
  14. android 9 patch
  15. shell waite php,linux shell wait命令详解
  16. sysstat工具包提供的主要命令
  17. 批量替换文件字体,简体-繁体
  18. Redis的incr命令引发的反序列化异常和ERR value is not an integer or out of range异常
  19. python 切片器_excel和python中的切片器列表
  20. 高斯消元法(matlab)

热门文章

  1. tornado连接数据库
  2. 一块V100运行上千个智能体、数千个环境,这个曲率引擎框架实现RL百倍提速
  3. 慕尼黑工业大学最新综述:深度神经网络中的不确定性
  4. 推荐3个C++系统项目!初级开发者必学!
  5. AAAI 2020 开源论文 | 一种针对图嵌入模型的受限黑盒对抗攻击框架
  6. 和49支战队瓜分600万奖金,这场史上奖金最多的AI大赛到底比什么?
  7. 超详细解读:神经语义解析的结构化表示学习 | 附代码分析
  8. 【腾讯面试题】SQL语句优化方法有哪些?
  9. 51Nod1079 中国剩余定理
  10. SpringBoot 配置 generator代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)