当前位置: 首页 > news >正文

常州网站建设费用wordpress改头像

常州网站建设费用,wordpress改头像,seo优化主要做什么,wordpress searchform.php今天#xff0c;我将向您展示如何编写CDI扩展。 CDI提供了一种扩展功能的简便方法#xff0c;例如 添加自己的范围#xff0c; 启用Java核心类进行扩展#xff0c; 使用注释元数据进行扩充或修改#xff0c; 以及更多。 在本教程中#xff0c;我们将实现一个扩展我将向您展示如何编写CDI扩展。 CDI提供了一种扩展功能的简便方法例如 添加自己的范围 启用Java核心类进行扩展 使用注释元数据进行扩充或修改 以及更多。 在本教程中我们将实现一个扩展该扩展将从属性文件中注入属性就像往常一样我将在github [更新 sources github ]中提供源。 目标 提供扩展使我们能够执行以下操作 PropertyFile(myProps.txt) public class MyProperties {Property(version)Integer version;Property(appname)String appname; } 其中版本和应用程序的名字在文件myProps.txt定义。 制备 首先我们需要CDI API的依赖项 dependencies.compile javax.enterprise:cdi-api:1.1-20130918 现在我们可以开始了。 让我们 弄湿 基础 每个CDI扩展的入口点都是一个实现javax.enterprise.inject.spi.Extension的类 package com.coderskitchen.propertyloader;import javax.enterprise.inject.spi.Extension; public class PropertyLoaderExtension implements Extension { // More code later } 另外我们必须将此类的全限定名添加到META-INF / services目录中名为javax.enterprise.inject.spi.Extension的文件中。 javax.enterprise.inject.spi.Extension com.coderskitchen.propertyloader.PropertyLoaderExtension 这些是编写CDI扩展的基本步骤。 背景资料 CDI使用Java SE的服务提供商体系结构这就是为什么我们需要实现标记接口并在实现类的FQN中添加文件的原因。 潜水更深 现在我们必须选择正确的事件来收听。 背景资料 CDI规范定义了几个事件这些事件在应用程序初始化期间由容器触发。 例如在容器以bean发现开始之前将触发BeforeBeanDiscovery 。 对于本教程我们需要监听ProcessInjectionTarget事件。 对于发现的每个Java类接口或枚举将触发此事件并且可能在运行时由容器实例化该事件。 因此让我们添加此事件的观察者 public T void initializePropertyLoading(final Observes ProcessInjectionTargetT pit) { } ProcessInjectionTarget通过getAnnotatedType方法授予对基础类的访问权限并通过getInjectionTarget授予创建中的实例的访问权限。 我们使用annotatedType获取类上的注释以检查PropertyFile是否可用。 如果没有我们将直接作为短路返回。 随后 InjectionTarget用于覆盖当前行为并从属性文件中设置值。 public T void initializePropertyLoading(final Observes ProcessInjectionTargetT pit) {AnnotatedTypeT at pit.getAnnotatedType();if(!at.isAnnotationPresent(PropertyFile.class)) {return;}} 在本教程中我们假定属性文件直接位于类路径的根目录中。 基于此假设我们可以添加以下代码以从文件中加载属性 PropertyFile propertyFile at.getAnnotation(PropertyFile.class); String filename propertyFile.value(); InputStream propertiesStream getClass().getResourceAsStream(/ filename); Properties properties new Properties(); try {properties.load(propertiesStream);assignPropertiesToFields(at.getFields, properties); // Implementation follows } catch (IOException e) {e.printStackTrace(); } 现在我们可以将属性值分配给字段。 但是对于CDI我们必须以略有不同的方式执行此操作。 我们应该使用InjectionTarget并覆盖当前的AnnotatedType。 这使CDI可以确保所有事情都可以按正确的顺序进行。 为了实现这一点我们使用了最终的Map FieldObject 我们可以在其中存储当前分配以供以后在InjectionTarget中使用 。 映射是在方法AssignPropertiesToFields中完成的。 private T void assignPropertiesToFields(SetAnnotatedField? super T fields, Properties properties) {for (AnnotatedField? super T field : fields) {if(field.isAnnotationPresent(Property.class)) {Property property field.getAnnotation(Property.class);String value properties.getProperty(property.value());Type baseType field.getBaseType();fieldValues.put(memberField, value);}} 最后我们现在将创建一个新的InjectionTarget以将字段值分配给所有新创建的基础类实例。 final InjectionTargetT it pit.getInjectionTarget();InjectionTargetT wrapped new InjectionTargetT() {Overridepublic void inject(T instance, CreationalContextT ctx) {it.inject(instance, ctx);for (Map.EntryField, Object property: fieldValues.entrySet()) {try {Field key property.getKey();key.setAccessible(true);Class? baseType key.getType();String value property.getValue().toString();if (baseType String.class) {key.set(instance, value);} else if (baseType Integer.class) {key.set(instance, Integer.valueOf(value));} else {pit.addDefinitionError(new InjectionException(Type baseType of Field key.getName() not recognized yet!));}} catch (Exception e) {pit.addDefinitionError(new InjectionException(e));}}}Overridepublic void postConstruct(T instance) {it.postConstruct(instance);}Overridepublic void preDestroy(T instance) {it.dispose(instance);}Overridepublic void dispose(T instance) {it.dispose(instance);}Overridepublic SetInjectionPoint getInjectionPoints() {return it.getInjectionPoints();}Overridepublic T produce(CreationalContextT ctx) {return it.produce(ctx);}};pit.setInjectionTarget(wrapped); 这就是做魔术的全部。 最后这里是ProperyLoaderExtension的完整代码。 PropertyLoaderExtension package com.coderskitchen.propertyloader;import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.event.Observes; import javax.enterprise.inject.InjectionException; import javax.enterprise.inject.spi.AnnotatedField; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionPoint; import javax.enterprise.inject.spi.InjectionTarget; import javax.enterprise.inject.spi.ProcessInjectionTarget; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Set;public class PropertyLoaderExtension implements Extension {final MapField, Object fieldValues new HashMapField, Object();public T void initializePropertyLoading(final Observes ProcessInjectionTargetT pit) {AnnotatedTypeT at pit.getAnnotatedType();if(!at.isAnnotationPresent(PropertyyFile.class)) {return;}PropertyyFile propertyyFile at.getAnnotation(PropertyyFile.class);String filename propertyyFile.value();InputStream propertiesStream getClass().getResourceAsStream(/ filename);Properties properties new Properties();try {properties.load(propertiesStream);assignPropertiesToFields(at.getFields(), properties);} catch (IOException e) {e.printStackTrace();}final InjectionTargetT it pit.getInjectionTarget();InjectionTargetT wrapped new InjectionTargetT() {Overridepublic void inject(T instance, CreationalContextT ctx) {it.inject(instance, ctx);for (Map.EntryField, Object property: fieldValues.entrySet()) {try {Field key property.getKey();key.setAccessible(true);Class? baseType key.getType();String value property.getValue().toString();if (baseType String.class) {key.set(instance, value);} else if (baseType Integer.class) {key.set(instance, Integer.valueOf(value));} else {pit.addDefinitionError(new InjectionException(Type baseType of Field key.getName() not recognized yet!));}} catch (Exception e) {pit.addDefinitionError(new InjectionException(e));}}}Overridepublic void postConstruct(T instance) {it.postConstruct(instance);}Overridepublic void preDestroy(T instance) {it.dispose(instance);}Overridepublic void dispose(T instance) {it.dispose(instance);}Overridepublic SetInjectionPoint getInjectionPoints() {return it.getInjectionPoints();}Overridepublic T produce(CreationalContextT ctx) {return it.produce(ctx);}};pit.setInjectionTarget(wrapped);}private T void assignPropertiesToFields(SetAnnotatedField? super T fields, Properties properties) {for (AnnotatedField? super T field : fields) {if(field.isAnnotationPresent(Propertyy.class)) {Propertyy propertyy field.getAnnotation(Propertyy.class);Object value properties.get(propertyy.value());Field memberField field.getJavaMember();fieldValues.put(memberField, value);}}} }资料下载 完整的源代码将在git hub上提供直到星期一晚上。 jar存档可在此处PropertyLoaderExtension中找到 。 最后的笔记 本教程说明了如何轻松地向CDI框架添加新功能。 在几行代码中添加了工作属性加载和注入机制。 在应用程序的生命周期中触发的事件提供了一种松散耦合且功能强大的方法来添加新功能拦截bean创建或更改行为。 属性注入也可以通过引入一组生产者方法来实现但是这种方法需要每个字段类型一个生产者方法。 使用这种通用方法您只需要添加新的转换器即可启用其他类型的值的注入。 参考 教程从我们的JCG合作伙伴 Peter Daum在Coders Kitchen博客上编写自己的CDI扩展 。 翻译自: https://www.javacodegeeks.com/2014/02/tutorial-writing-your-own-cdi-extension.html
http://www.huolong8.cn/news/4346/

