Spring学习笔记(一)

Spring框架学习笔记

经过了两个多月的学习,今天终于进入了Spring框架的学习,Spring作为JavaEE的灵魂框架,其重要性毋庸置疑,所以在此对学习Spring的过程做一些记录。

Spring简介

Spring是分层的Java SE/EOE应用full-stack轻量级开源框架,以IOC和AOP为内核。

  • full-stack:这里的全栈并不是我们平常所理解的意思,它是指Spring在各层上都有对应的解决方案,web层对应有SpringMVC,service层对应有SpringIOC,dao层对应有SpringJdbcTemplate。
  • IOC:Inversion Of Controller 控制反转 将创建Bean的控制权反转给Spring
  • AOP:Aspect Oriented Programming

Spring提供了展现层SpringMVC和持久层SpringJDBCTemplate以及业务层事务管理等众多的企业级开发技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的JavaEE企业应用开源框架。

Spring的优势

  1. 方便解耦,简化开发
  2. AOP编程的支持
  3. 声明式事务的支持
  4. 方便程序的测试
  5. 方便集成各种优秀的框架
  6. 降低了Java EE API 的使用难度
  7. Spring 的Java源码是经典学习范例

Spring快速入门的开发步骤

  1. 导入坐标
    在项目的pom.xml中添加如下依赖
<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

2.创建Bean
Bean的成员变量可以分为三类:普通数据类型、引用数据类型、集合数据类型

/**
 * Teacher类
 */
public class Teacher {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

//定义StudentDao接口
public interface StudentDao {
    public void study();
}
import com.fxq.dao.StudentDao;
import com.fxq.domain.Teacher;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class StudentDaoImpl implements StudentDao {
    //定义普通数据类型的成员变量
    private String name;
    //定义存储普通数据类型的List集合
    private List<String> strList;
    //定义存储引用数据类型的List集合
    private List<Teacher> teacherList;
    //定义Map集合,键为String 值为Teacher
    private Map<String,Teacher> teacherMap;
    //定义Properties集合,键值都为String
    private Properties properties;

    public StudentDaoImpl(String name, List<String> strList, List<Teacher> teacherList, Map<String, Teacher> teacherMap, Properties properties) {
        this.name = name;
        this.strList = strList;
        this.teacherList = teacherList;
        this.teacherMap = teacherMap;
        this.properties = properties;
    }

    public StudentDaoImpl() {
    }

   

    public void setName(String name) {
        this.name = name;
    }

  

    public void setStrList(List<String> strList) {
        this.strList = strList;
    }

    

    public void setTeacherList(List<Teacher> teacherList) {
        this.teacherList = teacherList;
    }

   

    public void setTeacherMap(Map<String, Teacher> teacherMap) {
        this.teacherMap = teacherMap;
    }

   

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void study() {
        System.out.println(strList);
        System.out.println(teacherList);
        System.out.println(teacherMap);
        System.out.println(properties);
        System.out.println("我在学习");
    }
}

  1. 创建applicationContext.xml配置文件

在这里插入图片描述
在resources目录下创建三个配置文件,在applicationContext.xml中使用import标签引入applicationContext-dao.xml,在applicationContext-dao.xml中引入applicationContext-domain.xml
4. 在配置文件进行配置

!--  applicationContext.xml  引入外部配置文件-->
    <import resource="applicationContext-dao.xml"/>
    <bean id="studentService" class="com.fxq.service.impl.StudentServiceImpl">
        <property name="sdao" ref="studentDao"/>
    </bean>
<!--    引入applicationContext-domain.xml-->
    <import resource="applicationContext-domain.xml"/>

    <bean id="studentDao" class="com.fxq.dao.impl.StudentDaoImpl">
        <property name="name" value="zhangsan"/>
        <property name="strList">
            <list>
                <!-- 普通数据类型直接yongvalue-->
                <value>Chinese</value>
                <value>math</value>
            </list>
        </property>
        <property name="teacherList">
            <list>
<!-- list中存储的引用类型-->
<!--需要用ref引用在applicationContext-domain.xml中创建的bean-->
               <ref bean="teacher"/>
                <ref bean="teacher2"/>
            </list>
        </property>
<!--        配置map集合并注入-->
        <property name="teacherMap">
            <map>
                <entry key="1" value-ref="teacher"/>
                <entry key="2" value-ref="teacher2"/>
            </map>
        </property>
<!--        配置properties集合并注入-->
        <property name="properties">
            <props>
                <prop key="3">three</prop>
                <prop key="4">four</prop>
            </props>
        </property>
    </bean>
  1. 创建ApplicationContext的对象,调用getBean方法获取Bean
//定义StudentService接口
public interface StudentService {
    public void study();
}

//定义studentService接口实现类
import com.fxq.dao.StudentDao;
import com.fxq.service.StudentService;

public class StudentServiceImpl implements StudentService {
    private StudentDao sdao;

    public StudentServiceImpl(StudentDao sdao) {
        this.sdao = sdao;
    }

    public StudentServiceImpl() {
    }

    public void setSdao(StudentDao dao){
        this.sdao = dao;
    }

    public void study() {
        sdao.study();
    }
}
import com.fxq.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class StudentController {
    public static void main(String[] args) {
        //获取StudentService对象
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app = new FileSystemXmlApplicationContext("E:\\IdeaProjects\\JavaWeb_code\\frame_spring\\src\\main\\resources\\applicationContext.xml");
//        StudentService studentService = (StudentService) app.getBean("studentService");
        StudentService studentService = app.getBean(StudentService.class);
        studentService.study();
    }

}

在StudentController中创建ApplicationContext容器,并通过getBean方法获取StudentService对象,并调用方法

ApplicationContext的实现类

ApplicationContext有3个实现类:

  1. ClassPathXmlApplicationContext (推荐)
    从类的根路径下加载配置文件

  2. FileSystemXmlApplicationContext
    从磁盘中加载配置文件,配置文件可以在磁盘的任意位置

  3. AnnotationConfigApplicationContext
    注解开发

getBean() 方法

  1. getBean(String name)
    参数:配置文件bean的id
    当配置文件中出现多个同类型的bean时也可以使用
  2. getBean(Class aClass)
    参数:Bean的字节码对象
    只能在文件中同类型的bean只有一个时才能使用

Spring创建Bean的三种方式

  1. 调用构造器创建,使用如下标签
 <constructor-arg name="xxx" ref="xxx"/>`
  1. 工厂静态方法创建,首先创建工厂类,定义静态方法,使用如下标签
import com.fxq.dao.UserDao;
import com.fxq.dao.impl.UserDaoImpl;

public class StaticFactory {
    public static UserDao getUserDao(){
        return new UserDaoImpl();
    }
}
<!--    工厂静态方法创建Bean-->
    <bean id="userDao" class="com.fxq.factory.StaticFactory" factory-method="getUserDao"></bean>
  1. 工厂实例方法创建,定义成员方法,使用如下标签
import com.fxq.dao.UserDao;
import com.fxq.dao.impl.UserDaoImpl;

public class DynamicFactory {
    public UserDao getUserDao(){
        return new UserDaoImpl();
    }
}

<!--    工厂实例方法创建Bean-->
    <!--<bean id="factory" class="com.fxq.factory.DynamicFactory"></bean>
    <bean id="xxx" factory-bean="factory" factory-method="getUserDao"></bean>-->

配置文件标签

bean标签

  1. id属性:在容器中Bean实例的唯一标识,不允许重复
  2. class属性:要实例化的Bean的全限定名
  3. scope属性:Bean的作用范围
    singleton(默认): 单例
    创建Bean的时机:在创建容器的时候创建Bean,多次创建的 Bean是同一个
    prototype:多例
    创建Bean的时机:每次调用getBean方法时都会创建一个不同的Bean

property标签

  1. name属性:属性名称
  2. value属性:注入的普通属性值
  3. ref属性:注入的对象引用值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值