Sillycat Framework(II)Enhancement of BeanFilter

介绍SillycatFramework中BeanFilter接口的实现细节,包括过滤规则配置、属性忽略逻辑及Spring配置策略等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sillycat Framework(II)Enhancement of BeanFilter

1. filter the bean properties
2. consider the Map=null
3. consider the property is POJO, and POJO is null
4. consider the properties file loading and configuration policy

1. BeanFilter interface in Java
BeanFilter.java:
package com.sillycat.easymarket.filter;
public interface BeanFilter {
public void filter(Object obj, String rule);
}

2. BeanFilterImpl implemetation class in Groovy
BeanFilterImpl.groovy:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
import com.sillycat.easymarket.utils.SystemConfigurationAdapter;
import org.springframework.stereotype.Component;

public class BeanFilterImpl implements BeanFilter {

private static final String DEFAULT_RULE = "rule1";

private static final String DOT = ".";

private final Log log = LogFactory.getLog(this.getClass());

@Autowired
SystemConfigurationAdapter filterConfiguration;

public void filter(Object obj, String rule) {
if (null == obj) {
log.info("null Object do not need filter!");
return;
}
if (null == rule || "".equals(rule.trim())) {
rule = DEFAULT_RULE;
}
log.info("filter rule...." + rule);
List<String> filters = filterConfiguration.getStringList(rule);
if (filters != null && !filters.isEmpty()) {
for (int i = 0; i < filters.size(); i++) {
String filter = filters.get(i);
try {
if (!needIgnore(obj, filter)) {
log.info("doing filter rule...." + filter);
PropertyUtils.setProperty(obj, filter, null);
}
} catch (IllegalAccessException e) {
log.error(e);
} catch (InvocationTargetException e) {
log.error(e);
} catch (NoSuchMethodException e) {
log.error(e);
}
}
}
}
private boolean needIgnore(Object obj, String filter)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
if (null == filter && "".equals(filter.trim())) {
flag = true;
return flag;
}
if (isNullValueInPath(obj, filter, 0)) {
flag = true;
return flag;
}
return flag;
}
private boolean isNullValueInPath(Object obj, String filter, int start)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
int location = 0;
if ((location = filter.indexOf(DOT, start)) > 0) {
String name = filter.substring(start, location);
Object obj_tmp = BeanUtils.getProperty(obj, name);
if (obj_tmp == null) {
flag = true;
return flag;
}
isNullValueInPath(obj, filter, location + 1);
}
return flag;
}
}

3. the filter.properties control the filter processor
#filter rule 1
rule1=personPassword
rule1=address.city
rule1=others.other2

#filter rule 2
rule2=personPassword
rule2=address.country
rule2=others.other1
rule2=others.other2
rule2=address.email

