Home > Back-end >  Strong turn failure, sincerely for advice...
Strong turn failure, sincerely for advice...

Time:11-29

//test
Package com. Itheima. A_dii;

The import org. Junit. Test;
The import org. Springframework. Context. ApplicationContext;
The import org. Springframework. Context. Support. ClassPathXmlApplicationContext;

Public class TestDI {
@ Test
Public void demo02 () {
String xmlPath="com/itheima a_di/beans. XML";
//load the configuration file, execute the statement in the configuration file
ApplicationContext ApplicationContext=new ClassPathXmlApplicationContext (xmlPath);

BookService BookService=(applicationContext BookService) getBean (" bookServiceId ");
/* *
* when performing the same time, find UserService first instance class
* due to just perform the configuration file, has injected the dao to servic, so you can call the same method in BookServiceImpl
* due to call the same method of BookDao adddBook method, so the code can perform adddBook
*/
BookService. Same ();

}
}


//beans. The XML configuration file
<? The XML version="1.0" encoding="utf-8"?>
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 "& gt;
Simulation of spring execution
Create a service instance: BookService BookService=new BookServiceImpl (IoC) & lt; Bean>
Create dao instance: BookDao BookDao=new BookDaoImple (IoC)
Set the dao to service: bookService setBookDao (bookDao); DI & lt; Bean> The child tags: & lt; Property>
! -->

Name: bean property name, through a setter method for
BookServiceImpl SetBookDao method in the properties of bookDao
Ref: another bean id value of reference
-->


The execution of the property name, proof of the need for a new BookDao, then go to create a dao instance
After creating the dao, and then to create the service, after creating the service execution ref, see need not to need the dao
Results need to dao id into the ref, the dao layer into the
in the serv-->





  • Related