Spring框架的IOC注入:

一、Java部分代码:

Person实体类:

 package com.ioc;

 import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import com.adviceAop.Dancer;
import com.aop.Singer; /**
* Spring 各种类型的注入。
* @Person.java
* @author BlueLake
* @2017-9-12 下午2:11:02
*/
public class Person { //普通类型:字符串类型
private String pname;
//普通类型:字符串类型
private int age;
//集合类型:list
private List<String> loves;
//集合类型:Set
private Set<String> books;
//集合类型:Map
private Map<String,String> heros;
//集合类型:Properties
private Properties palaces;
//引用类型
private Singer singer;
private Dancer dancer; private Emp emp ; //addr不加get、set方法,使用构造器注入。
private String addr; /**
* 无参数构造
*/
public Person() {
super();
} /**
* 参数构造。
* @param addr
*/
public Person(String addr) {
this.addr = addr;
} public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getLoves() {
return loves;
}
public void setLoves(List<String> loves) {
this.loves = loves;
}
public Set<String> getBooks() {
return books;
}
public void setBooks(Set<String> books) {
this.books = books;
} public Map<String, String> getHeros() {
return heros;
} public void setHeros(Map<String, String> heros) {
this.heros = heros;
} public Properties getPalaces() {
return palaces;
} public void setPalaces(Properties palaces) {
this.palaces = palaces;
} public Singer getSinger() {
return singer;
}
public void setSinger(Singer singer) {
this.singer = singer;
}
public Dancer getDancer() {
return dancer;
} public void setDancer(Dancer dancer) {
this.dancer = dancer;
}
public Emp getEmp() {
return emp;
} public void setEmp(Emp emp) {
this.emp = emp;
} @Override
public String toString() {
return "Person [pname=" + pname + ", age=" + age + ", loves=" + loves
+ ", books=" + books + ", heros=" + heros + ", palaces="
+ palaces + ", singer=" + singer + ", dancer=" + dancer
+ ", emp=" + emp + ", addr=" + addr + "]";
}
}

Emp实体类:

 package com.ioc;

 import com.aop.Singer;
/**
* 员工类。
* @Emp.java
* @author BlueLake
* @2017-9-12 下午7:27:33
*/
public class Emp { private String ename;
private int age ;
private Singer singer; public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Singer getSinger() {
return singer;
}
public void setSinger(Singer singer) {
this.singer = singer;
} }

唱歌类:

 package com.aop;
/**
* 唱歌类
* @Singer.java
* @author BlueLake
* @2017-9-12 下午7:28:53
*/
public class Singer { public void sing(){
System.out.println("玖月奇迹--你若盛开~~~");
}
}

跳舞类:

 package com.adviceAop;
/**
* 跳舞类
* @Dancer.java
* @author BlueLake
* @2017-9-12 下午7:29:14
*/
public class Dancer { public void dance(String name){
System.out.println("杨丽萍来跳孔雀舞~~~"+name);
// throw new RuntimeException("受伤了吗?");
}
}

二、配置applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- spring基本注入 ,property的name名称要和类里的名称一致。 -->
<bean id="person" class="com.ioc.Person" autowire="byName">
<!-- 1.构造器注入, 使用constructor-arg标签,必须有这个构造方法 -->
<constructor-arg name="addr">
<value>祖籍钱塘</value>
</constructor-arg>
<!-- 2.set 方法注入 使用 property 标签-->
<property name="pname"> <value>项羽</value> </property>
<property name="age" value="27"> </property>
<property name="loves">
<list>
<value>塞外骑马</value>
<value>江南采莲</value>
<value>剪烛西窗</value>
</list>
</property>
<property name="books">
<set>
<value>小窗幽记</value>
<value>围炉夜话</value>
<value>菜根谭</value>
</set>
</property>
<property name="heros">
<map>
<entry>
<key> <value>曹操</value> </key>
<value>北魏</value>
</entry>
<entry>
<key> <value>赵云</value> </key>
<value>西蜀</value>
</entry>
<entry>
<key> <value>周瑜</value> </key>
<value>东吴</value>
</entry>
</map>
</property>
<property name="palaces">
<props>
<prop key="china">故宫</prop>
<prop key="russia">克里姆林宫</prop>
<prop key="france">卢浮宫</prop>
</props>
</property> <!-- set注入,如果是引用类型,依赖注入 ,使用 ref 标签 -->
<property name="singer" >
<ref bean="singer"/>
</property>
<property name="dancer" ref="dancer"></property>
<!-- 3.自动注入 ,需要写set方法。-->
<!-- emp属性没有配置,会根据bean里的 autowire="byName" 自动注入 -->
</bean>
<!-- 使用p标签注入 ,基本属性直接写入值,依赖属性,指向id -->
<bean id="emp" class="com.ioc.Emp" p:ename="张良" p:age="25" p:singer-ref="singer">
</bean> </beans>

