java

package cn.ioc.student;

import java.util.List;
import java.util.Map;
import java.util.Set; public class Student {
private String name;
private Address addr;
private String[] books;
private List<String> testlist;
private Map<String,String> cards;
private Set<String> games;
private String wife; public String getWife() {
return wife;
} public void setWife(String wife) {
this.wife = wife;
} public Set<String> getGames() {
return games;
} public void setGames(Set<String> games) {
this.games = games;
} public List<String> getTestlist() {
return testlist;
} public void setTestlist(List<String> testlist) {
this.testlist = testlist;
} public Map<String, String> getCards() {
return cards;
} public void setCards(Map<String, String> cards) {
this.cards = cards;
} public String[] getBooks() {
return books;
} public void setBooks(String[] books) {
this.books = books;
} //-------------------------------------------------
public Address getAddr() {
return addr;
} public void setAddr(Address addr) {
this.addr = addr;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
//show
public void show(){
System.out.print("---我叫"+name+",---我住在"+addr.getAddr()+"---我爱看的书有"); for(int i=0;i<books.length;i++){
System.out.print(books[i]+",");
}
System.out.println("---list"+testlist);
System.out.println("---map="+cards);
System.out.println("---set="+games);
System.out.println("---wife老婆=");
}
}

address.class

package cn.ioc.student;

public class Address {
private String addr; public String getAddr() {
return addr;
} public void setAddr(String addr) {
this.addr = addr;
} }

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ioc -->
<bean id="address" class="cn.ioc.student.Address">
<property name="addr" value="beijing"></property>
</bean>
<!-- ref引用注入 -->
<bean id="student" class="cn.ioc.student.Student">
<property name="name" value="shangren"></property>
<property name="addr" ref="address"></property>
<!-- 数组注入 -->
<property name="books">
<array>
<value>书1</value>
<value>书2</value>
<value>书3</value>
</array>
</property>
<!-- list注入 -->
<property name="testlist">
<list>
<value>排球</value>
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- map -->
<property name="cards">
<map>
<entry key="中国银行" value="1997081100"></entry>
<entry>
<key><value>建设银行</value></key>
<value>110110110</value>
</entry>
</map>
</property>
<!-- set -->
<property name="games">
<set>
<value>dota</value>
<value>cs</value>
<value>cs</value>
</set>
</property>
<!-- 空 null -->
<property name="wife">
<null></null>
</property>
</bean>
</beans>

test.class

package cn.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.ioc.student.Student; public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
Student stu = (Student) ac.getBean("student");
stu.show();
}
}

结果

spring的各种注入的更多相关文章

  1. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  2. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  3. spring的依赖注入的最常见的两种方法

    package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...

  4. 一步一步深入spring(3)--spring的依赖注入方式

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...

  5. Spring Ioc-依赖注入的几种方式

    一 setter方法注入 配置文件如下: <bean id="helloAction" class="org.yoo.action.SpringSetterHell ...

  6. spring的依赖注入是什么意思

    最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...

  7. 关于spring的自动注入

    关于spring的自动注入 spring里面可以设置BeanDefinition自动注入类型,默认为AUTOWIRE_NO(不进行自动注入).mybatis里面的扫描接口生成MapperFactory ...

  8. SpringBoot系列: 理解 Spring 的依赖注入(二)

    ==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...

  9. SpringBoot系列: 理解 Spring 的依赖注入(一)

    ==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...

  10. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

随机推荐

  1. java的Timer定时器任务

    在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...

  2. 简单使用媒体查询@media

    @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@media 是非常有用的. 那媒体查询该如何使用呢? 一.铺垫 1.首先我们在使用 @media 的时候需要 ...

  3. Centos7下tomcat关闭异常问题

    目录 出错原因 解决方法 出错原因 ​ 在阿里云服务器上买的轻量级应用服务器,装上了tomcat,访问tomcat自带的首页,8080端口,第一次启动成功了,关闭也正常,但在服务器重启后,或者第二次启 ...

  4. CC2530应用——按键控制灯光状态变化

    独立新建工程并编写.编译代码,实现按键控制灯光闪烁状态的变换,实现以下任务要求:[1]程序开始运行:D4灯闪烁,D3.D5.D6灯熄灭.[2]按下模块上的SW1按键松开后,实现D5.D6灯轮流闪烁.[ ...

  5. Linux编程实现递归查找文件

    命令格式如下:SCRIPT_NAME       SEARCH_ON_DIR       SEARCH_FILENAME #!/bin/bash Find(){ files=`` for file i ...

  6. 【FPGA篇章三】FPGA常用语句:Verilog基本语法要素

    欢迎大家关注我的微信公众账号,支持程序媛写出更多优秀的文章 Verilog中总共有十九种数据类型,我们先介绍四个最基本的数据类型,他们是: reg型.wire型.integer型.parameter型 ...

  7. 【Spark】通过SparkStreaming实现从socket接受数据,并进行简单的单词计数

    文章目录 步骤 一.创建maven工程并导入jar包 二.安装并启动生产者 三.开发SparkStreaming代码 四.查看结果 步骤 一.创建maven工程并导入jar包 <properti ...

  8. ysql常用sql语句(12)- group by 分组查询

    测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...

  9. 图数据库 Nebula Graph 是什么

    图数据库(英语:Graph Database)是一个使用图结构进行语义查询的数据库.该系统的关键概念是图,形式上是点 (Node 或者 Vertex) 和边 (Edge 或者 Relationship ...

  10. FPGA的“可编程”使你迷惑吗?

    http://www.alteraforum.com.cn/showtopic-7791.aspx FPGA的“可编程”使你迷惑吗?   任何一个硬件工程师对FPGA都不会陌生,就好比C语言对于软件工 ...