Spring注入理解
2015-12-16 14:28
387 查看
spring有很多种依赖注入方式,因为知识有限我仅写我自己知道的,还请各位多多提提建议,大家一起学习、
spring通过xml进行ioc配置:
set注入:
@Controller public class GameController { private GameService gameService; public GameService getGameService() { return gameService; } @Autowired public void setGameService(GameService gameService) { this.gameService = gameService; }
只要被set的都需要在xml中实例化对象:值粘贴部分代码:
<bean id="GameDAO" class="mhf.dao.impl.GameDAOImpl"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="GameService" class="mhf.service.impl.GameServiceImpl"> <property name="gameDao" ref="GameDAO"></property> </bean>
我相信肯定有人跟我一样迷惑,property中name,ref分别对应的是哪里:
name是你controller中GameService的属性名即gameService,ref就是id为GameService的bean。
在这里SpringMVC的注解@Cotroller已经实例化了一个bean到spring上下文,所以我们来看id为GameService的bean,为了区分,name跟ref不一样,希望理解。
构造器注入:
引入私有成员变量后,使用有参数构造方法进行注入。
然后在xml中配置的时候使用<constructor-arg ref="">ref指向的是其他bean标签的id.
静态工厂的方法注入:
就是通过调用静态工厂的方法来获取自己需要的对象,为了让spring管理所有对象,我们不能直接通过"工程类.静态方法()"来获取对象,而是依然通过spring注入的形式获取:(没有使用不敢妄加评论,等以后使用之后会补充的)
相关文章推荐
- Spring的依赖注入(控制反转)的理解
- 关于spring中依赖注入(AOP)的理解
- 对spring控制反转以及依赖注入的理解
- spring依赖注入和Aop的理解
- 对Spring中接口注入的理解实例分析
- 对spring控制反转以及依赖注入的理解
- 101. Spring Boot轻松理解动态注入,删除bean【从零开始学Spring Boot】
- IOC,依赖倒置 (理解) Spring依赖注入的实现技术是:动态代理
- 如何理解spring的依赖注入
- Spring的Ioc和DI依赖注入的理解
- 【j2ee spring】2、深入理解注释@Resource注入
- 初步理解spring ioc原理(读完可自己实现依赖注入部分的spring框架)
- 对Spring中接口注入的理解实例分析
- 如何理解Spring的控制反转IOC和依赖注入DI思想
- (Spring的核心机制)理解依赖注入
- 【Spring学习笔记二】-理解依赖注入
- spring之依赖注入之理解
- 7.3.1: spring的核心机制依赖注入---理解依赖注入
- SpringBoot学习——@Autowired自动注入报:could not be found问题的理解和解决方案
- 如何理解 Spring 注入