Spring入门案例 DI
来源:互联网 发布:凸优化 清华大学 pdf 编辑:程序博客网 时间:2024/06/06 19:16
Spring入门案例 DI
Spring的两个核心之一,DI(Dependent Injection)依赖注入。
依赖:一个对象需要使用另一个对象
注入:通过setter方法进行另一个对象实例设置。、
Spring的执行过程:
1、 创建service实例:BookServicebookService = new BookServiceImpl() -->IoC <bean>
2、 创建dao实例:BookDao bookDao = newBookDaoImple() -->IoC
3、 将dao设置给service:bookService.setBookDao(bookDao); -->DI <property>
1、 在Spring项目下新建一个pakage。
2、 创建BookDao接口和实现类
/** * */package com.dianer.lily.tryDI;/** * * * @author LilyLee * @date 2017年6月5日 * @time 上午8:36:16 * @Version 1.0 * @email lilylee_1213@foxmail.com * */public interface BookDao {public void save();}
/** * */package com.dianer.lily.tryDI;/** * * * @author LilyLee * @date 2017年6月5日 * @time 上午8:36:53 * @Version 1.0 * @email lilylee_1213@foxmail.com * */public class BookDaoImpl implements BookDao {/* (non-Javadoc) * @see com.dianer.lily.tryDI.BookDao#addBook() */@Overridepublic void save() {// TODO Auto-generated method stubSystem.out.println("依赖注入: addBook()");}}
3、 创建BookService接口和实现类
/** * */package com.dianer.lily.tryDI;/** * * * @author LilyLee * @date 2017年6月5日 * @time 上午8:32:32 * @Version 1.0 * @email lilylee_1213@foxmail.com * */public interface BookService {public abstract void addBook();}
/** * */package com.dianer.lily.tryDI;/** * * * @author LilyLee * @date 2017年6月5日 * @time 上午8:33:35 * @Version 1.0 * @email lilylee_1213@foxmail.com * */public class BookServiceImpl implements BookService {/* (non-Javadoc) * @see com.dianer.lily.tryDI.BookService#addBook() */private BookDao bookDao;public void setBookDao(BookDao bookdao){this.bookDao=bookdao;}@Overridepublic void addBook() {// TODO Auto-generated method stubthis.bookDao.save();}public BookServiceImpl() {// TODO Auto-generated constructor stubSystem.out.println("被new了");}}
4、将dao和service配置到xml文件
<property> 用于进行属性注入
name: bean的属性名,通过setter方法获得
setBookDao ##> BookDao ##> bookDao
ref :另一个bean的id值的引用
<?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="bookServiceId" class="com.dianer.lily.tryDI.BookServiceImpl"> <property name="bookDao" ref="bookDaoId"></property> </bean> <bean id="bookDaoId" class="com.dianer.lily.tryDI.BookDaoImpl"> </bean> </beans>
5、写Juint测试类
/** * */package com.dianer.lily.tryDI;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * * * @author LilyLee * @date 2017年6月5日 * @time 上午8:42:59 * @Version 1.0 * @email lilylee_1213@foxmail.com * */public class DI_test {@Testpublic void test() {String xmlPath="com/dianer/lily/tryDI/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);BookService bookService = (BookService) applicationContext.getBean("bookServiceId");bookService.addBook();}}
第一次运行报错:
六月 05, 2017 8:44:44上午 org.springframework.context.support.AbstractApplicationContextprepareRefresh
信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@51b1373d:startup date [Mon Jun 05 08:44:44 CST 2017]; root of context hierarchy
六月 05, 2017 8:44:44上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions
信息: Loading XML beandefinitions from class path resource[com/dianer/lily/tryDI/applicationContext.xml]
错在xml文件里了,因为是用了IOC的xml文件,所以</beans>的位置忘记放到最后了。
第二次运行报错:
六月 05, 2017 8:53:14上午 org.springframework.context.support.AbstractApplicationContextprepareRefresh
信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@69e8c998:startup date [Mon Jun 05 08:53:14 CST 2017]; root of context hierarchy
六月 05, 2017 8:53:15上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML beandefinitions from class path resource[com/dianer/lily/tryDI/applicationContext.xml]
六月 05, 2017 8:53:15上午 org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons
信息: Pre-instantiatingsingletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@4d647e48:defining beans [bookserviceId,bookDaoId]; root of factory hierarchy
依赖注入
没有把所有该打印的信息都打印出来。
看了一下,错误是org.springframework.beans.factory.NoSuchBeanDefinitionException: No beannamed 'bookServiceId' is defined
原来是因为<beanid="bookserviceId"class="com.dianer.lily.tryDI.BookServiceImpl">
这句话写错了,一一对应真的很重要啊!
正确输出:
六月 05, 2017 8:58:44上午 org.springframework.context.support.AbstractApplicationContextprepareRefresh
信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@5ed6d53c:startup date [Mon Jun 05 08:58:44 CST 2017]; root of context hierarchy
六月 05, 2017 8:58:44上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions
信息: Loading XML beandefinitions from class path resource[com/dianer/lily/tryDI/applicationContext.xml]
六月 05, 2017 8:58:44上午 org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons
信息: Pre-instantiatingsingletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@18510b13:defining beans [bookServiceId,bookDaoId]; root of factory hierarchy
被new了
依赖注入: addBook()
- Spring入门案例 DI
- spring入门案例(IoC和DI)
- 入门案例-DI
- Spring(三)入门案例DI(依赖注入)
- Spring(1)初识Spring+IOC(控制反转)+DI(依赖注入)(附:快速入门案例)
- spring入门--spring入门案例
- Spring入门03 - 依赖注入DI
- Spring IOC与DI入门讲解
- spring 入门01--IoC与DI
- Spring入门之依赖注入(DI)
- Spring 中的入门DI(依赖注入)
- Spring入门之setter DI注入
- Spring基础入门之Ioc、DI、AOP
- spring注解入门之IOC,DI
- Spring入门案例
- 《spring---入门案例》
- Spring入门之案例
- spring 入门案例
- Android 动画:你真的会使用插值器与估值器吗?(含详细实例教学)
- python交互模式和命令行模式
- 数据结构之n--n(kruskal算法)
- 一个可随意定位置的带色Toast——开源代码Crouton的简单使用
- 关于QString::toWCharArray 无法解析的外部符号
- Spring入门案例 DI
- 多线程同步和死锁的深入分析
- base64文件解码
- 两数组的交 II
- java发送邮件基于SSL安全协议,dos测试成功发送
- LINQ的学习
- 形式化方法对软件开发的挑战:一些历史与现实
- 学习笔记TF021:预测编码、字符级语言建模、ArXiv摘要
- 深入理解Java中的String