springboot-java

1. 获取Springboot容器Bean对象调用业务方法

package com.example.springboot;

import com.example.springboot.service.StudentService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        /**
         * Springboot项目启动后,返回值是ConfigurableApplicationContext(一个Spring容器)
         * 它相当于原Spring容器中的启动容器ClasspathXmlApplicationContext
         */
        //获取Springboot容器
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        //从Spring容器中获取指定的Bean对象
        StudentService studentService = ((StudentService) applicationContext.getBean("studentServiceImpl"));
        //调用业务方法
        String say = studentService.say();
        System.out.println(say);
    }

}

2. 实现CommandLineRunner类重写run()方法调用业务方法

package com.example.springboot;

import com.example.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private StudentService studentService;

    //Springboot启动程序,初始化Spring容器
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    //重写CommandLineRunner类的run()方法
    @Override
    public void run(String... args) throws Exception {
        //调用业务方法
        String say = studentService.say("Other Springboot Java !");
        System.out.println(say);
    }
} 

 关闭Springboot启动Logo

//Springboot启动程序,初始化Spring容器
    public static void main(String[] args) {

        //获取入口Springboot类
        SpringApplication springApplication = new SpringApplication(Application.class);
        //设置参数
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.run(args);
//        SpringApplication.run(Application.class, args);
    } 

 修改baidu

相关推荐