Spring简介及基础组件

一、Spring发展历程

2003年2月Spring框架正式开源,Spring致力于J2EE应用的各种解决方案,而不仅仅专注于某一层解决方案。可以说Spring是企业应用开发的“一站式”选择,Spring贯穿于表现层、业务层、持久层,然而Spring并不想取代那些已经有的框架,而是以高度的开放性,与这些已有的框架进行整合。

二、Spring的目标

1、让现有的技术更容易使用,2、促进良好的编程习惯。Spring是一个全面的解决方案,它坚持一个原则:不从新造轮子。已经有较好解决方案的领域,Spring绝不重复性实现,比如:对象持久化和OR映射,Spring只对现有的JDBC,Hibernate等技术提供支持,使之更容易使用,而不做重复的实现。Spring框架有很多特性,这些特性由7个定义良好的模块构成。

三、Spring体系结构

四、基础组件使用

1. 配置文件 --> 配置类

(1)最原始的方式:配置文件

在src/main/resources下建立配置文件beans.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 id="person" class="com.dapeng.study1.Person">
        <property name="name" value="dapeng"></property>
        <property name="age" value="25"></property>
    </bean>

</beans>

测试:

package com.dapeng.study1;

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

/**
 * 测试配置文件加载bean
 * Created by dapeng on 2019/12/30.
 */
public class MainTest1 {
    public static void main(String args[]) {
        //把beans.xml的类加载到容器
        ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中获取bean
        Person person = (Person) app.getBean("person");

        System.out.println(person);
    }
}

(2)使用@Configuration注解替代配置文件

package com.dapeng.study1;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 * Created by dapeng on 2019/12/30.
 */
//告诉Spring这是一个配置类,等同于配置文件
@Configuration
public class MainConfig {
    //给容器中注册一个bean,id默认为方法名,也可以用@Bean("abc")类设置id,类型为方法返回值类型
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("dapeng");
        person.setAge(25);
        return person;
    }
}

测试:

package com.dapeng.study1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试配置类加载bean
 * Created by dapeng on 2019/12/30.
 */
public class MainTest2 {
    public static void main(String args[]) {
        //把配置类中的信息加载到容器
        ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);
        //从容器中获取bean
        Person person = (Person) app.getBean("person");

        System.out.println(person);
    }
}

2. @ComponentScan

一个项目中有许多类,要是全部像这样配置一遍太费尽,那有什么好办法吗?可以用扫描注解@ComponentScan

这个注解可以指定扫描范围,让spring启动的时候扫描指定范围的类,然后放进IOC容器中;还可以设置过滤器指定哪些类不用扫描,或者只扫描哪些类;还可以自定义过滤规则。

@Configuration
@ComponentScan(value = "com.dapeng.study2")
public class MainConfig {
    //给容器中注册一个bean,id默认为方法名,也可以用@Bean("abc")类设置id,类型为方法返回值类型
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("dapeng");
        person.setAge(25);
        return person;
    }
}

测试:

    @Test
    public void test01() {
        //将配置类信息加载进IOC容器
        ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);
        //获取容器中所有注册的bean,打印
        String[] names = app.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }

测试结果:

假如现在只需要扫描被@Controller标记的类,该怎么做呢?很简单,只需要用includeFilters,这里要注意把useDefaultFilters设置为false

@ComponentScan(value = "com.dapeng.study2", includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)

测试结果:扫描注册的bean只剩下orderController了

同样的,也可以设置不让扫描哪些类,使用excludeFilters

@ComponentScan(value = "com.dapeng.study2", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
})

测试结果:orderController不在容器中了

也可以自定义过滤器:

/**
 * 自定义扫描过滤器
 * Created by dapeng on 2019/12/30.
 */
public class MyTypeFilter implements TypeFilter {
    /**
     * Created by dapeng on 2019/12/30.
     *
     * @param metadataReader        可以获取到当前正在扫描的类信息
     * @param metadataReaderFactory 可以获取到其他任何类信息
     */
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
        //当前扫描的类的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //当前扫描的类的信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //当前扫描的类资源(类的路径)
        Resource resource = metadataReader.getResource();

