目录
7.App2(不完全spring方式实现调用,没有解决耦合问题)main
8.applicationContext.xml Spring配置文件
三.DI入门案例思路分析 (在IOC内部将有依赖关系的bean进行关系绑定(DI)(Xml版))
2.Service中使用new形式创建的Dao对象是否保留?
3.Service中需要的Dao对象如何进入到Service中
2.在业务层中Service中给数据层Dao对象提供一个Setter方法当作入口,(这个Setter方法时容器在调用)
3.在Spring配置文件之中使用(业务层)对象Service与Dao(数据层)Bean之间的关系(也就是使用DI进行绑定)(在业务层之中写,因为Dao数据一直都是Service进行的接受)
1.管理什么?
(Service与Dao)bean
2.如何将被管理的对象告知IOC容器?
利用配置的方式告诉他,
3.被管理的对象交给IOC容器,如何获取到IOC容器?(如何获取IOC容器)
Spring会提供一个接口
4.IOC容器得到后,如何从容器中获取bean?
还是接口的方法拿出来
5.使用Spring导入哪些坐标?(maven里面的pom.xml)

- package com.itheima.dao;
-
- public interface BookDao {
-
- public void save();
- }
- package com.itheima.dao.impl;
-
- import com.itheima.dao.BookDao;
-
- public class BookDaoImpl implements BookDao {
-
-
- @Override
- public void save() {
- System.out.println("Book Dao save 哈哈...");
- }
- }
- package com.itheima.service;
-
- public interface BookService {
-
- public void save();
- }
- package com.itheima.service.impl;
-
- import com.itheima.dao.BookDao;
- import com.itheima.dao.impl.BookDaoImpl;
- import com.itheima.service.BookService;
-
- public class BookServiceImpl implements BookService {
-
- private BookDao bookDao = new BookDaoImpl();
-
- @Override
- public void save() {
- System.out.println("book service save");
- bookDao.save();
- }
- }
- package com.itheima;
-
- import com.itheima.service.BookService;
- import com.itheima.service.impl.BookServiceImpl;
-
- public class App {
-
- public static void main(String[] args) {
-
- BookService bookService = new BookServiceImpl();
-
- bookService.save();
-
- }
- }
- package com.itheima;
-
- import com.itheima.dao.BookDao;
- import com.itheima.service.BookService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class App2 {
- public static void main(String[] args) {
-
- //获取IOC容器
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-
- //获取容器中的对bean对象 要的是bean的id,转成BookDao类
- BookDao bookDao = (BookDao) ctx.getBean("bookDao");
- BookService bookService = (BookService) ctx.getBean("BookService");
- //调用
- bookDao.save();
- bookService.save();
- }
- }
bean标签表示配置bean
id属性标签表示给bean起名字
class标签表示给bean定义类型
- "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="com.itheima.dao.impl.BookDaoImpl" >bean>
-
- <bean id="BookService" class="com.itheima.service.impl.BookServiceImpl">bean>
-
-
-
-
-
- beans>
- "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.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>Spring-demo-01artifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>18maven.compiler.source>
- <maven.compiler.target>18maven.compiler.target>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.10.RELEASEversion>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
- dependencies>
-
- project>

IOC管着Bean
不写实现类(否则耦合度高)
提供一个进入的入口(方法),
写配置使其Spring知道它们之间的关系
- //删除业务层Service中使用new的方式创建的数据层Dao对象
- private BookDao bookDao;
- //提供对应的set方法
- public void setBookDao(BookDao bookDao) {
- this.bookDao = bookDao;
- }
配置Service与Dao的关系
property标签表示当前Bean的属性
name属性表示配置哪一个具体的属性()
ref属性表示参照哪一个Bean(表示与Bean中的哪一个对象进行绑定)
- <bean id="BookService" class="com.itheima.service.impl.BookServiceImpl">
- <property name="bookDao" ref="bookDao1">property>
- bean>
(案例结构和撒上面的没有变化,仅仅有几个文件发生了改变,所以这次我只写发生变化的)
- package com.itheima.service.impl;
-
- import com.itheima.dao.BookDao;
- import com.itheima.service.BookService;
-
- public class BookServiceImpl implements BookService {
-
- //删除业务层中使用new的方式创建的Dao对象
- private BookDao bookDao;
-
- //提供对应的set方法
- public void setBookDao(BookDao bookDao) {
- this.bookDao = bookDao;
- }
-
- @Override
- public void save() {
- System.out.println("book service save");
- bookDao.save();
- }
- }
- "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="bookDao1" class="com.itheima.dao.impl.BookDaoImpl" >bean>
-
- <bean id="BookService" class="com.itheima.service.impl.BookServiceImpl">
- <property name="bookDao" ref="bookDao1">property>
- bean>
-
-
-
-
-
- beans>