Spring_1

1.什么是Spring?

Spring—分层的解决业务逻辑层与其他各层之间的松耦合问题的javaSE/EE的一站式开源框架。
1.分层的
2.javaSE/EE一站式
3.开源的
4.解决业务逻辑层与其他各层之间的松耦合问题,所以将面向接口的编程思想整个框架。
松耦合问题—低耦合,高内聚原则
耦合–简单的理解成类与类之间的联系
内聚–简单的理解成将实现某一个功能的程序集中在一起。
好处:不用牵一发而动全身。那里有错改那里
目的:为了提高代码的重用性,便于维护。
2.Spring的结构组成

上面是Spring官方提供的结构图
从下往上test[测试],core容器,aop【面向切面编程】,web,data access
1.test部分只有一个模块:
spring-test.jar:spring测试,提供junit与mock测试功能.
2.core容器包含4个模块
spring-core:依赖注入IoC与DI的最基本实现
spring-beans:Bean工厂与bean的装配
spring-context:spring的context上下文即IoC容器
spring-expression:spring表达式语言
3.aop[面向切面]部分包含4个模块
spring-aop:面向切面编程
spring-aspects:集成AspectJ
spring-instrument:提供一些类级的工具支持和ClassLoader级的实现,用于服务器spring-instrument-tomcat:针对tomcat的instrument实现
4.web部分包含4个模块
spring-web:基础web功能,如文件上传
spring-webmvc:mvc实现
spring-webmvc-portlet:基于portlet的mvc实现
spring-struts:与struts的集成,不推荐,spring4不再提供
5.data access部分包含5个模块
spring-jdbc:jdbc的支持
spring-tx:事务控制
spring-orm:对象关系映射,集成orm框架
spring-oxm:对象xml映射
spring-jms:java消息服务
3.Spring的优点
1.方便解耦,简化开发:Spring是一个超级工厂(超级容器),可以将对象的创建和依赖关系交给Spring工厂去管理
2.AOP编程:Spring提供面向切面编程,可以方便的对程序进行运行监控、权限验证等操作
3.声明事务:只需要通过配置就可以完成对事务的管理,不需要手动编程
4.方便测试:Spring支持junit4,可以通过Spring注解方式测试程序
5.方便集成各种框架:Spring支持各种开源框架的集成。例如(struts、Hibernate、MyBaties等)
6.降低JavaEE API的使用难度: Spring对JavaEE开发中非常难用的API进行封装,使这些开发API应用难度降低。
4.Spring的核心技术
1.IoC(Inverse of Control 反转控制):将java对象创建和维护权利交由Spring工厂进行管理和维护。
2.DI(依赖注入):将某一个java类中的依赖对象快速的添加到另一个java类中。
3.AOP(Aspect Oriented Programming 面向切面编程),基于动态代理的功能增强方式[给自己的程序中添加一些系统需求的处理【日志管理,数据的安全性检查…】]。
4.事务管理的相关操作。
5.spring整合/管理其他各层的框架【Spring集成web层SpringMVC/Spring整合数据访问层MyBatis】{SSM}
5.Spring的IoC(Inverse of Control 反转控制)
没有使用IoC(Inverse of Control 反转控制)时

public  class  Student{
public  void  getStuInfo(){
System.out.println(“Student类的实例方法”)}
}
//测试类中访问Student类中的getStuInfo
public class  TestMain{
  public static  void  main(String  args){
      Student  stu=new Student();
      stu.getStuInfo();
}
}

上面程序中Student对象是开发者自己创建的,而且需要自己来维护这个对象。
使用IoC(Inverse of Control 反转控制)时
1.创建Mavne项目
2.完善项目结构
3.pom.xml导入Spring依赖包

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


4.创建java类

package com.wangxing.spring.bean;

public class Student {
    public void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

5.在resources目录中创建Spring配置文件【applicationcontext.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 -->
    <!-- 通知Sping容器创建类对象 -->
    <!--class属性:指定创建对象的java类【包名+类名】-->
    <!--id属性:指定对象名称-->
    <bean id="stu" class="com.wangxing.spring.bean.Student"></bean>
</beans>

6.测试

package com.wangxing.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[]args){
        //得到Spring容器对象
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
        //从spring容器中得到创建Student类对象
        Student student=(Student)ac.getBean("stu");
        student.getStuInfo();
    }
}

上面的测试代码中我们并没有new,说明我们自己没有创建对象,但是我们依然调用了Student类的实例方法。
那么Student类的对象是谁创建的?
答:Spring帮我们创建了Student类的对象。
Spring是怎么知道要创建的是Student类的对象?
答:我们通过在Spring的配置文件中配置

元素,告诉Spring创建Student类对象。
Spring创建的Student类对象保存在哪里?
答:保存在Spring容器中,我们只需要得到Spring容器对象,通过getBean方法得到自己想要的对象即可。
IoC(Inverse of Control 反转控制)— 将对象的创建和维护权利从开发者本身转移至Spring的过程就是IoC(Inverse of Control 反转控制)。
解析Spring中的配置文件和核心对象
1.Spring中的配置文件
名称:推荐使用applicationcontext.xml
位置:一般都在resources目录中
内容:

<?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 -->
        <!-- 通知Sping容器创建类对象 -->
        <!--class属性:指定创建对象的java类【包名+类名】-->
        <!--id属性:指定对象名称-->
        <bean id="stu" class="com.wangxing.spring.bean.Student"></bean>
</beans>

-------根元素中引入的其他元素的命名空间以及地址,个约束文件
< bean id=“stu” class=“com.wangxing.spring.bean.Student”>< /bean>-通知Spring容器创建类对象
class属性:指定创建对象的java类【包名+类名】
id属性:指定对象名称,将来从Spring容器中得到自己需要的对象是要依赖这个属性值。
为什么根元素是beans,创建对象使用bean元素?
bean的含义在程序中是组件的意思,Spring认为有它创建的接口的对象/类的对象都是Spring本身的一个组件所以就使用bean元素表示它所创建的元素,由于Spring可以创建多个组件,所以根元素就是beans。
2.Spring容器对象
Spring容器对象—用来保存和维护Spting创建的对象。
Spring容器对象
1.ApplicationContext接口是BeanFactory接口的子接口
2.BeanFactory接口
创建ApplicationContext接口对象/BeanFactory接口对象
1.new ClassPathXmlApplicationContext(“Spring配置文件”)----通过查找classpath[类路径{resources目录}]下的Spring配置文件创建ApplicationContext接口对象/BeanFactory接口对象。
ApplicationContext ac=new ClassPathXmlApplicationContext(“applicationcontext.xml”);
BeanFactory ac=new ClassPathXmlApplicationContext(“applicationcontext.xml”);
2.new FileSystemXmlApplicationContext(“Spring配置文件”);—通过查找系统路径[理解成绝对路径]下的Spring配置文件创建ApplicationContext接口对象/BeanFactory接口对象。【不推荐使用】
ApplicationContext ac=new FileSystemXmlApplicationContext(“F:\20200728\IdeaProjects\TestSpringDemo1\src\main\resources\applicationcontext.xml”);
BeanFactory ac=new FileSystemXmlApplicationContext(“F:\20200728\IdeaProjects\TestSpringDemo1\src\main\resources\applicationcontext.xml”);
从Spring容器对象中取得自己需要的类对象?
通过这个Spring容器对象提供的方法getBean(“”)将自己需要的对象得到。
6.Bean实例化4种方式
1.无参数构造方法(开发最常用)

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

Spring配置文件

<bean id="stu" class="com.wangxing.spring.bean.Student"></bean>

测试代码

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//从Spring容器中得到创建Student类对象
Student student= (Student)ac.getBean("stu");
student.getStuInfo();

2.静态工厂方法实例化bean–在一个类中书写静态的方法,这个方法返回某个Bean的对象(在方法中创建Bean的对象)。

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

静态工厂

package com.wangxing.spring.bean;
public class StaticFactryMethodClass {
    //创建静态方法
    public  static  Student getStudentObj(){
        return  new Student();
    }
}

Spring配置文件

<?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="stu" class="com.wangxing.spring.bean.StaticFactoryMethodsClass" factory-method="getStudentObj"></bean>
</beans>

测试代码

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();

3.实例工厂方法实例化bean—在一个类中书写实例的方法,这个方法返回某个Bean的对象(在方法中创建Bean的对象)。

package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

实例工厂

package com.wangxing.spring.bean;
public class FactoryMethodClass {
    //创建实例方法
    public    Student getStudentObj(){
        return  new Student();
    }
}

Spring配置文件

<!-- 实例工厂方法配置 -->
<!-- 1.创建实例工厂对象 -->
<bean id="factoryMethodClass" class="com.wangxing.spring.bean.FactoryMethodClass"></bean>
<!-- 2.配置由实例工厂创建Student对象-->
<bean id="stu" factory-bean="factoryMethodClass" factory-method="getStudentObj"></bean>

测试代码

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();
4.FactoryBean接口方式实例化bean
package com.wangxing.spring.bean;
public class Student {
    public  void getStuInfo(){
        System.out.println("Student类的实例方法");
    }
}

实现FactoryBean接口的子类

package com.wangxing.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class FactoryBeanSunClass implements FactoryBean<Student> {
    @Override
    public Student getObject() throws Exception {
        return new Student();
    }
    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }
}

Spring配置文件


测试代码

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student=(Student)ac.getBean("stu");
student.getStuInfo();

7.FactoryBean接口与BeanFactory接口的区别
BeanFactory接口—Spring容器—用来保存和维护Spring创建的对象【管理】
FactoryBean接口-------------------用来创建java对象【创建】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值