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. Python - 获取本机IP地址、Mac地址

    Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...

  2. 小鸟初学Shell编程(八)环境变量、预定义变量与位置变量

    环境变量 环境变量:每个Shell打开都可以获得到的变量. 我们知道通过export的方式打开可以让子进程读取父进程的变量的值,那怎么样才能让每一个进程都能读取到变量的值呢? 在这呢,系统有一些默认的 ...

  3. 用SignApk.jar对APK进行签名

    对apk签名需要使用SignApk.jar和签名文件.可以使用Android源码获取,若没有源码,可以在这下载:SignApk.jar.(包含了SignApk.jar和签名文件和批处理文件) 1 Si ...

  4. sparkSQL中的example学习(1)

    SparkSQLDemo.scala import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types ...

  5. centos6.8下hadoop3.1.1完全分布式安装指南

    前述:这篇文档是建立在三台虚拟机相互ping通,防火墙关闭,hosts文件修改,SSH 免密码登录,主机名修改等的基础上开始的. 一.传入文件 1.创建安装目录 mkdir /usr/local/so ...

  6. XGBoost 完整推导过程

    参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...

  7. k8s Ingress和ingress控制器

    ingress架构图简介 我们知道service的表现形式为IP:PORT,即工作在第四层传输层(TCP/IP层),那么对于不同的URL地址经常对应用不同的后端服务或者虚拟服务器,这些应用层的转发机制 ...

  8. Java数据库连接组件C3P0和DBCP

    C3P0连接池组件 开源数据库连接池组件 jar包:c3p0-0.9.2.jar和mchange-commons-java-0.2.2.3.jar 支持JDBC3规范和JDBC2的标准扩展 使用项目H ...

  9. WAF的部署方式——有直路部署和旁路部署

    随着电子商务.网上银行.电子政务的盛行,WEB服务器承载的业务价值越来越高,WEB服务器所面临的安全威胁也随之增大,因此,针对WEB应用层的防御成为必然趋势,WAF(WebApplicationFir ...

  10. JS高阶---继承模式(原型链继承)

    [前言] 之前已经介绍了对象创建的五种模式,下面看下继承模式 本节介绍下<原型链继承> [主体] 验证如下: 关键点: .