1. 最终目录
2. 代码示例
(1)BookDaoImpl.java 与 BookServiceImpl.java
package com.hung.dao.Impl;
import com.hung.dao.BookDao;
import com.hung.service.Impl.BookServiceImpl;
public class BookDaoImpl implements BookDao {
public void say() {
System.out.println("执行了dao的say()...");
}
}
package com.hung.service.Impl;
import com.hung.dao.BookDao;
import com.hung.dao.Impl.BookDaoImpl;
import com.hung.service.BookService;
public class BookServiceImpl implements BookService {
// 5.业务层中使用不使用new方式创建dao对象
// private BookDaoImpl bookDao = new BookDaoImpl();
private BookDao bookDao;
// 7.提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void say() {
System.out.println("执行了service的say()...");
// 执行dao的say()
bookDao.say();
}
}
(2)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring-IoC</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(3)applicationContext.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">
<!-- 1.在pom文件导入spring坐标:spring-context;创建此配置文件 -->
<!-- 2.IoC: 配置bean,解耦:解决new对象的藕合问题 -->
<!-- 2.1 除id外,还可以用name设别名,别名可多个,作用和id基本等同 -->
<bean id="bookDao" name="dao myDao" class="com.hung.dao.Impl.BookDaoImpl"/>
<!-- 2.2 还可以用scope设置bean创建时为非单例
非单例耗性能,因此:
适合给容器管理的bean: 表现层、业务层、数据层、工具 对象
不适合的: 封装的实体对象
<bean id="bookDao" name="dao myDao" class="com.hung.dao.Impl.BookDaoImpl" scope="prototype" />
-->
<bean id="bookService" class="com.hung.service.Impl.BookServiceImpl">
<!-- 6.DI: 配置service与dao的关系 -->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
(4)App.java
package com.hung;
import com.hung.service.BookService;
import com.hung.service.Impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
/**
* 测试
* 不用spring
* new一个service,并执行它的say()
*/
/*
BookServiceImpl bookService = new BookServiceImpl();
bookService.say();
*/
/**
* 使用spring方法
*/
// 3.获取IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 4.获取bean,主意:得到的是Object类型,需强转换成BookService类型
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.say();
}
}
标签:DI,Spring,dao,say,com,hung,import,IoC,public From: https://www.cnblogs.com/suihung/p/16608552.html