学习 Spring (二) Spring 注入
Spring入门篇 学习笔记
常用的两种注入方式
- 设值注入
- 构造注入
示例准备工作
添加 InjectionDAO:
public interface InjectionDAO {
void save(String arg);
}
添加 InjectionDAOImpl:
public class InjectionDAOImpl implements InjectionDAO {
public void save(String arg) {
System.out.println("保存数据:" + arg);
}
}
添加 InjectionService:
public interface InjectionService {
void save(String arg);
}
设值注入
添加 InjectionServicePropertyImpl:
public class InjectionServicePropertyImpl implements InjectionService {
private InjectionDAO injectionDAO;
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void save(String arg) {
System.out.println("Service(Property)接收参数:" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
}
添加配置文件 classpath:spring-injection-property.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="injectionService" class="com.karonda.ioc.injection.service.InjectionServicePropertyImpl">
<property name="injectionDAO" ref="injectionDAO"></property>
</bean>
<bean id="injectionDAO" class="com.karonda.ioc.injection.dao.InjectionDAOImpl"></bean>
</beans>
添加测试 TestInjectionProperty:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjectionProperty extends UnitTestBase {
public TestInjectionProperty() {
super("classpath:spring-injection-property.xml");
}
@Test
public void testSetter() {
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
}
构造注入
添加 InjectionServiceConstructorImpl:
public class InjectionServiceConstructorImpl implements InjectionService {
private InjectionDAO injectionDAO;
//构造器注入
public InjectionServiceConstructorImpl(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void save(String arg) {
//模拟业务操作
System.out.println("Service(Constructor)接收参数:" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
}
添加配置文件 spring-injection-constructor.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="injectionService" class="com.karonda.ioc.injection.service.InjectionServiceConstructorImpl">
<constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg>
</bean>
<bean id="injectionDAO" class="com.karonda.ioc.injection.dao.InjectionDAOImpl"></bean>
</beans>
添加测试 TestInjectionConstructor
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjectionConstructor extends UnitTestBase {
public TestInjectionConstructor() {
super("classpath:spring-injection-constructor.xml");
}
@Test
public void testCons() {
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
}
源码:learning-spring
学习 Spring (二) Spring 注入的更多相关文章
- Spring学习笔记二:注入方式
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html 我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...
- spring学习笔记二:spring使用构造方法注入(set方式注入)
项目目录树: 1.spring的依赖包配置 * SPRING_HOME/dist/spring.jar * SPRING_HOME/lib/log4j/log4j-1.2.14.jar * SPRIN ...
- SSH框架入门学习之二(spring)
Spring也是一个开源框架,我在学习Spring的时候,认为最重要的几点是:IOC(控制反转).AOP(面向切面)和容器概念. 详细的教程还请大家去看网上的视频,这里贴一个小Demo以供学习. (前 ...
- Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- Spring 4.0.2 学习笔记(2) - 自动注入及properties文件的使用
接上一篇继续, 学习了基本的注入使用后,可能有人会跟我一样觉得有点不爽,Programmer的每个Field,至少要有一个setter,这样spring配置文件中才能用<property> ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
随机推荐
- 初学Python——Socket网络编程
认识socket socket本质上就是在2台网络互通的电脑之间,架设一个通道,两台电脑通过这个通道来实现数据的互相传递.我们知道网络 通信 都 是基于 ip+port(端口) 方能定位到目标的具体机 ...
- AI 概率图模型
概率图模型(Probabilistic Graphical Model) 有向图模型 信念网络 贝叶斯网络 无向模型 马尔科夫随机场 马尔科夫网络
- Java关键字(六)——super
在 Java关键字(五)——this 中我们说 this 关键字是表示当前对象的引用.而 Java 中的 super 关键字则是表示 父类对象的引用. 我们分析这句话“父类对象的引用”,那说明我们使用 ...
- flex布局,最后一行左对齐
拥抱flex 网上查找资料解决办法都是操作数据,个人感觉css问题还是用css来解决(当然问题不同,解决方案不同,这里只是针对某个问题的解决方法,不能解决所有问题,大家视情况而定,如果还是不行欢迎沟通 ...
- 任务调度工具Quartz入门笔记
一,导包 1)官网下载:http://www.quartz-scheduler.org/downloads/ 2)Maven <dependency> <groupId>org ...
- hibernate多对多 一对多 及简单入门 主键生成策略
Hibernate简单使用 入门 通过hibernate的 一对多 多对多轻松看懂hibernate配置 (不使用注解) hibernate对jdbc访问数据库的代码进行轻量级封装,简化重复代码 减少 ...
- HDU - 1255 扫描线+离散化进阶
这道题最开始我以为和HDU - 1542 那道题一样,只需要把cover次数改成2次即可,但是后面仔细一想,我们需要求的是覆盖次数大于等于2次的,这样的话,我们需要维护两个长度,HDU-1542 由于 ...
- echarts x轴 增加滚动条
charts x轴 增加滚动条 在option 配置项中添加 [ dataZoom 中配置 ] 设置x轴滚动条 效果图: 动态拖动 以下参考代码 dataZoom配置 官网写法 option = { ...
- Jmeter之上传数据流(图片、文本等)请求
MIME类型~Content-Type: 参数名称~name
- WebSocket实现一个聊天室
聊天室页面-->index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...