Spring学习 Ioc篇(三)
1、在注解注入方式中,首先要在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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config />
- </<beans>
- <?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config />
- <bean id="personDao" class="com.cvicse.dao.impl.PersonDaoBean" />
- <bean id="personService" class="com.cvicse.service.impl.PersonServiceBean"/>
- </beans>
- public class PersonServiceBean implements PersonService {
- @Resource private PersonDao personDao;
- private String name;
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
- public PersonServiceBean(){}
- public PersonServiceBean(PersonDao personDao, String name) {
- this.personDao = personDao;
- this.name = name;
- }
- public void save(){
- personDao.add();
- }
- }
在程序中标注了@Resource后,spring容器会默认根据名字进行在xml中搜素id为personDao的bean,如果搜索不到则按PersonDao 的接口去搜索相应的实现类型com.cvicse.dao.impl.PersonDaoBean。
byType: 按照类型自动装配,可以根据属性的类型,在容器中寻找跟类型匹配的bean。如果发现多个,那么会抛出异常。如果没有找到,即属性值为null.
byName: 按照名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。
constructor : 与byType的方式类似,不同之处在于它应用于构造器参数。如果在人那个其中没有找
到,即属性值为null。
autodetect :通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。(如果发现默认的构造器,那么将使用byType方式)
- public class PersonServiceBean implements PersonService {
- private PersonDao personDao;
- private String name;
- public PersonServiceBean() {
- }
- public void save()
- {
- personDao.save();
- }
- }
配置文件的配置如下:
- <bean id="personDao" class="com.cvicse.dao.PersonDao"/>
- <bean id="personServiceBean"
- class="com.cvicse.service.impl.PersonServiceBean" autowire="byType"/>
- <?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-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:component-scan base-package="com.cvicse"/>
- </bean>
其中,base-package就是要扫描的包以及子包
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问层组件,即DAO
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
- import org.springframework.stereotype.Repository;
- @Repository
- public class PersonDao {
- public void save() {
- System.out.println("save");
- }
- }
这样,当容器扫描到此类的时候就纳入了容器里。
- import org.springframework.stereotype.Service;
- import com.cvicse.dao.PersonDao;
- import com.cvicse.service.PersonService;
- @Service
- public class PersonServiceBean implements PersonService {
- @Resource private PersonDao personDao;
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
- public void save()
- {
- personDao.save();
- }
- }
这样我们如何调用此bean?也没有配置文件里的id.spring规范这样规定:在自动扫描的情况下,要得到一个容器管理的bean,可以提供bean的全名,但是第一个字符小写!
- ApplicationContext ctx = new
- ClassPathXmlApplicationContext("beans.xml");
- PersonService personService =
- (PersonService)ctx.getBean("personServiceBean ");
- personService.save();
- @Service(“personService”)
- public class PersonServiceBean implements PersonService
如果要改成原型模式怎么做呢?这样
- @Service(“personService”) @Scope(“prototype”)
- public class PersonServiceBean implements PersonService
Spring学习 Ioc篇(三)的更多相关文章
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
- Spring学习 Ioc篇(二 )
5.spring依赖注入的方式 方法一:使用构造器方式进行注入 1.dao的类和接口 package com.cvicse.dao.impl; import com.cvicse.dao.Person ...
- 死磕Spring之IoC篇 - BeanDefinition 的解析过程(面向注解)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - 开启 Bean 的加载
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - Bean 的创建过程
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - @Autowired 等注解的实现原理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - Spring 应用上下文 ApplicationContext
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - 文章导读
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
随机推荐
- 电脑的f5刷新不了
新买的电脑,f5刷新不了页面,网上查了后发现是fn功能键的原因.同时fn+f5即可刷新.可是依然感觉好别扭... 按下fn+esc,再只按f5,就可以刷新页面了.神奇...
- WCF初探-1:认识WCF
1.WCF是什么? WindowsCommunication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由.NET Framework 3. ...
- 1172. Ship Routes
http://acm.timus.ru/problem.aspx?space=1&num=1172 水题DP 大整数直接上java 代码: import java.math.BigInte ...
- jQuery.extend源码深层分析
在网站的开发中,经常会自己写一些jQuery插件来方便使用,其中自然少不了一个关键的方法->jQuery.extend(),使用这个方法来扩展jQuery对象. 那么今天就来讲讲这个函数的实现原 ...
- jboss入门学习1
环境准备 win8 jdk1.6 jboss4.0.5 oracle10g 修改jboss默认端口 server/default/conf/jboss-service.xml 1.WebService ...
- qpython3 读取安卓lastpass Cookies
之前我的博客写了python读取windows chrome Cookies,沿着同样的思路,这次本来想尝试读取安卓chrome Cookies, 但是可能是chrome的sqlite3版本比较高读取 ...
- python 笔记1:安装python;eclipse中安装配置pydev
1 下载安装python. 官网:https://www.python.org/downloads/ 根据自己的操作系统选择需要的版本下载并安装. 我的电脑操作系统windows xp的,只 ...
- 为什么在保护模式下IA-32处理器最高可访问4GB的内存
在保护模式下,IA-32处理器可访问最高达4GB的内存,这是32位无符号二进制整数地址能够寻址的上限. 今天看汇编的时候发现书里带过一句,不太明白为什么内存上限是4GB,就搜了一下,总结了一下答案. ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- B/S和C/S测试的区别
B/S(Brower/Server)以访问方式为主,包含客户端浏览器.web应用服务器.数据库服务器的软件系统.一般的B/S结构,都是多层架构的,有界面层.业务逻辑层.数据层.由于这种结构不需 ...