Spring整合MyBatis

在了解SpringBoot的main方法之前,先看一下Spring整合Mybatis的例子,首先对于Spring整合Mybatis,对于Spring的配置文件可以通过 .xml配置类 的形式,这里不做详细介绍。

项目结构

spring boot themlef放在哪个目录 spring boot main_main方法

Spring的main方法

public class Test {

	public static void main(String[] args) throws Exception {

		ApplicationContext context =
				new AnnotationConfigApplicationContext(MyConfig.class);

		GoodsService goodsService = context.getBean(GoodsService.class);

		Goods goods = goodsService.selectByPrimaryKey(1);
		System.out.println("查询结果:" + goods.getId() + "--" + goods.getName() + "--" + goods.getStore());
	}
}

1.第一步先启动Spring容器,new AnnotationConfigApplicationContext返回一个application容器,此时引入Spring配置类,并注入Bean对象。
2.从Spring容器中获取对应的Bean对象,对应为GoodsService对象

SpringBoot整合Mybatis

这里只是通过整合MyBatis去查看SpringBoot的main方法。对于SpringBoot取消了Spring的相关配置文件,相反都统一到了application.properties文件中,对数据库的相关端口号,进行配置。

项目结构

spring boot themlef放在哪个目录 spring boot main_System_02

此处Application文件为项目的入口。
SpringBoot的main方法

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

上述代码为SpringBoot初始创建时的main方法,@SpringBootApplication 注解将Application设置为配置类,进入到@SpringBootApplication的源码,可以看到组合三个注解:@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration.分析这三个注解,
此时点击查看SpringApplication底层源码。

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            listeners.started(context);
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

查看run方法,返回值为ConfigurableApplicationContext返回值为一个Application容器,对于main方法进行修改

@SpringBootApplication//该注解类似于配置类
public class Application  {

    public static void main(String[] args) {
        
        ApplicationContext context = SpringApplication.run(Application.class, args);

        GoodsService goodsService = context.getBean(GoodsService.class);

        Goods goods = goodsService.getGoodsById(1);
        System.out.println("产品信息:" + goods);
    }
}

这里通过ApplicationContext返回一个application容器,此时通过DEBUG调试查看context内容,在我们进行调试之前,先思考对于Application类,由于Application是一个配置类,就会有BeanDefinition去抽象信息。那么是否会生成一个Bean对象??

spring boot themlef放在哪个目录 spring boot main_java_03


spring boot themlef放在哪个目录 spring boot main_spring boot_04


此时我们在BeanDefinition中查看到application

spring boot themlef放在哪个目录 spring boot main_System_05


singletonObjects中查找到application对象,那么我们就可以在main方法中 @Autowired 自动注入可以将Service对象进行注入使用。

CommandLineRunner接口

对于Application类实现CommandLineRunner接口,有一个 **run()**方法,该方法是SpringIOC启动完成后,调用的方法。

package org.springframework.boot;

@FunctionalInterface
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}
@SpringBootApplication//该注解类似于配置类
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        //启动springIOC容器
        ApplicationContext context = SpringApplication.run(Application.class, args);

        GoodsService goodsService = context.getBean(GoodsService.class);

        Goods goods = goodsService.getGoodsById(1);
        System.out.println("产品信息:" + goods);
    }

    @Override
    public void run(String... args) throws Exception {//CommandLineRunner接口实现的方法
        System.out.println("这是springIOC启动完成后 调用的方法");
    }
}

对于main方法的其他配置。

#关闭启动logo
#spring.main.banner-mode=off
#设置定义配置类的路径 也即是入口路径
#spring.main.sources= //application.class的文件路径
#生成的bean对象是否覆盖
#spring.main.allow-bean-definition-overriding=true

若在application.properties文件中设置了 #spring.main.sources 在main方法中

@SpringBootApplication//该注解类似于配置类
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication();
        ApplicationContext context1 = app.run(args);
        GoodsService goodsService = context1.getBean(GoodsService.class);
        Goods goods = goodsService.getGoodsById(1);
        System.out.println("产品信息:" + goods);

    }
 }