4. TestCase of this groovy implementation:
package com.sillycat.easymarket.filter;
import java.util.HashMap;
import java.util.Map;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilterTest extends BaseTestCase {
private BeanFilter beanFilter;
public void setUp() throws Exception {
super.setUp();
beanFilter = (BeanFilter) ctx.getBean("beanFilter");
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testDumy() {
assertTrue(true);
}
public void testFilter() {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("hua.luo@chengdu.digby.com");
a.setId(Integer.valueOf(1));
//p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
//p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
beanFilter.filter(p, null);
System.out.println(p);
System.out.println(p.getOthers());
}
}

5. properties file loading policy in spring
<bean id="filterConfiguration" class="com.sillycat.easymarket.utils.SystemConfigurationAdapter">
<property name="configfile" value="filter.properties"/>
</bean>

package com.sillycat.easymarket.utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SystemConfigurationAdapter {
private final Log log = LogFactory.getLog(this.getClass());
private CompositeConfiguration config;
private PropertiesConfiguration propertiesConfig;
private String configfile = "easygroovy.properties";
public void init() {
config = new CompositeConfiguration();
if (propertiesConfig == null) {
try {
propertiesConfig = new PropertiesConfiguration(configfile);
log.info("configuration file loading..." + configfile);
propertiesConfig
.setReloadingStrategy(new FileChangedReloadingStrategy());
config.addConfiguration(propertiesConfig);
} catch (ConfigurationException e) {
log.error(e);
}
}
}
public String getString(String propertyKey) {
if (config == null) {
init();
}
return config.getString(propertyKey);
}
public String getString(String propertyKey, String defaultValue) {
if (config == null) {
init();
}
return config.getString(propertyKey, defaultValue);
}
public int getInt(String propertyKey) {
if (config == null) {
init();
}
return config.getInt(propertyKey);
}
public int getInt(String key, int defaultValue) {
if (config == null) {
init();
}
return config.getInt(key, defaultValue);
}
public float getFloat(String propertyKey) {
if (config == null) {
init();
}
return config.getFloat(propertyKey);
}
public float getFloat(String propertyKey, float defaultValue) {
if (config == null) {
init();
}
return config.getFloat(propertyKey, defaultValue);
}
public boolean getBoolean(String propertyKey) {
if (config == null) {
init();
}
return config.getBoolean(propertyKey);
}
public boolean getBoolean(String propertyKey, boolean defualtValue) {
return config.getBoolean(propertyKey, defualtValue);
}
public String[] getStringArray(String propertyKey) {
if (config == null) {
init();
}
return config.getStringArray(propertyKey);
}
public List<String> getStringList(String propertyKey) {
List<String> list = new ArrayList<String>();
String[] strArr = getStringArray(propertyKey);
for (String value : strArr) {
list.add(value);
}
return list;
}
@SuppressWarnings("rawtypes")
public List getList(String propertyKey) {
if (config == null) {
init();
}
return config.getList(propertyKey);
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
}
Windows 系统修复工具主要用于解决 Windows 11/10 系统中的各种常见问题,具有操作简单、功能全面等特点: 文件资源管理器修复:可解决文件资源管理器卡死、崩溃、无响应等问题,能终止崩溃循环。还可修复右键菜单无响应或选项缺失问题,以及重建缩略图缓存,让图片、视频等文件的缩略图正常显示,此外,还能处理桌面缺少回收站图标、回收站损坏等问题。 互联网和连接修复:能够刷新 DNS 缓存,加速网页加载速度,减少访问延迟。可重置 TCP/IP 协议栈,增强网络连接稳定性,减少网络掉线情况,还能还原 Hosts 文件,清除恶意程序对网络设置的篡改,保障网络安全,解决电脑重装系统后网络无法连接、浏览器主页被篡改等问题。 系统修复:集成系统文件检查器(SFC),可自动扫描并修复受损的系统文件。能解决 Windows 激活状态异常的问题,还可重建 DLL 注册库,恢复应用程序兼容性,解决部分软件无法正常运行的问题,同时也能处理如 Windows 沙箱无法启动、Windows 将 JPG 或 JPEG 保存为 JFIF 等系统问题。 系统工具维护:提供启动管理器、服务管理器和进程管理器等工具,用户可控制和管理启动程序、系统服务和当前运行的进程,提高系统的启动和运行速度,防止不必要的程序和服务占用系统资源。还能查看系统规格,如处理器线程数、最大显示分辨率等。 故障排除:集成超过 20 个微软官方诊断工具,可对系统问题进行专业排查,还能生成硬件健康状态报告。能解决搜索和索引故障、邮件和日历应用程序崩溃、设置应用程序无法启动等问题,也可处理打印机、网络适配器、Windows 更新等相关故障。 其他修复功能:可以重置组策略设置、catroot2 文件夹、记事本等多种系统设置和组件,如重置 Windows 应用商店缓存、Windows 防火墙设置等。还能添加重建图标缓存支持,恢复粘滞便笺删除
资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 今天给大家分享一个关于C#自定义字符串替换方法的实例,希望能对大家有所帮助。具体介绍如下: 之前我遇到了一个算法题,题目要求将一个字符串中的某些片段替换为指定的新字符串片段。例如,对于源字符串“abcdeabcdfbcdefg”,需要将其中的“cde”替换为“12345”,最终得到的结果字符串是“ab12345abcdfb12345fg”,即从“abcdeabcdfbcdefg”变为“ab12345abcdfb12345fg”。 经过分析,我发现不能直接使用C#自带的string.Replace方法来实现这个功能。于是,我决定自定义一个方法来完成这个任务。这个方法的参数包括:原始字符串originalString、需要被替换的字符串片段strToBeReplaced以及用于替换的新字符串片段newString。 在实现过程中,我首先遍历原始字符串,查找需要被替换的字符串片段strToBeReplaced出现的位置。找到后,就将其替换为新字符串片段newString。需要注意的是,在替换过程中,要确保替换操作不会影响后续的查找和替换,避免遗漏或重复替换的情况发生。 以下是实现代码的大概逻辑: 初始化一个空的字符串result,用于存储最终替换后的结果。 使用IndexOf方法在原始字符串中查找strToBeReplaced的位置。 如果找到了,就将originalString中从开头到strToBeReplaced出现位置之前的部分,以及newString拼接到result中,然后将originalString的查找范围更新为strToBeReplaced之后的部分。 如果没有找到,就直接将剩余的originalString拼接到result中。 重复上述步骤,直到originalStr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值