【spring-bean】浅谈Spring的依赖注入(基础)
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.xsd
http://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.xsd
http://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 hierarchy
2015-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.xsd
http://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 hierarchy
2015-2-16 9:45:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
小泰迪【狗】正在吃。。。(⊙o⊙)…。。
小泰迪【狗】正在发出汪汪的的声音!
相关文章
- Spring DI - 依赖注入
- Spring IOC - 控制反转(依赖注入) - 懒加载机制
- Spring IOC - 控制反转(依赖注入) - 入门案例 - 获取对象的方式 - 别名标签
- Spring 依赖注入(注解方式)
- 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)
- Spring Security 入门篇
- spring常用的注入方式有哪些?
- Spring @Conditional注解 详细讲解及示例
- Spring MVC注解冲突
- 【Java】Spring boot、Mysql项目部署到Linux中后启动日志慢8小时(时区不对/时区错误)
- Spring中如Service有多个实现类,它怎么知道该注入哪个ServiceImpl类
- spring batch(二):核心部分(1):配置Spring batch
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
- Spring依赖注入:注解注入总结
- Spring中注入List,Set,Map,Properties的xml文件配置方法
- Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
- Spring Security 实战干货:图解用户是如何登录的
- Spring Boot国际化支持
- Spring MVC 学习笔记 spring mvc Schema-based configuration
- spring框架漏洞整理(Spring Boot Actuator相关漏洞)
- 【SSM直击大厂】第十章:Spring AOP面向切面编程
- spring 面试题第五天(10道开胃菜)
- spring mvc Spring Data Redis RedisTemplate [转]
- spring boot微服务改造冲突
- 【spring mvc】Spring MVC 的参数解析器ArgumentResolver阐述
- Spring(AbstractRoutingDataSource)实现动态数据源切换--转载
- spring IOC(Spring 生命周期,先1.构造方式,2,初始化方法,3,目标方法,4,销毁方法)