Spring注入,Ioc的具体配置
Posted 溪山晴雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring注入,Ioc的具体配置相关的知识,希望对你有一定的参考价值。
Spring框架的IOC注入:
一、Java部分代码:
Person实体类:
1 package com.ioc; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 import com.adviceAop.Dancer; 9 import com.aop.Singer; 10 11 /** 12 * Spring 各种类型的注入。 13 * @Person.java 14 * @author BlueLake 15 * @2017-9-12 下午2:11:02 16 */ 17 public class Person { 18 19 //普通类型:字符串类型 20 private String pname; 21 //普通类型:字符串类型 22 private int age; 23 //集合类型:list 24 private List<String> loves; 25 //集合类型:Set 26 private Set<String> books; 27 //集合类型:Map 28 private Map<String,String> heros; 29 //集合类型:Properties 30 private Properties palaces; 31 //引用类型 32 private Singer singer; 33 private Dancer dancer; 34 35 private Emp emp ; 36 37 //addr不加get、set方法,使用构造器注入。 38 private String addr; 39 40 41 42 /** 43 * 无参数构造 44 */ 45 public Person() { 46 super(); 47 } 48 49 /** 50 * 参数构造。 51 * @param addr 52 */ 53 public Person(String addr) { 54 this.addr = addr; 55 } 56 57 58 public String getPname() { 59 return pname; 60 } 61 public void setPname(String pname) { 62 this.pname = pname; 63 } 64 public int getAge() { 65 return age; 66 } 67 public void setAge(int age) { 68 this.age = age; 69 } 70 public List<String> getLoves() { 71 return loves; 72 } 73 public void setLoves(List<String> loves) { 74 this.loves = loves; 75 } 76 public Set<String> getBooks() { 77 return books; 78 } 79 public void setBooks(Set<String> books) { 80 this.books = books; 81 } 82 83 public Map<String, String> getHeros() { 84 return heros; 85 } 86 87 public void setHeros(Map<String, String> heros) { 88 this.heros = heros; 89 } 90 91 public Properties getPalaces() { 92 return palaces; 93 } 94 95 public void setPalaces(Properties palaces) { 96 this.palaces = palaces; 97 } 98 99 public Singer getSinger() { 100 return singer; 101 } 102 public void setSinger(Singer singer) { 103 this.singer = singer; 104 } 105 public Dancer getDancer() { 106 return dancer; 107 } 108 109 public void setDancer(Dancer dancer) { 110 this.dancer = dancer; 111 } 112 public Emp getEmp() { 113 return emp; 114 } 115 116 public void setEmp(Emp emp) { 117 this.emp = emp; 118 } 119 120 @Override 121 public String toString() { 122 return "Person [pname=" + pname + ", age=" + age + ", loves=" + loves 123 + ", books=" + books + ", heros=" + heros + ", palaces=" 124 + palaces + ", singer=" + singer + ", dancer=" + dancer 125 + ", emp=" + emp + ", addr=" + addr + "]"; 126 } 127 }
Emp实体类:
1 package com.ioc; 2 3 import com.aop.Singer; 4 /** 5 * 员工类。 6 * @Emp.java 7 * @author BlueLake 8 * @2017-9-12 下午7:27:33 9 */ 10 public class Emp { 11 12 private String ename; 13 private int age ; 14 private Singer singer; 15 16 public String getEname() { 17 return ename; 18 } 19 public void setEname(String ename) { 20 this.ename = ename; 21 } 22 public int getAge() { 23 return age; 24 } 25 public void setAge(int age) { 26 this.age = age; 27 } 28 public Singer getSinger() { 29 return singer; 30 } 31 public void setSinger(Singer singer) { 32 this.singer = singer; 33 } 34 35 }
唱歌类:
1 package com.aop; 2 /** 3 * 唱歌类 4 * @Singer.java 5 * @author BlueLake 6 * @2017-9-12 下午7:28:53 7 */ 8 public class Singer { 9 10 public void sing(){ 11 System.out.println("玖月奇迹--你若盛开~~~"); 12 } 13 }
跳舞类:
1 package com.adviceAop; 2 /** 3 * 跳舞类 4 * @Dancer.java 5 * @author BlueLake 6 * @2017-9-12 下午7:29:14 7 */ 8 public class Dancer { 9 10 public void dance(String name){ 11 System.out.println("杨丽萍来跳孔雀舞~~~"+name); 12 // throw new RuntimeException("受伤了吗?"); 13 } 14 }
二、配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 11 12 <!-- spring基本注入 ,property的name名称要和类里的名称一致。 --> 13 <bean id="person" class="com.ioc.Person" autowire="byName"> 14 <!-- 1.构造器注入, 使用constructor-arg标签,必须有这个构造方法 --> 15 <constructor-arg name="addr"> 16 <value>祖籍钱塘</value> 17 </constructor-arg> 18 <!-- 2.set 方法注入 使用 property 标签--> 19 <property name="pname"> <value>项羽</value> </property> 20 <property name="age" value="27"> </property> 21 <property name="loves"> 22 <list> 23 <value>塞外骑马</value> 24 <value>江南采莲</value> 25 <value>剪烛西窗</value> 26 </list> 27 </property> 28 <property name="books"> 29 <set> 30 <value>小窗幽记</value> 31 <value>围炉夜话</value> 32 <value>菜根谭</value> 33 </set> 34 </property> 35 <property name="heros"> 36 <map> 37 <entry> 38 <key> <value>曹操</value> </key> 39 <value>北魏</value> 40 </entry> 41 <entry> 42 <key> <value>赵云</value> </key> 43 <value>西蜀</value> 44 </entry> 45 <entry> 46 <key> <value>周瑜</value> </key> 47 <value>东吴</value> 48 </entry> 49 </map> 50 </property> 51 <property name="palaces"> 52 <props> 53 <prop key="china">故宫</prop> 54 <prop key="russia">克里姆林宫</prop> 55 <prop key="france">卢浮宫</prop> 56 </props> 57 </property> 58 59 <!-- set注入,如果是引用类型,依赖注入 ,使用 ref 标签 --> 60 <property name="singer" > 61 <ref bean="singer"/> 62 </property> 63 <property name="dancer" ref="dancer"></property> 64 <!-- 3.自动注入 ,需要写set方法。--> 65 <!-- emp属性没有配置,会根据bean里的 autowire="byName" 自动注入 --> 66 </bean> 67 <!-- 使用p标签注入 ,基本属性直接写入值,依赖属性,指向id --> 68 <bean id="emp" class="com.ioc.Emp" p:ename="张良" p:age="25" p:singer-ref="singer"> 69 </bean> 70 71 </beans>
上面少了:<bean id="singer" class="com.aop.Singer"></bean>
<bean id="dancer" class="com.adviceAop.Dancer" ></bean>
三、测试类
1 package com.ioc; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.log4j.LoggerTest; 7 /** 8 * 测试注入代码 9 */ 10 public class PersonTest { 11 12 public static void main(String[] args) { 13 //加载Spring配置文件 14 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 15 //通过bean的 id和 类型,获取类对象 16 Person per = ac.getBean("person",Person.class); 17 System.out.println(per.toString()); 18 //添加日志 19 // LoggerTest.log.info(per.toString()); 20 /* 21 * Person [pname=项羽, age=27, loves=[塞外骑马, 江南采莲, 剪烛西窗], 22 * books=[小窗幽记, 围炉夜话, 菜根谭], heros={曹操=北魏, 赵云=西蜀, 周瑜=东吴}, 23 * palaces={france=卢浮宫, china=故宫, russia=克里姆林宫}, 24 * [email protected], [email protected], 25 * [email protected], addr=祖籍钱塘] 26 */ 27 28 //通过bean 的id获取类,再将Object 强转成对应的类型。 29 Emp emp = (Emp) ac.getBean("emp"); 30 System.out.println(emp.getEname()+"\t"+emp.getAge());//张良 25 31 } 32 33 }
以上是关于Spring注入,Ioc的具体配置的主要内容,如果未能解决你的问题,请参考以下文章