Spring基础

本文深入介绍了Spring框架的主要作用,包括解耦、AOP支持、声明式事务管理、测试便捷性以及对其他框架的集成。通过一个简单的Spring Demo展示了如何配置ApplicationContext,创建Bean,并通过XML配置文件实现DAO层的实例化。此外,还探讨了两种对象注入方式——构造方法和setter注入,强调了Spring如何降低程序耦合性并提高测试效率。
摘要由CSDN通过智能技术生成

Spring作用

方便解耦,简化开发

Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理

AOP编程的支持

Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

声明式事务的支持

只需要通过配置就可以完成对事务的管理,而无需手动编程

方便程序的测试

Spring对Junit的支持,可以通过注解方便的测试Spring程序

方便集成各种优秀框架

Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持

降低JavaEE API的使用难度

Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。

IOC

spring最重要的就是IOC(控制反转)和AOP(面向切面),控制反转的意思就是将对象交给容器来管理,而不是在我们需要的时候new一个。

谁控制谁,控制什么:传统Java SE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而IoC是有专门一个容器来创建这些对象,即由Ioc容器来控制对象的创建;谁控制谁?当然是IoC 容器控制了对象;控制什么?那就是主要控制了外部资源获取(不只是对象包括比如文件等)。
为何是反转,哪些方面反转了:有反转就有正转,传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;而反转则是由容器来帮忙创建及注入依赖对象;为何是反转?因为由容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象,所以是反转;哪些方面反转了?依赖对象的获取被反转了。

Spring中的ApplicationContext接口和ClassPathXmlApplicationContext

a:相当于一个bean工厂,用于管理,装配对象。
c:用于加载xml文件。

第一个spring demo

1.先添加依赖

<dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.3</version>
        </dependency>
    </dependencies>

2.分别创建dao和daoimpl

package edu.xja.dao;
package edu.xja.dao.impl;

import edu.xja.dao.CustomerDao;

public class CustomerDaoImpl implements CustomerDao {

    @Override
    public void save() {
        System.out.println("存入数据了");
    }
}
public interface CustomerDao {
    void save();
}

3.创建application.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <!-- 创建 CustomerDaoImpl对象-->
    <bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>

</beans>

4.创建测试类:

 @Test
    void save() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
       CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
        customerDao.save();
        ClassPathXmlApplicationContext c = (ClassPathXmlApplicationContext) applicationContext;
        c.close();
    }

结果
在这里插入图片描述
好处:省去了在类里面new,降低程序的耦合性

在应用中mvc中,serviceimpl中通常要实例化一个daoimpl,测试也要实例化一个serviceimpl,这时有两种方法创建对象
第一种:利用构造方法创建对象
1.配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <!-- 创建 CustomerDaoImpl对象-->
    <bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>
    <bean id="customerService" class="edu.xja.service.impl.CustomerServiceImpl">
<!--        通过构造方法创建对象-->
        <constructor-arg index="0" ref="customerDao"></constructor-arg>
<!--        <property name="customerDao" ref="customerDao"></property>-->
    </bean>
</beans>

2.创建service和serviceimpl

package edu.xja.service;

public interface CustomerService {
    void save();
}

package edu.xja.service.impl;

import edu.xja.dao.CustomerDao;
import edu.xja.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao;

    public CustomerServiceImpl() {
    }

    public CustomerServiceImpl(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    public CustomerDao getCustomerDao() {
        return customerDao;
    }

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public void save() {
        customerDao.save();
    }
}

3.创建测试类

package edu.xja.service.impl;

import edu.xja.service.CustomerService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.jupiter.api.Assertions.*;

class CustomerServiceImplTest {

    @Test
    void save() {
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
        customerService.save();
    }
}

第二种:通过set方法注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <!-- 创建 CustomerDaoImpl对象-->
    <bean id="customerDao" class="edu.xja.dao.impl.CustomerDaoImpl"></bean>
    <bean id="customerService" class="edu.xja.service.impl.CustomerServiceImpl">
    <property name="customerDao" ref="customerDao"></property>
    </bean>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值