springboot

 一个标准的springboot程序结构应该是什么样?

   1. spring通常建议我们将main方法所在的类放到一个root包下,@EnableAutoConfiguration(开启自动配置)注解通常都放到main所在类的上面,下面是一个典型的结构布局,供参考

   

com
 +- example
     +- myproject
         +- Application.java//main方法所在类在最底层
         |
         +- bean            //bean类
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service         //service层
         |   +- CustomerService.java
         |
         +- web             //controller层
             +- CustomerController.java

    从整体看去跟我们平时的布局差不多,就是将main方法放到了最底层。

    这样@EnableAutoConfiguration可以从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(如果你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用。

    通常我们只需要一个@EnableAutoConfiguration类

 

springboot功能全解.

    1.SpringApplication  程序入口

        SpringApplication.run(MySpringConfiguration.class, args);

    2.自定义打印项(就是控制台输出内容)    

        在classpath下加入banner.txt 来定义程序启动时要输出的内容,例如我这样

        

        

    banner的变量:

Variable

Description

${application.version}

MANIFEST.MF中的版本号例如1.0

${application.formatted-version}

格式化后的版本号,就是加了个V,例如V1.0…

${spring-boot.version}

springboot版本号,例如1.2.2.RELEASE.

${spring-boot.formatted-version}

格式化后的Springboot版本号,例如V1.2.2.RELEASE………

    3. 自定义SpringApplication

        可以自定义一些应用的配置,如下关闭banner输出:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MySpringConfiguration.class);
    app.setShowBanner(false);
    app.run(args);
}

        SpringApplication的一些方法:

        

        SpringApplication的构造器参数往往是一个类.class,而这个类一定是加有@Configuration注解的,另外还可以换成xml的配置路径哦,前面有写出来过,SpringApplication.run("classpath:/META-INF/application-context.xml",args);

     4. 流畅的创建API

通过SpringApplicationBuilder构建
    new SpringApplicationBuilder()
    .showBanner(false)
    .sources(Parent.class)
    .child(Application.class)
    .run(args);

    5.  程序的事件和监听

除了通常的Spring框架的事件,如ContextRefreshedEvent SpringApplication发送一些额外的应用程序事件。触发一些事件实际上是ApplicationContext之前创建。

        除了一些常见的spring时间,像ContextRefreshedEvent  SpringApplication会产生一些额外的事件,某些事件甚至会再ApplicationContext创建之间触发。你可以通过很多方式创建监听器,一般最常用的就是如下:

public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(FirstController.class);
        app.addListeners(new TestListener());
        app.run(args);
}

付一个自定义的listener。

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
public class TestListener implements ApplicationListener<ApplicationStartedEvent>{
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        /*do something*/
    }
}

 

    程序事件运行顺序:

An ApplicationStartedEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.

An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.

An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.

An ApplicationFailedEvent is sent if there is an exception on startup.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值