方式一:
//返回Spring容器对象
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
UserService userService = (UserService) context.getBean("userServiceImpl");
String hi = userService.sayHi("spring boot java");
System.out.println(hi);
方式二:
1、实现CommandLineRunner接口
public class Application implements CommandLineRunner {
2、启动Spring容器
public static void main(String[] args) {
//启动SpringBoot,启动Spring容器
SpringApplication.run(Application.class, args);
}
3、注入UserService的Bean
@Autowired
private UserService userService;
4、实现接口方法
//相当于纯Java程序的main方法
@Override
public void run(String... args) throws Exception {
String hi = userService.sayHi("springboot java");
System.out.println(hi);
}
关闭SpringBoot图标logo
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}