Spring IOC控制反转、DI注入以及配置

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();
    }
}

测试结果:

相关推荐
y先森5 小时前
CSS3中的伸缩盒模型(弹性盒子、弹性布局)之伸缩容器、伸缩项目、主轴方向、主轴换行方式、复合属性flex-flow
前端·css·css3
前端Hardy5 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189115 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
sun0077005 小时前
ubuntu dpkg 删除安装包
运维·服务器·ubuntu
oi776 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
IT女孩儿6 小时前
CSS查缺补漏(补充上一条)
前端·css
吃杠碰小鸡7 小时前
commitlint校验git提交信息
前端
学Linux的语莫7 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible
Onlooker1297 小时前
云服务器部署WebSocket项目
服务器