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的时候,也可以这样获取