案例:

beans.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >

  <bean id="userAction" class="com.cdsxt.action.UserAction" >
    <!-- name:对应action类内属性的名字 ref:对应bean元素的id的值
    <property name="userDao" ref="userDao" />
    <property name="str" value="aaaaa" />-->
    <constructor-arg index="0" ref="userDao" />
    <constructor-arg index="1" value="aaaa" />
    <constructor-arg index="2" >
      <list >
        <value>aaaa</value>
        <value>bbb</value>
      </list>
    </constructor-arg>
    <constructor-arg index="3" >
      <map >
        <entry key="aaa">
          <value>aaaa</value>
        </entry>
      </map>
    </constructor-arg>
    <constructor-arg index="4" >
      <props >
        <prop key="bbb">bbbb</prop>
      </props>
    </constructor-arg>
  </bean>
  <bean id="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" ></bean>
</beans>

Action:

public class UserAction {
  private UserDao userDao;
  private String str;
  private List<String> strList;
  private Map<String, String> strMap;
  private Properties pro;
  public UserAction(){ }
  public UserAction(UserDao userDao, String str, List<String> strList, Map<String, String> strMap, Properties pro) {
    this.userDao = userDao;
    this.str = str;
    this.strList = strList;
    this.strMap = strMap;
    this.pro = pro;
  };
  public void add(){
    System.out.println("==========UserAction============");
    userDao.add();
  }
  public UserDao getUserDao() {
    return userDao;
  }
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
  public String getStr() {
    return str;
  }
  public void setStr(String str) {
    this.str = str;
  }
  public List<String> getStrList() {
    return strList;
  }
  public void setStrList(List<String> strList) {
    this.strList = strList;
  }
  public Map<String, String> getStrMap() {
    return strMap;
  }
  public void setStrMap(Map<String, String> strMap) {
    this.strMap = strMap;
  }
  public Properties getPro() {
    return pro;
  }
  public void setPro(Properties pro) {
    this.pro = pro;
  }
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    UserAction u1 =(UserAction) context.getBean("userAction");
    System.out.println(u1.str+"====="+u1.strList);
    System.out.println(u1.strMap+"===="+u1.pro);
    u1.add();
  }
}

public interface UserDao {
  public void add();
}

public class UserDaoImpl implements UserDao{

  @Override
  public void add() {
    System.out.println("=========UserDao===========");
  }

}

实现spring IOC的常见二种方法 setter注入与构造器注入的更多相关文章

  1. spring IOC的常见几种以来注入的方式

    在spring ioc中有三种依赖注入,分别是:a.接口注入:b.setter方法注入:c.构造方法注入: 接口注入: public class ClassA { private InterfaceB ...

  2. Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)

    IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...

  3. spring 获取 WebApplicationContext的几种方法

    spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...

  4. mysql 远程连接数据库的二种方法

    http://blog.csdn.net/freecodetor/article/details/5799550 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5 ...

  5. IIS7.5使用web.config设置伪静态的二种方法

    转自 网上赚钱自学网 .http://www.whosmall.com/post/121 近几天公司里开发的项目有几个运行在IIS7.5上,由于全站采用的是伪静态,因此从网上找到两两种方法来实现.这两 ...

  6. mysql 远程连接数据库的二种方法

    一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口"3306",用户名为"root",密码"123 ...

  7. XML解析的二种方法之dom解析

    XML解析的二种方法:dom解析和sax解析 文件大小      存储位置          读取速度 dom解析     小文件      放在内存中              快 sax解析   ...

  8. debian7更换gcc版本的二种方法分享

    debian7更换gcc版本的二种方法分享   最近在编译qt,之前用的是debian6,gcc版本是gcc-4.4,当使用debian7时,编译遇到了很多跟debian6不一样的问题,debian7 ...

  9. SQL 2005批量插入数据的二种方法

    SQL 2005批量插入数据的二种方法 Posted on 2010-07-22 18:13 moss_tan_jun 阅读(2635) 评论(2) 编辑 收藏 在SQL Server 中插入一条数据 ...

随机推荐

  1. Microsoft Dynamics CRM4.0 JScript 过滤lookup 出现 Microsoft Dynamics CRM 窗口无法打开,可能已被弹出窗口阻止程序所阻止。

    一.现象:JScript过滤lookup字段,选择lookup字段出现下图的情况: 出现:Microsoft Dynamics CRM 窗口无法打开,可能已被弹出窗口阻止程序所阻止.请将这台Micro ...

  2. 【jmeter】jMeter使用Badboy录制Web测试脚本

    JMeter 是纯Java编写的软件功能和性.能测试工具,其录制脚本过于笨拙和复杂.而Badboy是用C++开发的动态应用测试工具,其拥有强大的屏幕录制和回放 功能,同时提供图形结果分析功能,刚好弥补 ...

  3. 使用Jquery实现Win8开始菜单效果的站点导航

    前言: 本人是个Metro控,自我感觉到处都充斥着Metro的元素,个人认为这种风格强调表现以及内容,以简洁著称,不过也不是大部分都喜欢,也有一些人和你讨厌这种风格~不过本人非常喜欢这种风格,看我博客 ...

  4. 【Spring学习笔记-MVC-7】Spring MVC模型对象-模型属性讲解

    作者:ssslinppp       来自为知笔记(Wiz) 附件列表 处理模型数据.png

  5. wp模版强制用CSS空两格的问题

    之前我写过一篇文章<关于模板该不该用css强制编辑器文本开头空两格>,里面有说到一个观点,模版作者设计的时候,不要控制文章段落空两格,但是我用久了wp,我才慢慢发现,做wp模版的时候,确实 ...

  6. 扫盲贴,802.11AD

    早在去年,大家已经开始关注新的802.11ad规范,其高频高带宽低延迟的特性也让大家对将来的无线网络应用形态充满了期待,而今年的CES2016展会上,已经有不少的802.11ad产品出现,看来2016 ...

  7. Hive基础之Hive体系架构&运行模式&Hive与关系型数据的区别

    Hive架构 1)用户接口: CLI(hive shell):命令行工具:启动方式:hive 或者 hive --service cli ThriftServer:通过Thrift对外提供服务,默认端 ...

  8. Samba 简介

    SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...

  9. Solr SchemaXml 一些解读

    The schema.xml file contains all of the details about which fields your documents can contain, and h ...

  10. linux双网卡绑定实现冗余与负载均衡

    1 编辑/etc/modprobe.conf   在/etc/modprobe.conf里加入如下两行: alias bond0 bonding options bond0 mode=1 miimon ...