posts - 206,  comments - 26,  views - 17万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

一、IOC案例
实现步骤:
第一步 、使用Maven导入Spring坐标

<dependencies>
    <!--导入spring的坐标spring-context-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>

 


第二步、定义Spring管理的类(接口)

BookDao接口和BookDaoImpl实现类:

复制代码
public interface BookDao {
    public void save();
}
 
public class BookDaoImpl implements BookDao {
    public void save() {
        System.out.println("book dao save ...");
    }
}
复制代码

 


BookService接口和BookServiceImpl实现类:

复制代码
public interface BookService {
    public void save();
}
 
public class BookServiceImpl implements BookService {
    private BookDao bookDao = new BookDaoImpl();
    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
    }
}
复制代码

 


第三步、创建Spring配置文件,配置对应类作为Spring管理的bean对象

定义applicationContext.xml配置文件并配置BookServiceImpl

复制代码
<?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标签:表示配置bean
        id属性:表示给bean起名字
        class属性:表示给bean定义类型
    -->
    <bean id="bookService" class="com.it.service.impl.BookServiceImpl"></bean>
 
</beans>
复制代码

 


==☆☆注意事项:bean定义时id属性在同一个上下文中(IOC容器中)不能重复☆☆==

第四步、初始化IOC容器(Spring核心容器/Spring容器),通过容器获取Bean对象

 

复制代码
public class App {
    public static void main(String[] args) {
        //1.创建IoC容器对象,加载spring核心配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2 从IOC容器中获取Bean对象(BookService对象)
        BookService bookService= (BookService)ctx.getBean("bookService");
        //3 调用Bean对象(BookService对象)的方法
        bookService.save();
    }
}
复制代码

 


运行测试成功。打印service层和dao层的save方法。

 

二、DI案例
第一步、删除使用new的形式创建对象的代码


public class BookServiceImpl implements BookService {
    private BookDao bookDao;  //【第一步】删除使用new的形式创建对象的代码
    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
    }
}

 

 

第二步、提供依赖对象对应的setter方法

复制代码
public class BookServiceImpl implements BookService {
    private BookDao bookDao;
    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
    }
    //【第二步】提供依赖对象对应的setter方法
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }
}
复制代码

 


第三步、配置service与dao之间的关系

在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">
    <!--
        bean标签:表示配置bean
        id属性:表示给bean起名字
        class属性:表示给bean定义类型
    -->
    <bean id="bookDao" class="com.it.dao.impl.BookDaoImpl"/>
 
    <bean id="bookService" class="com.it.service.impl.BookServiceImpl">
        <!--配置server与dao的关系
            property标签:表示配置当前bean的属性
            name属性:表示配置哪一个具体的属性
            ref属性:表示参照哪一个bean
        -->
        <property name="bookDao" ref="bookDao"/>
    </bean>
</beans>
复制代码

 

 

运行测试成功。打印service层和dao层的save方法。

posted on   努力--坚持  阅读(22)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示