`
cy729215495
  • 浏览: 127351 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

freemark自定义标签类 注入spring 的service

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics