1.使用xml的方式进行配置IOC容器,首先引入依赖
在Resource资源下配置,applicationContext.xml ,刷新mevan后可以直接选择配置spring.xml文件
XML
<!-- spring核心用来管理bean -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
applicationContext.xml
配置bean:
id:实现标识给bean起名字
class属性标识给这个bean对象定义类型
name:属性是可以给当前bean起别名(使用" ",",",";"分隔开来)
singleton:单例(默认)
prototype:非单例
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/>
<bean id="bookService" class="org.itheima.service.impl.BookServiceImpl"/>
</beans>
现在配置DAO层:
java
public interface BookDAO {
public void book();
}
java
public class BookDAOImpl implements BookDAO {
@Override
public void book() {
System.out.println("BookDAO......");
}
}
Service层:
java
public interface BookService {
public void write();
}
java
public class BookServiceImpl implements BookService {
private BookDAO bookDAO = new BookDAOImpl();
@Override
public void write() {
System.out.println("BookService......");
}
}
测试文件1:
java
public class BookDAOTest {
@Test
public void book() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDAO bookDAO = (BookDAO) ctx.getBean("bookDAO");
bookDAO.book();
}
}
打印结果:
测试文件2:
java
public class BookServiceTest {
@Test
public void write() {
ClassPathXmlApplicationContext txc = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) txc.getBean("bookService");
bookService.write();
}
}
打印结果:
依赖 new ClassPathXmlApplicationContext("applicationContext.xml")的方式不太合适,同样是创建对象的形式,那不如直接new一个实体DAO,因此此方法需要改进!!
2.使用dao与Service的关系
与以上不同的是BookServiceImpl与测试类不同:
java
public class BookServiceImpl implements BookService {
private BookDAO bookDAO = new BookDAOImpl();
@Override
public void write() {
bookDAO.book();
System.out.println("BookService......");
}
}
测试类:
java
public class BookServiceImplTest {
@Test
public void testWrite() {
ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) cax.getBean("bookService");
bookService.write();
}
}
测试结果:
3.使用DI注入 bean实例,并且使用service 调用dao层方法
首先配置Service的依赖关系properties
properties:配置当前bean的属性
name属性标识配置哪一个具体的属性
ref属性表示参照哪个bean对象
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/>
<bean id="bookService" class="org.itheima.service.impl.BookServiceImpl">
<property name="bookDAO" ref="bookDAO"/>
</bean>
</beans>
dao层:
java
public interface BookDAO {
public void book();
}
java
public class BookDAOImpl implements BookDAO {
@Override
public void book() {
System.out.println("BookDAO......");
}
}
service层:
java
public interface BookService {
public void write();
}
通过容器将bookDAO对象传入进去
java
public class BookServiceImpl implements BookService {
private BookDAO bookDAO;
@Override
public void write() {
bookDAO.book();
System.out.println("BookService......");
}
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
}
测试类:
java
public class BookServiceImplTest {
private BookService bookService;
@Test
public void write() {
ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) cax.getBean("bookService");
bookService.write();
}
}
测试结果: