Spring Boot中Bean定义方调用方式解析
作者:BarryW
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用spring提供的其他对象或者说有一些不需要交给spring管理,但是需要用到spring里的一些对象。如果这是spring框架的独立应用程序,我们通过
ApplicationContextac=newFileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
这样的方式就可以很轻易的获取我们所需要的对象。
但是往往我们所做的都是WebApplication,这时我们启动spring容器是通过在web.xml文件中配置,这样就不适合使用上面的方式在普通类去获取对象了,因为这样做就相当于加载了两次spring容器,而我们想是否可以通过在启动web服务器的时候,就把Application放在某一个类中,我们通过这个类在获取,这样就可以在普通类获取springbean对象了,让我们接着往下看
下面介绍在SpringBoot中是如何使用的
1.在Spring Boot可以扫描的包下
写的工具类为SpringUtil,实现ApplicationContextAware接口,并加入Component注解,让spring扫描到该bean
springutil:
package me.shijunjie.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; } System.out.println("---------------------------------------------------------------------"); System.out.println("---------------------------------------------------------------------"); System.out.println("---------------me.shijunjie.util.SpringUtil------------------------------------------------------"); System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========"); System.out.println("---------------------------------------------------------------------"); } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通过class获取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
为了测试,我们再启动的时候先通过代码方式给spring容器中注入一个bean,入下所示
package me.shijunjie.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import me.shijunjie.entity.Demo2; @Configuration public class BeanConfig { @Bean(name="testDemo") public Demo2 generateDemo() { Demo2 demo = new Demo2(); demo.setId(12345); demo.setName("test"); return demo; } }
然后我们编写测试controller,并从刚才写的springutil中获取这个bean
package me.shijunjie.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import me.shijunjie.util.SpringUtil; @RestController @RequestMapping("/application") public class TestApplicationController { @RequestMapping("/test1") public Object testSpringUtil1() { return SpringUtil.getBean("testDemo"); } }
测试
启动web应用,打开浏览器输入http://localhost:8080/application/test1,测试成功
2不在Spring Boot的扫描包下
这种情况处理起来也很简单,先编写SpringUtil类,同样需要实现接口:ApplicationContextAware,具体编码如下:
package me.shijunjie.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringUtil2 implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil2.applicationContext == null) { SpringUtil2.applicationContext = applicationContext; } System.out.println("---------------------------------------------------------------------"); System.out.println("---------------------------------------------------------------------"); System.out.println("---------------me.shijunjie.util.SpringUtil------------------------------------------------------"); System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil2.applicationContext+"========"); System.out.println("---------------------------------------------------------------------"); } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通过class获取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
使用@Bean注解,在App.java类中将SpringUtil注解进
package me.shijunjie.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; import me.shijunjie.util.SpringUtil2; @ComponentScan(basePackages={"me.shijunjie"}) // 扫描该包路径下的所有spring组件 /*@EnableJpaRepositories("me.shijunjie.dao") // JPA扫描该包路径下的Repositorie *//*@EntityScan("me.shijunjie.entity") // 扫描实体类 */@SpringBootApplication @EnableScheduling public class App extends SpringBootServletInitializer{ @Bean public SpringUtil2 getSpringUtil2() { return new SpringUtil2(); } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
测试(使用热部署的需要重启服务器)
启动web应用,打开浏览器输入http://localhost:8080/application/test2,测试成功
除此以外,也可以在App.java中使用@Import进行导入。
package me.shijunjie.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import org.springframework.scheduling.annotation.EnableScheduling; import me.shijunjie.util.SpringUtil2; @ComponentScan(basePackages={"me.shijunjie"}) // 扫描该包路径下的所有spring组件 /*@EnableJpaRepositories("me.shijunjie.dao") // JPA扫描该包路径下的Repositorie *//*@EntityScan("me.shijunjie.entity") // 扫描实体类 */@SpringBootApplication @EnableScheduling @Import(SpringUtil2.class) public class App extends SpringBootServletInitializer{ /*@Bean public SpringUtil2 getSpringUtil2() { return new SpringUtil2(); }*/ public static void main(String[] args) { SpringApplication.run(App.class, args); } }
跑出结果和上面相同
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。