Spring 的四种方式依赖注入
来源:互联网 发布:mac 的火箭按钮不见了 编辑:程序博客网 时间:2024/06/05 03:38
一、Spring 基于构造函数的依赖注入
package com.home.model;public class Teditor { private Specker specker; public Teditor(Specker specker){ System.out.println("Inside Teditor constructor."); this.specker=specker; } public void speckeCheck(){ specker.Speckering(); }}public class Specker { public Specker(){ System.out.println("Inside Specker constructor."); } public void Speckering(){ System.out.println("Inside Speckering!"); }}//以下为配置文件<!-- 基于构造函数的依赖注入 --> <bean id="teditor" class="com.home.model.Teditor"> <constructor-arg ref="specker"></constructor-arg> </bean> <bean id="specker" class="com.home.model.Specker"></bean>//测试代码 @RequestMapping(value="/teditor",method=RequestMethod.GET) public ModelAndView DITEST(){ ModelAndView view=new ModelAndView("/login/hello"); ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Teditor teditor=(Teditor) context.getBean("teditor"); teditor.speckeCheck(); return view; }
输出结果:
Inside Specker constructor.
Inside Teditor constructor.
inside into Sparker construstor
二、Spring 基于设值函数的依赖注入
//SparkSystem类public class SparkSystem { private Sparker sparker; public void setSparker(Sparker sparker){ System.out.println("SparkSystem===Inside setSpark"); this.sparker=sparker; } public Sparker getSparker(){ System.out.println("SparkSystem==Inside get getPark"); return sparker; } public void spark(){ sparker.sparkering(); }}//Sparker类package com.home.model;public class Sparker { public Sparker(){ System.out.println("inside into Sparker construstor"); } public void sparkering(){ System.out.println("inside in Sparker for sparkering"); }}//配置文件 <!-- Spring 基于设值函数的依赖注入 --> <bean id="sparkSystem" class="com.home.model.SparkSystem"> <property name="sparker" ref="sparker"></property> </bean> <bean id="sparker" class="com.home.model.Sparker"></bean>
三、Spring 注入内部 Beans
正如你所知道的 Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 bean。因此在 或 元素内 元素被称为内部bean
package com.home.model;//Computer类public class Computer { private Message message; public Computer(){ System.out.println("inside into Computer constructor"); } public void setMessage(Message message){ System.out.println("inside into setComputer"); this.message=message; } public Message getMessage(){ System.out.println("inside into getComputer"); return message; } public void messageing(){ message.messageing(); }}//Message类package com.home.model;public class Message { public Message(){ System.out.println("inside into Message constructor!"); } public void messageing(){ System.out.println("inside messageing...."); }}//配置文件<!-- 注入内部 Beans --> <bean id="computer" class="com.home.model.Computer"> <property name="message"> <bean id="message" class="com.home.model.Message"> </bean> </property> </bean>
四、Spring 注入集合
****<list>它有助于连线,如注入一列值,允许重复。 <set>它有助于连线一组值,但不能重复。 <map>它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。 <props>它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。****
package com.home.model;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class CollectionExample { List addressList;//它有助于连线,如注入一列值,允许重复 Set addressSet;//它有助于连线一组值,但不能重复 Map addressMap;//它可以用来注入名称-值对的集合,其中名称和值可以是任何类型 Properties addressProp;//它可以用来注入名称-值对的集合,其中名称和值都是字符串类型 public List getAddressList() { System.out.println("List:"+addressList); return addressList; } public void setAddressList(List addressList) { this.addressList = addressList; } public Set getAddressSet() { System.out.println("Set:"+addressSet); return addressSet; } public void setAddressSet(Set addressSet) { this.addressSet = addressSet; } public Map getAddressMap() { System.out.println("Map:"+addressMap); return addressMap; } public void setAddressMap(Map addressMap) { this.addressMap = addressMap; } public Properties getAddressProp() { return addressProp; } public void setAddressProp(Properties addressProp) { System.out.println("Prop:"+addressProp); this.addressProp = addressProp; }}//配置文件如下: <!-- 注入集合 --> <bean id="collectionExample" class="com.home.model.CollectionExample"> <property name="addressList"> <list> <value>China</value> <value>India</value> <value>India</value> <value>USA</value> </list> </property> <property name="addressSet"> <set> <value>China</value> <value>India</value> <value>India</value> <value>USA</value> </set> </property> <property name="addressMap"> <map> <entry key="1" value="China"></entry> <entry key="2" value="India"></entry> <entry key="3" value="India"></entry> <entry key="4" value="USA"></entry> </map> </property> <property name="addressProp"> <props> <prop key="one">China</prop> <prop key="two">India</prop> <prop key="three">India</prop> <prop key="four">USA</prop> </props> </property> </bean>//测试类 @RequestMapping(value="/collection",method=RequestMethod.GET) public ModelAndView collection(){ ModelAndView view=new ModelAndView("/login/hello"); ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionExample collectionExample=(CollectionExample)context.getBean("collectionExample"); collectionExample.getAddressList(); collectionExample.getAddressSet(); collectionExample.getAddressMap(); collectionExample.getAddressProp(); return view; }打印结果如下:List:[China, India, India, USA]Set:[China, India, USA]Map:{1=China, 2=India, 3=India, 4=USA}Prop:{two=India, one=China, three=India, four=USA}
阅读全文
0 0
- spring依赖注入的四种方式
- Spring依赖注入的四种方式
- spring 依赖注入的四种方式
- spring四种依赖注入的方式
- spring依赖注入的四种方式
- spring的四种依赖注入方式
- Spring 的四种方式依赖注入
- Spring的四种依赖注入方式
- spring依赖注入的四种方式
- spring的四种依赖注入的方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- spring四种依赖注入方式
- linux下安装anaconda,torch,tensorflow
- 【UGUI】UGUI 基础知识(代码设置锚点)
- android学习路线
- 学习笔记-自定义密码输入框和自定义数字密码软键盘
- 决策树学习笔记(一)
- Spring 的四种方式依赖注入
- iOS 高德地图怎么在屏幕内显示所有的Marker?
- Python中的闭包
- 按位定义结构体
- JDK8的ParallelStream遍历无序的问题
- 倒计时时钟 面向对象
- html标签
- 计算两点间的距离
- Collection集合之操作元素方法实现