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

做外贸怎么进入国外的网站上海建设人才网站

做外贸怎么进入国外的网站,上海建设人才网站,企业网站类型主要包括,模板免费下载官网php cdi今天#xff0c;我将向您展示如何编写CDI扩展。 CDI提供了一种扩展功能的简便方法#xff0c;例如 添加自己的范围#xff0c; 启用Java核心类进行扩展#xff0c; 使用注释元数据进行扩充或修改#xff0c; 以及更多。 在本教程中#xff0c;我们将实现一个… php cdi 今天我将向您展示如何编写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创建或更改行为。 还可以通过引入一组生产者方法来实现属性注入但是这种方法需要每种字段类型使用一个生产者方法。 使用这种通用方法您只需要添加新的转换器即可启用其他类型的值的注入。 参考 教程在Coders Kitchen博客上由我们的JCG合作伙伴 Peter Daum 编写您自己的CDI扩展 。 翻译自: https://www.javacodegeeks.com/2014/02/tutorial-writing-your-own-cdi-extension.htmlphp cdi
http://wiki.neutronadmin.com/news/182270/

相关文章:

  • 廉江手机网站建设公司济南万速网站建设
  • 服务号不认证可做微网站吗深圳网站建设icxun
  • 网站建设的因素网站的优点和缺点
  • 天河区门户网站教育局板块郑州同济医院收费高吗
  • 网站建设个人工作总结福田欧曼行星
  • 那些网站可以做宣传wordpress退出代码
  • 企业网站建设太原网站建设儿童网站模板
  • 无锡企业自助建站系统多语言网站怎么实现
  • allintitle:湛江网站建设 seowordpress分类自定义文字
  • 个人注册一个小公司要多少钱张家港网站建设优化
  • 大庆做流产油城女子网站企业网站系统
  • 八亿wap建站网站优化seo培
  • 做视频网站 投入购物网站功能报价
  • 广西住房与建设厅网站首页跨境电商saas
  • 淮南市网站开发的方式电商平台项目商业计划书
  • 凡科免费建站wordpress注册
  • 网站查询关键词排名软件无锡 网站建设
  • 镇江企业网站湖南做网站 地址磐石网络
  • wordpress ip 改变重庆网站seo方法
  • 深圳网络营销十年乐云seo专家长沙百度首页优化
  • 新乡市建设路小学网站网站建设现状 数据
  • 成都打鱼网站建设短网址生成器是什么意思
  • 网站建设及维修合同范本网站推广公司黄页
  • 建设门户网站的基本意义有哪些合肥网络运营公司哪家好
  • 网站会员营销营销推广文案
  • 工业设计网站导航360免费
  • 网站建设的实施方案免费网战空间
  • 成都企业建站公司在线咨询搜索量查询百度指数
  • 深圳市官网网站建设哪家好易企秀怎么做招聘网站超链接
  • 网站栏目标题小说网站的里面的搜索是怎么做的