Flyweight模式的Spring实现
我有一个Flyweight模式在java编程中实现,没有Spring。请建议如何将其作为Spring管理的Bean。它有静态方法调用和内部类。其目的是最初从CRM加载所有选项集,以便在应用程序中共享相同的选项集。这可以防止向CRM打昂贵的电话。用户被限制创建任何新的选项集,因此不能创建内部类。首先需要实现为Springbean,然后让它成为ApplicationContextAware以引用其他bean。我主要使用SpringXML配置来定义beanFlyweight模式的Spring实现,spring,design-patterns,javabeans,flyweight-pattern,Spring,Design Patterns,Javabeans,Flyweight Pattern,我有一个Flyweight模式在java编程中实现,没有Spring。请建议如何将其作为Spring管理的Bean。它有静态方法调用和内部类。其目的是最初从CRM加载所有选项集,以便在应用程序中共享相同的选项集。这可以防止向CRM打昂贵的电话。用户被限制创建任何新的选项集,因此不能创建内部类。首先需要实现为Springbean,然后让它成为ApplicationContextAware以引用其他bean。我主要使用SpringXML配置来定义bean public class OptionSet
public class OptionSetFactory{
private static Map <String, Object>optionSets = new HashMap();
//Inner class to restrict users creating OptionSet
private class OptionSet implements IOptionSet{
private String entityName;
private String attributeName;
private Hashtable<Integer, String> options;
private IOrganizationService service;
private static final String GUID_EMPTY = "00000000-0000-0000-0000-000000000000";
private ApplicationContext context;
OptionSet(String entityName, String attributeName){
this.entityName = entityName;
this.attributeName = attributeName;
//this.options = options;
OrganizationRequest request = new OrganizationRequest();
request.setRequestName("RetrieveAttribute");
Guid guid = new Guid();
guid.setValue(GUID_EMPTY);
ParameterCollection paramCol = new ParameterCollection();
KeyValuePairOfstringanyType kv0 = new KeyValuePairOfstringanyType();
kv0.setKey("MetadataId");
kv0.setValue(guid);
paramCol.getKeyValuePairOfstringanyTypes().add(kv0);
KeyValuePairOfstringanyType kv1 = new KeyValuePairOfstringanyType();
kv1.setKey("EntityLogicalName");
kv1.setValue(entityName);
paramCol.getKeyValuePairOfstringanyTypes().add(kv1);
KeyValuePairOfstringanyType kv2 = new KeyValuePairOfstringanyType();
kv2.setKey("LogicalName");
kv2.setValue(attributeName);
paramCol.getKeyValuePairOfstringanyTypes().add(kv2);
KeyValuePairOfstringanyType kv3 = new KeyValuePairOfstringanyType();
kv3.setKey("RetrieveAsIfPublished");
kv3.setValue(true);
paramCol.getKeyValuePairOfstringanyTypes().add(kv3);
request.setParameters(paramCol);
try {
OrganizationResponse response=service.execute(request);
PicklistAttributeMetadata pickListAttrMetadata = (PicklistAttributeMetadata)response.getResults().getKeyValuePairOfstringanyTypes().get(0).getValue();
OptionSetMetadata optionSetMetadata = pickListAttrMetadata.getOptionSet();
for(OptionMetadata optionMetaData : optionSetMetadata.getOptions().getOptionMetadatas()){
//TODO populate OptionSet from optionsetMetadata
}
} catch (IOrganizationServiceExecuteOrganizationServiceFaultFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public String getAttributeName() {
return attributeName;
}
@Override
public Hashtable<Integer, String> getOptions() {
return options;
}
}
//static block to load predefined OptionSets in HashMap
static{
OptionSetFactory factory = new OptionSetFactory();
optionSets.put("dsl_type", factory.new OptionSet("dsl_type", "dsl_operationalstructure"));
}
//Static method calls for user to fetch OptionSet based on inputs
public static IOptionSet getOptionSet(String entityName, String attributeName){
return (IOptionSet) optionSets.get(entityName+"."+attributeName);
}
public static IOptionSet getOptionSet(String attributeName){
return (IOptionSet) optionSets.get(attributeName.toLowerCase());
}
}
公共类选项集合工厂{
私有静态映射选项集=新HashMap();
//用于限制用户创建OptionSet的内部类
私有类OptionSet实现IOptionSet{
私有字符串entityName;
私有字符串attributeName;
私有哈希表选项;
私人组织服务;
私有静态最终字符串GUID_EMPTY=“00000000-0000-0000-0000-000000000000”;
私有应用程序上下文上下文;
OptionSet(字符串entityName、字符串attributeName){
this.entityName=entityName;
this.attributeName=attributeName;
//this.options=选项;
OrganizationRequest=新建OrganizationRequest();
setRequestName(“RetrieveAttribute”);
Guid=新Guid();
guid.setValue(guid\u为空);
ParameterCollection paramCol=新的ParameterCollection();
KeyValuePairOfstringanyType kv0=新的KeyValuePairOfstringanyType();
kv0.setKey(“MetadataId”);
kv0.setValue(guid);
paramCol.getKeyValuePairOfstringanyTypes().add(kv0);
KeyValuePairOfstringanyType kv1=新的KeyValuePairOfstringanyType();
kv1.setKey(“EntityLogicalName”);
kv1.设置值(entityName);
paramCol.getKeyValuePairOfstringanyTypes().add(kv1);
KeyValuePairOfstringanyType kv2=新的KeyValuePairOfstringanyType();
kv2.设置键(“逻辑名称”);
kv2.设置值(属性名称);
paramCol.getKeyValuePairOfstringanyTypes().add(kv2);
KeyValuePairOfstringanyType kv3=新的KeyValuePairOfstringanyType();
kv3.设置键(“检索已发布”);
kv3.设置值(真);
paramCol.getKeyValuePairOfstringanyTypes().add(kv3);
请求设置参数(paramCol);
试一试{
OrganizationResponse=service.execute(请求);
PicklistAttributeMetadata=(PicklistAttributeMetadata)响应.getResults().getKeyValuePairOfstringanyTypes().get(0.getValue();
OptionSetMetadata OptionSetMetadata=pickListAttrMetadata.getOptionSet();
对于(OptionMetadata OptionMetadata:optionSetMetadata.getOptions().getoptionMetadata()){
//TODO从optionsetMetadata填充OptionSet
}
}捕获(IOOrganizationServiceExecuteOrganizationServiceFaultMessage e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
@凌驾
公共字符串getEntityName(){
返回entityName;
}
@凌驾
公共字符串getAttributeName(){
返回属性名;
}
@凌驾
公共哈希表getOptions(){
返回选项;
}
}
//在HashMap中加载预定义选项集的静态块
静止的{
OptionSetFactory工厂=新的OptionSetFactory();
optionSets.put(“dsl_类型”,factory.new OptionSet”(“dsl_类型”,“dsl_操作结构”);
}
//静态方法调用用户根据输入获取OptionSet
公共静态IOptionSet getOptionSet(字符串entityName,字符串attributeName){
return(IOptionSet)optionSets.get(entityName+“+”+attributeName);
}
公共静态IOptionSet getOptionSet(字符串attributeName){
return(IOptionSet)optionSets.get(attributeName.toLowerCase());
}
}
在我看来,如果您删除了此文件中出现的所有“static”,SpringHi Sean应该可以完全管理它,我无法完全想象它。静态初始值设定项和静态HashMap呢。是否可以在上述情况下发布bean定义是的,使初始值设定项和实例初始值设定项以及hashmap非静态,并且一切都应该工作。请在您看到bean定义工作时发布它,我会尝试并让您知道。在我看来,如果您删除了此文件中所有出现的“static”选项,斯普林希·肖恩应该可以很好地处理,我无法想象。静态初始值设定项和静态HashMap呢。是否可以在上面的情况下发布bean定义是的,使初始值设定项和实例初始值设定项以及hashmap非静态,并且一切都应该工作。请在您看到bean定义工作时发布它,我将尝试并让您知道。