什么是Spring以及Spring环境的搭建

1.什么是Spring
    Spring是一个开源的轻量级的应用开发框架,目的是为了简化企业级编程,降低各个模块间的侵入性和耦合度.
    Spring提供了IOC和AOP的功能,可以将组建的耦合度降低,便于日后的维护和升级,实现各个模块的组件化编程
    为什么要用Spring?
        Spring的本质是管理软件中的对象,就是创建对象和维护对象之间的关系.

2.Spring容器
    Spring容器的理解就是在Spring中,任何的java和javaBean都被当做Bean处理,这时Bean都交给Spring容器管理和使用

3.Spring环境的搭建

  1.导入相关的jar包(beans/core);
  2.导入依赖包(common-logging);

            
  3.配置XML文件(作用:告诉spring帮我管理那些bean);(放在Spring容器中的对象都是Bean)
        1)在classpath下,application.xml/applicationContext.xml
        2)<beans><bean id="" class="" /></beans>

<?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="helloworldBean" class="com.gxxy.spring_01test.HelloSpring"></bean>
    <!-- 
        id:全局必须唯一
        name:可以给这个类型定义多个别名(alias),多个名字之间需要使用英文逗号隔开
              并且可以使用Factory的getAliases()方法获取,
              一般在SpringMVC中使用(相当于url-pattern,多个可使用时空格隔开)
     -->
</beans>

  4.使用容器:
        1)创建一个资源文件对象(ClasspathResource)
        2)创建一个BeanFactory(Spring的容器);创建一个基于XML的BeanFactory:xmlBeanFactory,传入xml配置
        3)从容器中获取对象:
            1.getBean(Class cls):按照类型获取bean;
            2.getBean(String name):按照名字获取bean;
            3.getBean(String name,Class cls):按照名字和类型获取;

package com.gxxy.spring_01test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@SuppressWarnings("deprecation")
public class HelloSpringTest {
    // 创建一个资源文件对象(ClasspathResource)
    Resource resource = new ClassPathResource("application.xml");
    // 创建一个BeanFactory(Spring的容器);
    // 创建一个基于XML的BeanFactory:xmlBeanFactory,传入xml配置
    BeanFactory factory = new XmlBeanFactory(resource);
    @Test
    public void testFirstSpring() {
        /*
         * IHelloSpringDAO hell = new HelloSpring(); hell.helloSpring();
         */
        // 从容器中获取对象
        IHelloSpringDAO bean = factory.getBean("helloworldBean", IHelloSpringDAO.class);
        bean.helloSpring();

    }
    /*
     * Bean: 放在容器中的对象都是Bean
     * 面向接口编程: 什么是面向接口编程?-->面向接口编程可以解决什么问题? 解耦
     * Ioc/DI Ioc:控制反转 Inverse of Control DI: 依赖注入 Dependent Injection
     */
}

4.Spring的加载过程
  1.找到对应的配置文件;

    // 创建一个资源文件对象(ClasspathResource)
    Resource resource = new ClassPathResource("application.xml");
    // 创建一个BeanFactory(Spring的容器);
    // 创建一个基于XML的BeanFactory:xmlBeanFactory,传入xml配置
    BeanFactory factory = new XmlBeanFactory(resource);

  2.加载配置文件;
  3.解析所有的Bean元素,识别id和class属性;
  4.通过反射创建一个这个类型对应的实例;
  5.把id作为key,把实例作为value存在spring容器中;
  6.getBean从容器中获取到创建好的对象的实例;   

5.import 分散配置信息
在总配置文件中使用`import`元素导入各个模块的配置文件信息(Struts2中我们使用`include`导入)
其中可以使用两种预定义的前缀,这两种方式同样适合于其他的资源路径查找,分别是:
1.classPath  classpath:跟文件的编译路径即可
2.file       file://localhost:80/ 跟文件的绝对路径

<?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">
    <!--<import resource="file://localhost:80/D:JavaJavaCodeprojectSpringDemoTestsrccomgxxyspring_01testHello.xml"/> -->
    <import resource="classpath:com/gxxy/spring_01test/Hello.xml"/>
    <import resource="classpath:com/gxxy/spring_02commenttest/Hellocomment.xml"/>
    <import resource="classpath:com/gxxy/spring_03container/basic/Hellobasic-context.xml"/>
    <import resource="classpath:com/gxxy/spring_03container/createbeanfactory/CreatbeanFactory.xml"/>
</beans>
原文地址:https://www.cnblogs.com/zhang-bo/p/6626985.html