package start.util.Filter; 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 = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } } //获取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); } } package start.admin.method; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.spring.LifecycleBeanPostProcessor; import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.aspectj.lang.annotation.Aspect; import org.junit.AfterClass; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.web.WebMvcProperties; import org.springframework.boot.autoconfigure.webservices.WebServicesProperties; import org.springframework.cglib.proxy.MethodProxy; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import start.HelloWorldApplication; import start.ServletInitializer; import start.admin.dao.user.OnlineUserDao; import start.util.Filter.SpringUtil; import start.util.html.ConfigXml; import javax.annotation.Resource; import java.lang.reflect.Method; import java.util.List; public class Test { public String aa() { ApplicationContext context = SpringUtil.getApplicationContext(); OnlineUserDao a =context.getBean(OnlineUserDao.class); Long list=a.count(); //onlineUserDao.findAll(); System.out.println(list); return ""; } }
关注"都市百货" 了解南陵
微信咨询wanglf2r(不拉群 发广告者勿加)
热门评论