mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add SpringUtil
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package cn.hutool.extra.spring;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Spring(Spring boot)工具封装,包括:
|
||||
*
|
||||
* <pre>
|
||||
* 1、Spring IOC容器中的bean对象获取
|
||||
* </pre>
|
||||
*
|
||||
* @author loolly
|
||||
* @since 5.1.0
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
if (SpringUtil.applicationContext == null) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取applicationContext
|
||||
*
|
||||
* @return ApplicationContext
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
//通过name获取 Bean.
|
||||
|
||||
/**
|
||||
* 通过name获取 Bean
|
||||
* @param name Bean名称
|
||||
* @return Bean
|
||||
*/
|
||||
public static <T> T getBean(String name) {
|
||||
//noinspection unchecked
|
||||
return (T) applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param clazz Bean类
|
||||
* @return Bean对象
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name,以及Clazz返回指定的Bean
|
||||
*
|
||||
* @param <T> bean类型
|
||||
* @param name Bean名称
|
||||
* @param clazz bean类型
|
||||
* @return Bean对象
|
||||
*/
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return applicationContext.getBean(name, clazz);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -0,0 +1,35 @@
|
||||
package cn.hutool.extra.spring;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = {SpringUtil.class, SpringUtilTest.Demo2.class})
|
||||
public class SpringUtilTest {
|
||||
|
||||
@Test
|
||||
public void getBeanTest(){
|
||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||
Assert.assertEquals(12345, testDemo.getId());
|
||||
Assert.assertEquals("test", testDemo.getName());
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Demo2{
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
@Bean(name="testDemo")
|
||||
public Demo2 generateDemo() {
|
||||
Demo2 demo = new Demo2();
|
||||
demo.setId(12345);
|
||||
demo.setName("test");
|
||||
return demo;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user