mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add SpringUtil
This commit is contained in:
parent
7680b7ac8d
commit
7b9c78869b
@ -9,6 +9,7 @@
|
|||||||
* 【core 】 新增WatchServer(issue#440@Github)
|
* 【core 】 新增WatchServer(issue#440@Github)
|
||||||
* 【core 】 ReflectUtil.getFieldValue支持static(issue#662@Github)
|
* 【core 】 ReflectUtil.getFieldValue支持static(issue#662@Github)
|
||||||
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
||||||
|
* 【extra】 新增SpringUtil
|
||||||
|
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
@ -206,5 +206,24 @@
|
|||||||
<version>3.0.2</version>
|
<version>3.0.2</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Spring Boot -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
<version>2.2.2.RELEASE</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<version>2.2.2.RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>1.2.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user