Spring框架认识(一)

1.Java Spring框架是什么?

Spring是分层的【控制层/业务层/数据访问层】

以IoC(inverse of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)以内核轻量级开源框架

Spring以IoC和AOP微内核,包含了开发java程序所需要的各种技术的轻量级开源容器框架。

2.Spring他有哪些好处?

1)方便解耦,简化开发

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

2)方便集成各种优秀的框架

Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架的支持(Mybatis Sturuts等等)

3) 降低了JavaEE API的使用难度

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

4)方便程序测试

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

5)AOP编程的支持

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

6)声明式事务支持

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

3.Spring体系结构

 1.Data Access/Integration
    数据访问/集成层包括 JDBC、ORM、OXM、JMS 和 Transactions 模块    
    JDBC 模块:提供了一个 JDBC 的抽象层,大幅度减少了在开发过程中对数据库操作的编码
    ORM 模块:对流行的对象关系映射 API,包括 JPA、JDO、Hibernate 和 MyBatis 提供了的集成层
    OXM 模块:提供了一个支持对象/XML 映射的抽象层实现,如 JAXB、Castor、XMLBeans、JiBX 和 XStream。
    JMS 模块:指 Java 消息服务,包含的功能为生产和消费的信息。
    Transactions 事务模块:支持编程和声明式事务管理实现特殊接口类,并为所有的 POJO。
    2.Web 模块
    Spring 的 Web 层包括 Web、Servlet、Struts 和 Portlet 组件
    Web 模块:提供了基本的 Web 开发集成特性,例如多文件上传功能、使用的 Servlet 监听器的 IoC 容器初始化以及 Web 应用上下文。
    Servlet模块:包括 Spring 模型—视图—控制器(MVC)实现 Web 应用程序。
    Struts 模块:包含支持类内的 Spring 应用程序,集成了经典的 Struts Web 层。
    Portlet 模块:提供了在 Portlet 环境中使用 MVC实现,类似 Web-Servlet 模块的功能。
    3.AOP模块
    Spring的其他模块还有 AOP、Aspects、Instrumentation
    AOP 模块:提供了面向切面编程实现,允许定义方法拦截器和切入点,将代码按照功能进行分离,以降低耦合性。
    Aspects 模块:提供与 AspectJ 的集成,是一个功能强大且成熟的面向切面编程(AOP)框架。
    Instrumentation 模块:提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用。
    4.Core Container(核心容器)
    Spring 的核心容器是其他模块建立的基础
    Beans 模块:提供了 BeanFactory,是工厂模式的经典实现,Spring 将管理对象称为 Bean。[Spring将被管理的元素都称之为Bean【组件】]
    Core 核心模块:提供了 Spring 框架的基本组成部分,包括 IoC【控制反转】 和 DI【依赖注入】 功能。
    Context 上下文模块:建立在核心和 Beans 模块的基础之上,它是访问定义和配置任何对象的媒介。ApplicationContext 接口是上下文模块的焦点。
    Expression Language 模块:是运行时查询和操作对象图的强大的表达式语言。
    5.Test模块
        支持 Spring 组件,使用 JUnit 或 TestNG 框架的测试。

例子:

第一个Spring程序

1.创建Maven的java项目,完善结构,导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>

核心文件

spring-aop:5.1.5.RELEASE
spring-beans:5.1.5.RELEASE
spring-context:5.1.5.RELEASE
spring-core:5.1.5.RELEASE
spring-expression:5.1.5.RELEASE
spring-jcl:5.1.5.RELEASE

2.创建java类

public class StudentBean {
    public  void  testStudent(){
        System.out.println("StudentBean的实例方法");
    }
}

3.在resources下创建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 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="stu" class="com.wangxing.demo1.bean.StudentBean"></bean>
</beans>

beans---根元素
    【在Spring中,Spring认为所有的java元素[类,接口...]对于Spring来说都是一个javabean】
    在Spring中配置大部分元素都是bean,包含bean元素的根元素就是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"   
是命名空间和命名空间地址beans,有了这个beans的命名空间和命名空间地址,在beans中配置元素bean的时候就会自动提示。【在beans中根元素中可以使用bean元素】

<bean id="stu" class="com.wangxing.demo1.bean.StudentBean"></bean>
等价于 StudentBean  stu=new StudentBean(); 创建StudentBean类的对象
告诉Spring框架/容器创建一个StudentBean类的对象。

id="stu":对象名称
class="com.wangxing.demo1.bean.StudentBean"=>被创建对象的java类

4.创建测试类

//根据applicationContext.xml创建Spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring容器中得到对象,调用实例方法,以证明Spring创建好了对象 。
StudentBean stu = ac.getBean("stu",StudentBean);
stu.testStudent();

结论:

我们在使用某一个java类的时候不会在自己new对象了,而是由Spring去给我们创建和维护对

Spring容器对象

1.BeanFactory是基础类型的Spring容器

org.springframeword.beams.facytory.BeanFactory的接口

Beanfactory就是一个管理Bean的工程岗,它主要负责初始化各种Bean,并调用他们的生命周期。

BeanFactory接口有多个实现类,最常见的是

org.springframework.beans.facotroy.xml.XmlBeanFactory

他是根据XML配置文件中的定义装配Bean的,现在被标注已过,不推荐使用。

BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("绝对路径"));

StudentBean stu = beanFactory.getBean("stu",StudentBean.class);

2.ApplicationContext是Spring容器

ApplicationContext是BeanFactory的子接口

不仅提供了BeanFactory的所有功能,还添加了对i18n(国际化)、资源访问,事件传播        的良好支持。

ApplicationContext接口有两个常用的实现类

        2.1ClassPathXmlApplicationContet类

        该类从类路径ClassPath中寻找指定的xml文件,找到并装载,完成ApplicationContext实例化工作。

        类路径=>resources目录。

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentBean studentBean= ac.getBean("stu", StudentBean.class);

        2.2FileSystemXmlApplicationContext类

                该类从指定的文件系统路径中寻找指定的 XML 配置文件,找到并装载完成ApplicationContext 的实例化工作

                文件系统路径:结对路径

         ApplicationContext ac=new FileSystemXmlApplicationContext("F:\\20210907\\ideaproject\\springdemo1\\src\\main\\resources\\applicationContext.xml");
        StudentBean studentBean= ac.getBean("stu", StudentBean.class);

通常在Java项目中,会采用通过 ClassPathXmlApplicationContext 类实例化 ApplicationContext 容器的方式

在 Web 项目中,ApplicationContext容器的实例化工作会交由Web服务器完成。
Web 服务器实例化 ApplicationContext 容器通常使用基于ContextLoaderListener 实现的方式

<!--指定Spring配置文件的位置,有多个配置文件时,以逗号分隔-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <!--spring将加载spring目录下的applicationContext.xml文件-->
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>
<!--指定以ContextLoaderListener方式启动Spring容器-->
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

注意:BeanFactory和ApplicationContext都是通过XML配置文件加载bean,二者的主要区别在于,如果Bean的某一个属性没有注入,则使用BeanFactory加载后,在第一次调用getBean时会抛出异常,而ApplicationContext则在初始化时自检,这样有利于检查依赖的属性是否注入。因此,在实际的开发中使用ApplicationContext,而只有在资源较少时,才考虑BeanFactory

 无奈源于不够强大

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值