上面少了:<bean id="singer" class="com.aop.Singer"></bean>

    <bean id="dancer" class="com.adviceAop.Dancer" ></bean>

三、测试类

 package com.ioc;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.log4j.LoggerTest;
/**
* 测试注入代码
*/
public class PersonTest { public static void main(String[] args) {
//加载Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过bean的 id和 类型,获取类对象
Person per = ac.getBean("person",Person.class);
System.out.println(per.toString());
//添加日志
// LoggerTest.log.info(per.toString());
/*
* Person [pname=项羽, age=27, loves=[塞外骑马, 江南采莲, 剪烛西窗],
* books=[小窗幽记, 围炉夜话, 菜根谭], heros={曹操=北魏, 赵云=西蜀, 周瑜=东吴},
* palaces={france=卢浮宫, china=故宫, russia=克里姆林宫},
* singer=com.aop.Singer@1367e28, dancer=com.adviceAop.Dancer@143bf3d,
* emp=com.ioc.Emp@c8769b, addr=祖籍钱塘]
*/ //通过bean 的id获取类,再将Object 强转成对应的类型。
Emp emp = (Emp) ac.getBean("emp");
System.out.println(emp.getEname()+"\t"+emp.getAge());//张良 25
} }

Spring注入,Ioc的具体配置的更多相关文章

  1. Spring注入方式及注解配置

    一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ...

  2. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

  3. Spring操作指南-IoC基础环境配置(基于注解自动装配)

    项目源码:http://code.taobao.org/p/LearningJavaEE/src/LearningSpring001%20-%20Automatically%20wiring%20be ...

  4. Spring操作指南-IoC基础环境配置(基于注解手动装配)

    Source: http://code.taobao.org/p/LearningJavaEE/src/LearningSpring002%20-%20Wiring%20beans%20with%20 ...

  5. Spring操作指南-IoC基础环境配置(基于XML)

  6. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  7. spring之IOC容器创建对象

    1.术语了解 1.1组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hi ...

  8. Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域

    Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...

  9. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入)使用ioc模式开发 实体类必须有无参构造方法1.搭建Spring环境下载jarhttp://maven.springframework. ...

随机推荐

  1. 给 vue项目添加ESLint

    eslint配置方式有两种: 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个js,JSON或者YAML文件来给整个目录和它的子目录指定配置信息.这些配置可以写在一 ...

  2. 单分子荧光原位杂交(smFISH)

    single-molecule RNA fluorescence in situ hybridization (RNA smFISH) 单分子荧光原位杂交(smFISH)是一种新的基因表达分析方法,能 ...

  3. Binomial Coefficient(二项式系数)

    In mathematics, any of the positive integers that occurs as a coefficient in the binomial theorem is ...

  4. 【洛谷p1164】小A点菜

    (……) 小A点菜[传送门] 上标签: (一个神奇的求背包问题方案总数的题) 核心算法: ;i<=n;i++) for(int j=m;j>=a[i];j--) f[j]+=f[j-a[i ...

  5. Codeforces Round #250 (Div. 1)E. The Child and Binary Tree

    题意:有一个集合,求有多少形态不同的二叉树满足每个点的权值都属于这个集合并且总点权等于i 题解:先用生成函数搞出来\(f(x)=f(x)^2*c(x)+1\) 然后转化一下变成\(f(x)=\frac ...

  6. shiro中JSP标签

    Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制,如根据登录用户显示相应的页面按钮. 导入标签库 <%@taglib prefix="shiro" uri=& ...

  7. linux的越墙方法

    .首先要安装openSSH, Ubuntu缺省没有安装SSH Server,使用以下命令安装: sudo apt-get install openssh-server 但是系统有时候会出现E类错误,无 ...

  8. mybatis批量插入的方式

    批量插入数据经常是把一个集合的数据一次性插入数据库,只需要执行一次sql语句,但是批量插入通常会报框架版本号的错误,本人就遇到 com.alipay.zdal.parser.exceptions.a: ...

  9. python 小练习 5

    Py从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992, 这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和 ...

  10. WDA基础十二:FREE PROGRAM SH (WDA TREE)

    一个需要用TREE展示搜索帮助的需求: 1.创建WDA程序:ZCATEGORY 2.Component Controller中添加节点: (说明,此节点仅在搜索帮助程序中使用,可以不用interfac ...