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. GitFirstRemote

    1.$ git ls-remote From git@github.com:Smoothfu/WPFITEMSSOURCEPRODUCTCOLLECTION.git9a6669a2e2c9e22b30 ...

  2. Blazor应用程序基于角色的授权

    原文:https://chrissainty.com/securing-your-blazor-apps-configuring-role-based-authorization-with-clien ...

  3. laravel Method Illuminate\Validation\Validator::validateReuqired does not exist.

    Method Illuminate\Validation\Validator::validateReuqired does not exist. 此错误是由于我们在配置验证时,写错了关键字, publ ...

  4. IIS创建文件服务器(WebDAV)

    1.安装IIS,选择安装WEBDAV组件.然后新建站点,站点目录不需要额外设置任何权限 安装完成后组件: 2.配置WebDAV: 添加创作规则:允许某用户写入,其他所有用户读取.(写入规则必须要放在第 ...

  5. DRF简易了解

    Drf框架 一丶API接口 # 为了在团队内部形成共识.防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实现规范,而且这种规范能够让后端写的接口,用途一目了然,减少双方之间的合作成本. ...

  6. 在React中使用react-router-dom路由

    1,路由组件的基本实现 使用React构建的单页面应用,要想实现页面间的跳转,首先想到的就是使用路由.在React中,常用的有两个包可以实现这个需求,那就是react-router和react-rou ...

  7. 困扰了2天的问题,终于解决了。VB6的MSComCtl.ocx在32位Win7显示对象库未注册

    解决方案在这里,中文的资料真的挺垃圾的.(重启几次之后又不行了....怎么回事???) 安装.net framework4.0以上版本, C:\Windows\System32, C:\Windows ...

  8. js处理日历

    我们在做自动化的时候可能会遇到选择日期这种情况 这个时候我们可能就会想到直接定位不就可以了,为啥还要使用js这种东西呢? 首先,我们想一下定位:定位不仅麻烦而且还不稳定,所以这种方式我是直接就弃用了 ...

  9. django rest framework 过滤 lim分页

    一.过滤 1.首先引用diango 自带的过滤配置 2.导入模块 from django_filters.rest_framework import DjangoFilterBackend from ...

  10. vue-cli 3 和 vue-cli 2的区别

    分享文章: 浅谈vue-cli 3 和 vue-cli 2的区别 https://blog.csdn.net/weixin_42080056/article/details/81631661 vue- ...