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()

原创粉丝点击