Spring 入门之 IOC与 DI

Spring 入门之 IOC与 DI,第1张

IOC(Inversion of Control)控制反转

使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转。


案例演示:

  1. 导入maven 依赖
<dependency>       
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>5.3.18version>
dependency>
  1. 创建book类
package com.daidai.domain;

public class Book {
    private Integer id;
    private String title;
    private String author;

    public Book() {
    }

    public Book(Integer id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void addBook(){
        System.out.println("addBook...");
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
  1. 创建 Spring 配置文件 ApplicationContext.xml

<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="book" class="com.daidai.domain.Book"/>

beans>
  1. 创建book对象,由ioc容器创建
package com.daidai.app;

import com.daidai.domain.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

        Book book = context.getBean("book", Book.class);

        book.addBook();

    }
}
DI(Dependency Injection)依赖注入

在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入

业务层要用数据层的类对象,以前是自己new的,现在自己不new了,靠别人[外部其实指的就是IOC容器]来给注入进来。这种思想就是依赖注入。


案例演示:

  1. 创建BookDao, BookService接口及其实现类。
package com.daidai;

public interface BookDao {
    void save();
}
package com.daidai;

public interface BookService {
    void startService();
}
package com.daidai.impl;

import com.daidai.BookDao;

public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("bookdao...");
    }
}
package com.daidai.impl;

import com.daidai.BookDao;
import com.daidai.BookService;

public class BookServiceImpl implements BookService {

    BookDao bookDao;

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void startService() {
        System.out.println("bookservice...");
        bookDao.save();
    }
}
  1. 创建ApplicationContext.xml

<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.daidai.impl.BookDaoImpl"/>
    <bean id="bookService" class="com.daidai.impl.BookServiceImpl">
        <property name="bookDao" ref="bookDao"/>
    bean>
beans>
  1. 创建测试
package com.daidai.app;

import com.daidai.impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        BookServiceImpl bookService = context.getBean("bookService", BookServiceImpl.class);
        bookService.startService();
    }
}


管理第三方 bean
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="000000"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    bean>
package com.daidai.apps;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App2 {
    //管理第三方bean
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContent.xml");
        DruidDataSource dataSourceDruid = context.getBean("druidDataSource", DruidDataSource.class);

        System.out.println(c3p0DataSource);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/877571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存