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. Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

    To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...

  2. 2019 Multi-University Training Contest 10 I Block Breaker

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  3. 从零开始制作数据集所需要的所有python脚本

    最近一直在做图片数据集,积累了很多心得.我把我所使用的python脚本全部拿出来,当然这些脚本大部分网上都有,只不过比较分散. 我已经把所有代码上传到github上,觉得写的好的话,请给我一个star ...

  4. AWVS 安全渗透扫描

    1.打开软件,点击 New Scan 2.在 website url 中输入被扫描的网址,点击 next 3.在 scanning profile 中选择测试的漏洞类型,默认选择 default(默认 ...

  5. C++ 函数重载,函数模板和函数模板重载,选择哪一个?

    重载解析 在C++中,对于函数重载.函数模板和函数模板重载,C++需要有一个良好的策略,去选择调用哪一个函数定义(尤其是多个参数时),这个过程称为重载解析. (这个过程将会非常复杂,但愿不要遇到一定要 ...

  6. CODING 敏捷实战系列课第二讲:Scrum 敏捷项目管理核心要素之 3355

    Scrum 是敏捷开发流派中最著名和最落地的一支,全球 70% 以上公司的敏捷转型都是以 Scrum 起步.CODING 特邀敏捷顾问.CST & CTC 认证敏捷教练申健老师将在本课程< ...

  7. 学习Vue第三节,事件修饰符stop、prevent、capture、self、once

    事件修饰符: .stop 阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当事件在该元素本身(比如不是子元素)触发时触发回调 .once 事 ...

  8. IDEA中如何使用debug调试项目 一步一步详细教程

    转载该文章:https://blog.csdn.net/yxl_1207/article/details/80973622 一.Debug开篇 首先看下IDEA中Debug模式下的界面. 如下是在ID ...

  9. MYSQL 日月周季年分组

    首先准备几条测试数据 DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `n_id` int(11) DEFAULT NULL, `d_created ...

  10. SpringBoot + SpringCloud的爬坑之旅

    1,application.yaml中配置没有生效问题解决 如果配置文件确认没有错误但是没有生效首先是要到编译目录去查看是否被编译过去了,如果没有,请先将项目clean在重启 但是idea启动项目时也 ...