非web环境的spring应用

springframework提供的spring容器,非常适合应用于javaweb环境中。 同时,spring组件的低耦合性为普通java应用也提供了足够的支持。

以下,我们通过一个范例来了解spring在普通java application project中的应用。

开发环境及工具

  • JDK 1.8
  • Eclipse - jee - Oxygen3a
  • Maven 3.5.2
  • spring framework 5.1.3

spring特点

  • 上帝类

spring容器就是上帝,需要什么bean只管找它要就是了,甚至可以说,bean的职责是命中注定的。

  • 梦境化

在梦境中,我们几乎从来都不关心我们是如何来到当前场景的,只知道如何应对当前场景的事件。spring应用和这个情况几乎一致,着眼目标,无问西东。

项目需求

  • 通过注解方式(JavaConfig)配置Bean;
  • main方法中加载spring容器(ApplicationContext),并获取先前配置的Bean。
  • maven构建
  • 非web环境
  • 易于测试

项目创建并重构(提供spring-framework支持)

  • 新建Maven Project,使用maven-archetype-quickstart 1.1骨架
  • 添加Maven依赖,spring-context

本例中,仅需spring-context依赖即可。 但为了便于后期扩展,直接添加了spring-data-jpa依赖项,它会间接地添加spring-context依赖项。

    <!-- 选用2.1.3版本,是为了配合springframework 5.1.3版本 -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
  • 编写spring配置信息,其实就是配置一大堆的bean,交给spring容器备用

spring支持的配置方式有xml配置和java配置,在此,我们暂且仅用java配置。

java配置,即:注解配置,使用@Configuration注解类,使用@Bean注解方法。

    @Configuration
    public class AppConfig {            
        @Bean(name="hello")
        public String hello() {
            return "hello, it is a bean from javaconfig.";
        }       
    }
  • 修改main方法,启用spring容器

    public class App {
        public static void main( String[] args ){
            System.out.println( "Hello World!" );
            ApplicationContext ctx = 
                new AnnotationConfigApplicationContext(AppConfig.class);
    
            System.out.println(ctx.getBean("hello"));
        }
    }
    

运行结果:

 

运行结果正常,没什么大问题。

运行测试

为方便测试,我们把maven依赖的junit的版本从默认的v3.8.1提升到v4.12,好处是加入了hamcrest依赖项,让测试断言通俗一点。

   <!-- 提升junit版本至junit4 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12<!-- default is v3.8.1 --></version>
        <scope>test</scope>
    </dependency>

针对注解配置类AppConfig编写测试类

package my.demo.it.contact.config;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppConfigTest {

    @Test
    public void bootstrapAppFromJavaConfig() {

        ApplicationContext context = 
            new AnnotationConfigApplicationContext(AppConfig.class);
        assertThat(context, is(notNullValue()));
        assertThat(context.getBean("hello"), is(notNullValue()));
    }
}

测试通过


 

附:引入slf4j-nop依赖,可以解决SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"问题。

        <!-- 解决:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.25</version>
        </dependency>

 

posted on 2019-02-26 14:25  god with us  阅读(1026)  评论(0编辑  收藏  举报