forked from Heeexy/SpringBoot-Shiro-Vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
back/src/main/java/com/heeexy/example/service/SpringUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.ng.find.util; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
/** | ||
* @author: 何霄煜 | ||
* @description: 获取Spirng bean的工具类 | ||
* @date: 2017/11/8 13:58 | ||
*/ | ||
public class SpringUtil implements ApplicationContextAware { | ||
private static ApplicationContext applicationContext = null; | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
if (SpringUtil.applicationContext == null) { | ||
SpringUtil.applicationContext = applicationContext; | ||
} | ||
} | ||
|
||
/** | ||
* 获取applicationContext | ||
* | ||
* @return | ||
*/ | ||
private static ApplicationContext getApplicationContext() { | ||
return applicationContext; | ||
} | ||
|
||
/** | ||
* 通过name获取 Bean. | ||
* | ||
* @param name | ||
* @return | ||
*/ | ||
|
||
public static Object getBean(String name) { | ||
return getApplicationContext().getBean(name); | ||
} | ||
|
||
/** | ||
* 通过class获取Bean. | ||
* | ||
* @param clazz | ||
* @param <T> | ||
* @return | ||
*/ | ||
|
||
public static <T> T getBean(Class<T> clazz) { | ||
return getApplicationContext().getBean(clazz); | ||
} | ||
|
||
/** | ||
* 通过name,以及Clazz返回指定的Bean | ||
* | ||
* @param name | ||
* @param clazz | ||
* @param <T> | ||
* @return | ||
*/ | ||
public static <T> T getBean(String name, Class<T> clazz) { | ||
return getApplicationContext().getBean(name, clazz); | ||
} | ||
} |