package com.system.yunjie.cms.ms.member;
import com.system.yunjie.cms.common.common.interceptor.ParamInterceptor;
import com.system.yunjie.cms.common.common.interceptor.InterceptorConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public SpringUtils() {
}
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static void setAppCtx(ApplicationContext webAppCtx) {
if (webAppCtx != null) {
applicationContext = webAppCtx;
}
}
/**
* 拿到ApplicationContext對象實例後就能夠手動獲取Bean的注入實例對象
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
return getApplicationContext().getBean(name, clazz);
}
public static final Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
Class clz = Class.forName(className);
return getApplicationContext().getBean(beanName, clz.getClass());
}
public static boolean containsBean(String name) {
return getApplicationContext().containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().isSingleton(name);
}
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getAliases(name);
}
public static void main(String[] args) {
/**
* springboot普通類獲取spring容器中的bean
*
*User UserDao UserImpl,其中UserImpl中有一個獲取User列表的方法getUserList
* 測試方法:
* 測試在springboot中根據UserImpl的Class對象調用getUserList方法獲取用戶列表數據
*/
// ParamInterceptor paramInterceptor1 = SpringUtils.getBean(ParamInterceptor.class);
// System.out.println("------------------------" + paramInterceptor1);
// ParamInterceptor paramInterceptor2 = SpringUtils.getBean(InterceptorConfig.class).getParamInterceptor();
// System.out.println("------------------------" + paramInterceptor2);
// Map param = new HashMap<String,Object>();
// param.put("name","Tom");
// List<User> listUser=SpringUtil.getBean(UserImpl.class).getClass()
// .getDeclaredMethod("getUserList", Map.class)
// .invoke(UserImpl.class,param);
}複製代碼
package com.system.yunjie.cms.ms.member;
import com.system.yunjie.cms.common.common.config.annotation.EnableFeignService;
import com.system.yunjie.cms.common.common.config.annotation.EnableGlobalExceptionHandler;
import com.system.yunjie.cms.common.common.config.annotation.EnableOAuth2Service;
import com.system.yunjie.cms.common.common.config.annotation.EnableValidateOrg;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringCloudApplication
@EnableOAuth2Service
@EnableFeignService
@EnableGlobalExceptionHandler
@EnableValidateOrg
@MapperScan(basePackages = "com.system.yunjie.cms.ms.member.dao")
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@LoadBalanced
public RestTemplate balancedTemplate() {
return new RestTemplate();
}
@Bean
public SpringUtils springUtils() {
return new SpringUtils();
}
}
複製代碼
package com.system.yunjie.cms.ms.member.controller;
import com.system.yunjie.cms.common.common.interceptor.InterceptorConfig;
import com.system.yunjie.cms.common.common.interceptor.ParamInterceptor;
import com.system.yunjie.cms.ms.member.SpringUtils;
import com.system.yunjie.cms.ms.member.service.MemberCsvImportService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static org.springframework.http.ResponseEntity.ok;
@RestController
@RequestMapping("/memberCsvImport")
@PreAuthorize("hasAuthority('ROLE_MEMBER')")
public class MemberCsvImportController {
MemberCsvImportService memberCsvImportService;
@Autowired
public MemberCsvImportController(MemberCsvImportService memberCsvImportService) {
this.memberCsvImportService = memberCsvImportService;
}
@Autowired
SpringUtils springUtils;
@GetMapping("/test")
public ResponseEntity test(){
ParamInterceptor paramInterceptor2 = springUtils.getBean(InterceptorConfig.class).getParamInterceptor();
System.out.println("------------------------" + paramInterceptor2);
ParamInterceptor paramInterceptor1 = springUtils.getBean(ParamInterceptor.class);
System.out.println("------------------------" + paramInterceptor1);
return ok().build();
}
}
複製代碼