1.构造方法注入

1.bean类
public class User {
private String name;
private Integer age;
private Cat cat;
public User(String name,Integer age,Cat cat){
this.name=name;
this.age=age;
this.cat=cat;
} @Override
public String toString() {
return "User"+"name"+name+"age"+age+"cat"+cat.getName();
}
} 2.测试方法
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User)applicationContext.getBean("user");
System.out.println(user);
} 3.xml文件配置
<!--构造方法 注入-->
<bean id="user" class="com.imooc.ioc.demo4.User">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="13"></constructor-arg>
<constructor-arg name="cat" ref="cat"></constructor-arg>
</bean>
<bean id="cat" class="com.imooc.ioc.demo4.Cat">
<property name="name" value="cat"></property>
</bean>

2.set方法注入

1.bean类

public class Person {
private String name;
private Integer age;
private Cat cat; public void setCat(Cat cat) {
this.cat = cat;
} public Cat getCat() {
return cat;
} public void setName(String name) {
this.name = name;
} public void setAge(Integer age) {
this.age = age;
} public String getName() {
return name;
} public Integer getAge() {
return age;
} @Override
public String toString() {
return "name"+name+"age"+age+"cat"+cat.getName();
}
} 2.测试方法
public void demo2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person)applicationContext.getBean("person1");
System.out.println(person);
}
3.xml文件配置
<!--set方法注入-->
<bean id="person1" class="com.imooc.ioc.demo4.Person">
<property name="age" value="12"></property>
<property name="name" value="李四"></property>
<property name="cat" ref="cat"></property>
</bean>

3.复杂类型的注入

数组  List   set Map  Properties

package com.imooc.ioc.demo5;

import java.util.*;

/*
*
*
* 复杂类型注入*/
public class CollectionBean {
private String[] args; //数组类型
private List<String> list;//list集合
private Set<String> set ;//set集合
private Map<String,Integer> map;
private Properties properties; public List<String> getList() {
return list;
} public Map<String, Integer> getMap() {
return map;
} public Properties getProperties() {
return properties;
} public Set<String> getSet() {
return set;
} public String[] getArgs() {
return args;
} public void setList(List<String> list) {
this.list = list;
} public void setMap(Map<String, Integer> map) {
this.map = map;
} public void setProperties(Properties properties) {
this.properties = properties;
} public void setSet(Set<String> set) {
this.set = set;
} public void setArgs(String[] args) {
this.args = args;
} @Override
public String toString() {
return "arrs"+ Arrays.toString(args)+"list"+list+"map"+map+"properties"+properties+"set"+set;
}
} 2.测试方法
public  void  demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean=(CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
3.配置文件
<bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean" >
<!--数组的属性注入-->
<property name="args">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--list的属性注入-->
<property name="list">
<list>
<value>111</value>
<value>333</value>
<value>222</value>
</list>
</property>
<!--set集合的属性注入-->
<property name="set">
<set>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</set>
</property>
<!--map的属性注入-->
<property name="map">
<map>
<entry key="aaa" value="111"></entry>
<entry key="bbb" value="222"></entry>
</map>
</property>
<!--properties的属性注入-->
<property name="properties">
<props>
<prop key="username">zhangsan</prop>
</props>
</property>
</bean>
 

Spring bean注入的更多相关文章

  1. [spring]Bean注入——在XML中配置

    Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...

  2. Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html  Spring bean提供了3中注入方式:属 ...

  3. spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)

    这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...

  4. spring bean 注入

    概念 http://developer.51cto.com/art/200610/33311.htm http://kb.cnblogs.com/page/45266/ ==https://www.c ...

  5. Spring Bean 注入 2 注解篇

    1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...

  6. Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配

    1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...

  7. [spring]Bean注入——使用注解代替xml配置

    使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...

  8. Spring JMX之一:使用JMX管理Spring Bean

    spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...

  9. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

随机推荐

  1. PIE调用Python获得彩色直方图

    前段时间我一直在研究PIE SDK与Python的结合,因为在我的开发中,我想获取一张图片的统计直方图,虽然在SDK中有提供关于直方图的类接口(如IStatsHistogram 接口.Histogra ...

  2. Jar 打包与执行

    Java学习笔记之一,用于个人记录.整理自<Head First Java>. 假设有如下目录结构: 程序入口在 Jukebox8.java.这个代码文件开头是有如下这样的包声明语句的: ...

  3. Java生鲜电商平台-会员积分系统的设计与架构

    Java生鲜电商平台-会员积分系统的设计与架构 说明:互联网平台积分体系主要用于激励和回馈用户在平台的消费行为和活动行为,一个良好的积分体系可以很好的提升用户的粘性及活跃度. 一.互联网平台积分体系设 ...

  4. vs2015 创建MVC项目

    直接上图吧! 第一步:新建项目 第二步:选择模板 第三步:系统自动生成项目文件 第四步:创建控制器(C):找到Controllers文件夹->右键->添加->控制器 第五步:添加控制 ...

  5. 新mac 下第一次 安装 mongodb 步骤

    新入手mac,安装mongo步骤记录:不建议使用网上的brew安装方法,因为试了半天没有成功,应该是新版本限制比较多! 从mongodb官网下载mac版本mongo: 1.访问MongoDB官方下载地 ...

  6. 在Angular4中使用ng2-baidu-map详解

    一.引言 之前在Angular4使用过百度地图,记录一下踩过的坑 二.实现 1.安装 npm install angular2-baidu-map 2.在app.module.ts配置 ak key在 ...

  7. FCC-学习笔记 Boo who

    FCC-学习笔记  Boo who 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp.cn/;英文版的地 ...

  8. Redis入门学习(一):简介

    Redis是一个开源的.高性能的.基于键值对的缓存与存储系统,通过提供多种键值数据类型来适应不同场景下的缓存与存储需求.同时Redis的诸多高层级功能使其可以胜任消息队列.任务队列等不同的角色. 20 ...

  9. Linux磁盘信息查询及删除文件操作

    查询磁盘容量 $df -hl 删除文件固定行数 (1)删除第一行 $sed -i '1d' a.txt (2)删除指定行数 $sed -i '1,100d' a.txt 删除末尾行 $sed -i ' ...

  10. Python元组与字符串操作(10)——冒泡法

    冒泡法 属于交换排序,元素两两比较大小,交换位置,结果可升序或降序排列 nums = [2,5,1,6,7,9,8,3,4] for i in range(len(nums)): ##计数器0~8 f ...