自定义注解
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface FreeMarkerCustomTag {
String value() default "";
}
使用自定义注解
@FreeMarkerCustomTag("permitShow")
public class PermitShowTag extends BaseTag {
}
通过自定注解获取bean
/**
*
* @author guokaige
* @Date 2019年5月7日 下午1:51:37
*/
@Component
public class FreeMarkerConfig implements ApplicationContextAware{
ApplicationContext applicationContext ;
@Autowired
protected freemarker.template.Configuration configuration;
@PostConstruct
public void setSharedVariable() throws IOException, TemplateException {
// 根据注解获取bean ,key is bean name ,value is bean object
Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(FreeMarkerCustomTag.class);
for (String key : map.keySet()) {
configuration.setSharedVariable(key, map.get(key));
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
}