package com.orchid.system.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
applicationContext = ac;
}
public static Object getBean(String name) throws BeansException{
return applicationContext.getBean(name);
}
}
注意这里一点要加注解@component
然后是web.xml
<listener> <listener-class>com.orchid.system.util.SpringBeanUtil</listener-class> </listener>
然后是freemarker自定义标签的类
private ContentReleaseService contentReleaseService;
public AdListDirective(){
if(contentReleaseService==null)
this.contentReleaseService=(ContentReleaseService) SpringBeanUtil.getBean("contentReleaseServiceImpl");
}
new classpathxmlapplicationcontext()不知为何会报堆满了,还是这种方式获取bean很好。
还是shiro需要获取service的时候,也可以这样获取
本文介绍了一种在Spring框架中高效获取Bean的方法,通过自定义工具类SpringBeanUtil实现,并展示了如何在Freemarker标签类中应用该工具类来初始化业务逻辑所需的Service实例。

被折叠的 条评论
为什么被折叠?



