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. linux安装和使用zookeeper

    一.安装条件 想要安装zookeeper,必须先在linux中安装好jdk.安装步骤见: https://www.cnblogs.com/expiator/p/9987351.html 二.下载并解压 ...

  2. jenkins19年最新最管用的汉化

    今天准备学学jenkins ,官方下载了一个最新版本,发现是英文版,网上找个许多汉化方式,几乎都是一种,下载插件 :Locale plugin ....很尴尬,下载完了还是没有汉化 ,是不是jenki ...

  3. 浅析java线程和OS线程的关系

    探究java线程和OS线程之间的联系 一.准备工作 1.查看linux创建线程的方法    man pthread_create 根据man的配置可知,pthread_create会创建一个线程,这个 ...

  4. tomcat的基本应用

    1.JVM基本介绍 JAVA编译型 ---> 编译 C 编译型---> linux --->编译一次 windows --->编译一次 macos ubuntu 跨平台 移值型 ...

  5. jQuery 选择器有61种你都知道了多少

    下面列举了61种jQuery 选择器 参考 选择器 语句 描述 * $("*") 选择所有元素 #id $("#lastname") id=“lastname” ...

  6. CTF必备技能丨Linux Pwn入门教程——stack canary与绕过的思路

    Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...

  7. xenserver 添加和卸载硬盘

                最近在浪潮服务器上安了xenserver系统,创建虚拟机,没注意磁盘超负载就重启了服务导致各种坑,一言难尽,忧伤逆流成河啊,所以准备将各种操作整理总结记录下,持续更新ing~~ ...

  8. linux(05) 编译安装py3

    一.编译安装python3 https://www.cnblogs.com/pyyu/p/9015317.html 1.下载python3的源码 cd /opt yum install wget -y ...

  9. web-天下武功唯快不破

    没有武术是不可摧毁的,而最快的速度是获得长期成功的唯一途径.>>>>>> ----你必须尽可能快地做到这一点!---- <<<<<&l ...

  10. XML 配置文件,知识点

    namespace 属性:配置成接口的全限定名称,将 Mapper 接口和 XML 文件关联起来: select 标签的 id 属性值,对应定义的接口方法名. insert 标签的属性 paramet ...