【spring-bean】浅谈Spring的依赖注入(基础)
来源:互联网 发布:unity3d教程pdf 编辑:程序博客网 时间:2024/05/09 21:59
spring作为一种轻量级企业框架,其易用性和简化java开发的宗旨得以充分体现。在spring中,对象及其所依赖的对象(bean)创建都由Spring IOC容器进行管理(创建,销毁)。spring是一个基于容器的框架,本文就spring容器的依赖注入进行详解。
一丶属性注入。
属性注入是通过POJO的setXxx()方法注入Bean的属性值或所依赖对象,由于属性注入具有可选择性和灵活性高的特点,因此企业级J2EE应用通常都采用此方式.属性注入需要Bean提供默认的构造函数并为其对应属性提供set方法.原理是spring IOC容器利用JNDI技术获得bean的class属性,通过反射来调用bean的默认构造函数创建实例对象,继续利用反射机制调用set方法为其注入属性.
public class Cat implements Animal{ private String name; private String sound; @Override public void eat() { System.out.println(this.name + "【猫】正在吃。。。(⊙o⊙)…。。"); } @Override public void speed() { System.out.println(this.name + "【猫】正在发出" + sound + "的声音!"); } /** * @return 返回 name */ public String getName() { return name; } /** * @param 对 name 进行赋值 */ public void setName(String name) { this.name = name; } /** * @return 返回 sound */ public String getSound() { return sound; } /** * @param 对 sound 进行赋值 */ public void setSound(String sound) { this.sound = sound; } }父类Animal
public interface Animal{ void eat(); void speed();}
对应的spring配置文件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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="cat" class="com.test.dao.impl.Cat"><property name="name" value="汪星人"></property><property name="sound" value="喵喵的"></property></bean><strong></beans></strong>
定义一个bean对应cat类,其中一个属性为name="汪星人" ,另一个sound="喵喵的".
其中spring-beans包中有一个BeanDefinitionParser解析接口,用来解析bean的。然后利用反射创建bean。
其中<property>还有一种配置方式,首先应用p命名空间 引入xmlns:p="http://www.springframework.org/schema/p" 如下:
<?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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="cat" class="com.test.dao.impl.Cat" p:name="汪星人" p:sound="喵喵的"></bean></beans>junit测试类如下
public class AnimalTest{ @Test public void eat() { @SuppressWarnings("resource") ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Animal a = (Animal)ac.getBean("cat"); a.eat(); a.speed(); }}其中ClassPathXmlApplicationContext是读取classpath路径下的ApplicationContext文件.
显示结果如下:
2015-2-16 9:30:08 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7054c4ad: startup date [Mon Feb 16 09:30:07 CST 2015]; root of context hierarchy2015-2-16 9:30:08 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]汪星人【猫】正在吃。。。(⊙o⊙)…。。汪星人【猫】正在发出喵喵的的声音!
二丶构造函数注入
构造函数注入是除属性注入之外的另一种常用的注入方式,它保证了一些必要的属性在Bean实例化时就得到设置,并且确保了Bean实例化后可以使用.
1> 按类型匹配入参
使用构造注入的前提是Bean必须提供相应需要注入属性参数的构造函数,如下:
public class Dog implements Animal{ private String name; private String sound; Dog(String name, String sound) { this.name = name; this.sound = sound; } @Override public void eat() { System.out.println(this.name + "【狗】正在吃。。。(⊙o⊙)…。。"); } @Override public void speed() { System.out.println(this.name + "【狗】正在发出" + sound + "的声音!"); } /** * @return 返回 name */ public String getName() { return name; } /** * @param 对 name 进行赋值 */ public void setName(String name) { this.name = name; } /** * @return 返回 sound */ public String getSound() { return sound; } /** * @param 对 sound 进行赋值 */ public void setSound(String sound) { this.sound = sound; } }其中属性注入和构造注入的spring配置文件略有不同,如下所示:
<?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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="dog" class="com.test.dao.impl.Dog"><constructor-arg type="java.lang.String" value="小泰迪" /><constructor-arg type="java.lang.String" value="汪汪的" /></bean></beans>其中的type="java.lang.String" 代表注入的属性在java中是String类型的.构造注入用的是<constructor>标签,还可以用index来代表第几个参数进行构造注入.
<bean id="dog" class="com.test.dao.impl.Dog"><constructor-arg index="0" value="小泰迪" /><constructor-arg index="1" value="汪汪的" /></bean>
用junit测试如下.
2015-2-16 9:45:20 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7054c4ad: startup date [Mon Feb 16 09:45:20 CST 2015]; root of context hierarchy2015-2-16 9:45:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]小泰迪【狗】正在吃。。。(⊙o⊙)…。。小泰迪【狗】正在发出汪汪的的声音!
- 【spring-bean】浅谈Spring的依赖注入(基础)
- Spring基础 注解实现Bean依赖注入
- Spring中bean的依赖注入解析
- spring源码解读 Bean的依赖注入
- spring 依赖注入bean的属性
- (学习笔记)spring入门之Bean的依赖注入
- Spring受管Bean依赖注入(设值注入)
- Spring受管Bean依赖注入(设值注入)
- Spring的bean注入
- 【Spring】 Bean的注入
- Spring 有生命周期的bean的依赖注入
- Spring 有生命周期的bean的依赖注入
- Spring的配置、生命周期、依赖注入bean的操作
- spring的管理bean和依赖注入的原理剖析
- 白话Spring(基础篇)---依赖注入
- spring 配置bean的方法及依赖注入发方式
- Spring-2:bean的两种依赖注入方式
- spring 的依赖注入
- linux kernel如何处理大端小端字节序
- 分享一个技巧
- 备份参数设置
- What is the difference between DPI (dots per inch) and PPI (pixels per inch)?
- SQL——处理行的累加
- 【spring-bean】浅谈Spring的依赖注入(基础)
- 【HNOI2004】宠物收养所 平衡树
- windows下 Coreseek与sphinx 配置
- C++ 复制构造函数和赋值操作符
- struts2零配置详解(struts2-Convention+Plugin 中文)
- POJ测试数据合集
- Android实现类似ListView模式的回收和更新机制的瀑布流
- Linux kernel 版本号被记上了一个后缀"+" (plus sign)
- linux5.4下面安装ORACLE 10G RAC注意事项