        String className = classMetadata.getClassName();
        if (className.contains("Dao")) {
            return true;//返回true则当前类的实例被注册到IOC容器中
        }
        return false;//返回false则不会
    }
}
//告诉Spring这是一个配置类,等同于配置文件
@Configuration
@ComponentScan(value = "com.dapeng.study2", includeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
}, useDefaultFilters = false)
public class MainConfig {
    //给容器中注册一个bean,id默认为方法名,也可以用@Bean("abc")类设置id,类型为方法返回值类型
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("dapeng");
        person.setAge(25);
        return person;
    }
}

测试结果:只有类名包含Dao的类实例会被注册到IOC容器中

给容器中注册bean的方式总结:

1. @Bean 一般用来导入第三方的包或类。比如Person为第三方的类,需要在我们的IOC容器中使用
2. 包扫描注解+标识注解(@ComponentScan + @Controller、@Service、@Responsitory、@Componet),一般是针对我们自己写的类,使用这个
3. @Import 快速给容器导入一个组件。注意:@Bean有点简单 。(用的少)
4. 通过FactoryBean注册到容器中。(用的少)

3. @Scope 单实例、多实例

prototype 多实例:IOC容器启动的时候,并不会调用方法创建对象,而是每次获取的时候才会调用方法创建对象
singleton 单实例:IOC容器启动的时候,会调用方法创建对象并放到IOC容器中,以后每次获取的时候就是直接从容器中拿的是同一个bean

@Configuration
public class MainConfig {
    /**
     * prototype 多实例:IOC容器启动的时候,并不会调用方法创建对象,而是每次获取的时候才会调用方法创建对象
     * singleton 单实例:IOC容器启动的时候,会调用方法创建对象并放到IOC容器中,以后每次获取的时候就是直接从容器中拿的是同一个bean
     */
    @Scope("prototype")
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("dapeng");
        person.setAge(25);
        return person;
    }
}

4. @Lazy 懒加载

懒加载:针对单实例bean,容器启动的时候不创建对象,仅当第一次使用的时候才创建

@Configuration
public class MainConfig {
    /**
     * 懒加载:针对单实例bean,容器启动的时候不创建对象,仅当第一次使用的时候才创建
     */
    @Lazy
    @Bean
    public Person person() {
        System.out.println("person实例创建。。");
        Person person = new Person();
        person.setName("dapeng");
        person.setAge(25);
        return person;
    }
}

测试:

public class Study4Test {
    @Test
    public void test01() {
        ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);
        System.out.println("容器加载完成。。");
        app.getBean("person");
    }
}

测试结果:

5. @Conditional

有条件地往IOC容器中添加实例。例如:操作系统不是Windows的时候,才把person2添加到容器中

@Configuration
public class MainConfig {

    @Bean
    public Person person1() {
        System.out.println("person1 实例创建。。。");
        return new Person();
    }

    @Conditional(WinCondition.class)
    @Bean
    public Person person2() {
        System.out.println("person2 实例创建。。。");
        return new Person();
    }
}
public class WinCondition implements Condition {

    /**
     * Created by dapeng on 2019/12/30.
     *
     * @param conditionContext      上下文环境
     * @param annotatedTypeMetadata 注解信息
     */
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //当前环境变量(包括操作系统信息)
        Environment environment = conditionContext.getEnvironment();
        String osName = environment.getProperty("os.name");
        if (!osName.contains("Windows")) {//如果不是windows平台,添加到IOC容器中
            return true;//返回true,则当前类实例会被添加到IOC容器中
        }
        return false;
    }
}

 测试:

public class Study5Test {

    @Test
    public void test01() {
        ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);
        System.out.println("容器加载完成。。。");
    }
}

测试结果:因为当前操作系统是Windows,所以person2没有添加到容器中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章