相关文章:

  • 最新网站制作seo综合查询接口
  • 服务佳的网站建设遵义做网站哪个公司最好
  • 网站建设企业服务商百家号如何给网站做推广
  • 栾川网站开发网站seo分析报告案例
  • 建设网站如何加入搜索老总办公室装修风格
  • 网站内链建设门户网站建设 增强责任意识
  • 网站侧边栏菜单广东平台网站建设制作
  • 站长工具百度百科房屋装修效果图app有哪些
  • 网站建设方案书1500字wordpress协会主题
  • 网站处于建设中会显示什么英文c2c网站功能模块设计
  • wordpress图片站点云南省人防工程建设网站
  • 企业自助建站金融 网站 源码
  • 网上商城建设网站常规seo优化步骤
  • 有什么网站是python做的网站dede后台
  • 注册个人公司流程及费用内蒙古seo
  • 做网站需要画原型图么网站建设改变某个表格大小
  • 做网站做得好的公司有哪些网站上的flv视频看不了
  • 哇哈哈网站建设策划书渠道推广方案
  • l网站建设携程网站建设的优缺点
  • 网站上线 flashwordpress落地页改造
  • 做视频网站容易收录吗店铺首页如何设计
  • 长沙建网站的公司多少钱wordpress 模拟word
  • 网站找人做seo然后网站搜不到了企业信息填报指南
  • 有哪些网站做的比较好看的图片有哪些网站做自建房设计
  • 昆明网站建设搭建aardio能开发手机应用吗
  • 制作哪个网站好已备案网站数量
  • 陕西省交通建设集团西长分公司网站营口手机网站建设
  • 去哪里做网站安全等级保护级别广州网站开发哪家好
  • 效果好网站建设哪家好女性logo大全图片
  • 深圳住房建筑网站石景山郑州阳网站建设