Spring框架

1,导入相关jar包:核心包:beans/core

2,依赖包:(com.apache.commons.login.jar/Spring-context/Spring-expression/Spring-aop/Spring-text);
3,配置XML文件(作用:告诉spring帮我管理哪些bean);

<?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="ModulesBean" class="com.xk.spring.modules.TestMoudles" init-method="init" 
    destroy-method="destroy"/>

</beans>


 1)在classpath下,application.xml/applicationContext.xml;
 2)<beans><bean id="" class="" /></beans>
4,使用容器:
 1)创建一个资源文件对象(Classpath@Resource);
 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.xk.spring.kp01_hello;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

@SuppressWarnings("deprecation")
public class TestHello {
    // 不采用@Autowired注解,不采用自动装配.
    // 采用面向对象编程思路.
  //BeanFactory
  //BeanFactory是Spring中提供的最简单,最基本的容器;这个容器只提供了IoC/DI的功能;

ClassPathResource resource = new ClassPathResource("HelloSpring.xml"); BeanFactory factory = new XmlBeanFactory(resource); IHello hello = new Hello(); @Test public void testName() throws Exception { IHello bean = factory.getBean("HelloSpringBean", IHello.class); bean.nice(); System.out.println("~~~~~~~~~~~~~~~~~~~"); hello.nice(); } }

第二种:

package com.xk.spring.kp02_modules;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//面向接口编程
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
// @ContextConfiguration("xml的名字.xml")如果有,则xml的名字不需要是测试类-context
public class TestMoudlesTest {
    /*
     ClassPathResource resource = new ClassPathResource("Moudles.xml");
     BeanFactory factory = new XmlBeanFactory(resource);
     */
    @Autowired //不用new实现类 完全面向接口编程
    BeanFactory factory;

    @Test // 测试
    public void testName() throws Exception {
        ITestmoudles bean = factory.getBean("ModulesBean", ITestmoudles.class);
        bean.sayhello();

    }
}

Spring加载过程:

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

分散配置信息:

在有时候,配置信息我们习惯每个类写自己的配置信息,这时候就需要分散配置信息.

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

Spring测试类:

基于Spring的测试的使用:

1, 首先导入基础测试需要的jar: test,aop,expression,context
2, 在测试类上编写注解 @RunWith(SpringJUnit4ClassRunner.class)
 表示给Spring给JUnit提供了一个运行环境,其实Spring内部会把当前的测试类当作Spring的一个bean处理
 如此, Spring就可以在测试文件启动的时候,自动的启动Spring容器
3, 在@RunWith相同的位置添加注解:@ContextConfiguration, 参数填写包含后缀的本模块的xml配置文件的名称
 这个参数可以不写, 但是如果不写,则需要按照约定的方式给配置文件起名,这个约定的配置文件名的格式是:
  `TestClassName-context.xml`
4, 在测试类中编写一个BeanFactory的字段,并使用注解@Autowired标注
 表示告诉Spring这个字段就是你默认需要创建的BeanFactory
 这个就是DI:Dependency Injection 依赖注入

详见代码块2和4.

代码块4
package
com.xk.spring.kp03_container.s1_basic; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class ContainerTest { //ApplicationContext: 也是一个接口, 继承了BeanFactory, ApplicationContext容器功能更加全面 @Autowired ApplicationContext appCtx; @Test public void testName() throws Exception { IContainer con = appCtx.getBean("testContainerBean", IContainer.class); con.testcontainer(); } }

对比:

BeanFactory:
 1,容器启动的时候不会实例化对象
ApplicationContext:
 1,容器启动的时候会实例化对象,
  可以通过在bean注解的 `lazy-init`属性可以让容器不会去实例化
  也可以在beans中添加属性`default-lazy-init`

创建Bean:CreateBean

bean的实例化方式:
 1, 传统的bean的配置方式
 <bean id="bean的全局唯一标识" class="bean对应的全限定名称" />
 2, 使用静态工厂方法生产bean
 <bean id="bean的全局唯一标识" class="自定义的一个含有创建bean的方法的普通类"
 factory-method="class中的创建bean的方法名称" />
  只要我们使用factory-method了属性, 那么Spring容器就会认为class配置的是一个工厂类
 3, instance 实例化创建bean
 4, beanFactory bean工厂创建bean

BeanScope(scope是bean标签中的一个属性)
默认情况下,每一个配置在bean中的对象都是单例的, 如果想让它不是单例的:
 在bean的注解中添加一个属性`scope`, 可以限定bean对象的生命作用域
 取值:
  1)singleton: 单例,默认值
   Spring 容器默认启动的时候会创建一个对象
  2)prototype: 原型(模式, 当每一次获取每个对象的时候都是获取的这个对象的镜像),线程安全
   Spring 容器默认启动的时候不会实例化该对象, 只有用的时候才去实例化
   当创建对象之后Spring容器也就不会去管理这个对象, 需要我们自己实现bean的销毁的方法

Spring生命周期:

如果我们不去使用Spring提供给JUnit的环境, 那么需要我们自己正常的关闭Spring容器,才会执行在配置
  文件中配置的 `destroy-method`方法
如果bean的scope类型是prototype, 那么spring不回去调用 `destroy-method`, 因为创建bean
  对象之后就不会再管理这个对象

在下一篇附上相应代码;

原文地址:https://www.cnblogs.com/huike/p/6628646.html