1.使用容器27
您希望通过代码从容器中获取对象。
通过SpringAplication.run(Application.class,args);获取返回值的容器。
public static ConfigurableApplicationContext run(Class primarySource, String... args) { return run(new Class[]{primarySource}, args);}ConfigurableApplicationContext : Publicationcontext的子接口是applicationcontext interface ConfigurableApplicationContext extends ApplicationContext
2.在Springbot中使用AplicationcontextttSpringAplication在main方法中.run()方法获取返回的Spring容器对象,然后获取业务bean
进行调用.
Userservice接口
package com.bjpowernode.service;// Spring Boot 中使用 ApplicationContext 27public interface UserService { void sayHello(String name);}
UserServiceImpl
package com.bjpowernode.service.impl;import com.bjpowernode.service.UserService;import org.springframework.stereotype.Service;@Service("userService")public class UserServiceImpl implements UserService { @Override public void sayHello(String name) { System.out.println(”SayHelolo执行了业务方法:"+name); }}
Course5Application
package com.bjpowernode;import com.bjpowernode.service.UserService;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class Course5Application { public static void main(String[] args) { // Spring Boot 中使用 ApplicationContext 27 //ConfigurableApplicationContext ctx = SpringApplication.run(Course5Application.class, args); ApplicationContext ctx = SpringApplication.run(Course5Application.class, args); UserService userSevice = (UserService) ctx.getBean("userService"); userSevice.sayHello(李四); }}
3.comandlineruner接口28course5_1代码
在开发过程中可能会出现这样的情况。一些内容需要在容器启动后执行。例如,读取配置文件、数据库连接等。SpringBoot为我们提供了两个界面来帮助我们实现这种需求。这两个接口分别是commandlineruner和applicationrunner。当容器启动完成时,他们的执行时间。这两个接口中有一种run方法,我们只需要实现这种方法。这两个接口的区别在于
在ApplicationRuner中,Run方法的参数是ApplicationArguments,
commandlineruner接口中run方法的参数为String数组
简介来说
Comnandlineruner接口,Applcationruner接口
这两个接口都有一种run方法。容器对象创建后,执行时间自动执行run()方法。
在容器对象中创建一些自定义的操作可以完成。
@FunctionalInterfacepublic interface CommandLineRunner { void run(String... args) throws Exception;}@FunctionalInterfacepublic interface ApplicationRunner { void run(ApplicationArguments args) throws Exception;}
Hellloserviceceler
package com.bjpowernode.service;//接口 28//演示 CommandLineRunner 接口 28public interface HelloService { String sayHello(String name);}
Helloserviceimplle接口实现
package com.bjpowernode.service.impl;import com.bjpowernode.service.HelloService;import org.springframework.stereotype.Service;///Helloservice接口实现类//演示 CommandLineRunner 接口 28@Service("helloService")public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "你好:"+name; }}
course51aplication
package com.bjpowernode;import com.bjpowernode.service.HelloService;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;//演示 CommandLineRunner 接口 28@SpringBootApplicationpublic class course51aplication implements CommandLineRunner { @Resource///自动注入 private HelloService helloService; public static void main(String[] args) { System.out.println(“准备创建容器对象”); ///创建容器对象 SpringApplication.run(course51aplication.class, args); System.out.println(“容器对象创建后”); } @Override public void run(String... args) throws Exception { String str = helloService.sayHello("lisi"); System.out.println(“调用容器中的对象=”+str); ///可以自定义操作,例如,读取文件, 数据库等等 System.out.println(“创建容器对象并执行方法”); }}