手写springboot自动装配 autoConfiguration

手写springboot自动装配 autoConfiguration,第1张

手写springboot自动装配 autoConfiguration

springboot自动装配 + java敏感词匹配

1. 项目准备

deploy项目 :  https://gitee.com/hctrl/deploy

word-spring-boot-starter : word-spring-boot-start: 敏感词匹配jar

项目已经上传到gitee, 大家可以自行下载查看

2. springboot自动装配必要的步骤

1. resource/meta-INF/spring.factories

2. 一个被@Configuration注解修饰的类

3. 一个被@ConfigurationProperties注解修饰的类, 用来读取配置,

项目目录如下 : 

 接下来我们看一下每个文件的内容

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.hctrl.wordspringbootstart.autoConfig.config.SensitiveWordAutoConfiguration

 

 接下来看一下自动装配的类实现

com.hctrl.wordspringbootstart.autoConfig.config.SensitiveWordAutoConfiguration 

 

@Configuration
@EnableConfigurationProperties({SensitiveWordProperties.class, ConvertedRuleProperties.class})
public class SensitiveWordAutoConfiguration {

	@Bean
	public SensitiveWordFilter sensitiveWordFilter(SensitiveWordProperties sensitiveWordProperties,
												   ConvertedRuleProperties convertedRuleProperties){
		SensitiveWordFilter sensitiveWordFilter = new SensitiveWordFilter();
		Map map = new HashMap<>();
		if (sensitiveWordProperties.getSensitiveWordList() != null){
			map = ProcessWordMapUtil.process(sensitiveWordProperties.getSensitiveWordList());
		}
		sensitiveWordFilter.setSensitiveWordMap(map);
		if (convertedRuleProperties.getMatchType() != null){
			sensitiveWordFilter.setMatchType(convertedRuleProperties.getMatchType());
		}
		if (convertedRuleProperties.getConvertedChar() != null){
			sensitiveWordFilter.setConvertedChar(convertedRuleProperties.getConvertedChar());
		}
		return sensitiveWordFilter;
	}

}

 接下来分别看一下两个配置类的内容

 

@ConfigurationProperties(prefix = "converted.rule")
public class ConvertedRuleProperties {

	private String matchType;

	private String convertedChar;

	public String getMatchType() {
		return matchType;
	}

	public void setMatchType(String matchType) {
		this.matchType = matchType;
	}

	public String getConvertedChar() {
		return convertedChar;
	}

	public void setConvertedChar(String convertedChar) {
		this.convertedChar = convertedChar;
	}
}

 

@ConfigurationProperties(prefix = "word.match")
public class SensitiveWordProperties {

	private Set sensitiveWordList;

	public Set getSensitiveWordList() {
		return sensitiveWordList;
	}

	public void setSensitiveWordList(Set sensitiveWordList) {
		this.sensitiveWordList = sensitiveWordList;
	}
}

 

 再看一下自己配置装配的类的内容

 

package com.hctrl.wordspringbootstart.autoConfig.filter;


import com.hctrl.wordspringbootstart.autoConfig.util.ProcessWordMapUtil;
import org.springframework.beans.factory.InitializingBean;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;



public class SensitiveWordFilter implements InitializingBean {

	public String matchType = "max";      // min : 最小匹配,  max: 最大匹配, 默认是最小匹配
	public String convertedChar = "*";        // 替换敏感词字符
	public Map sensitiveWordMap;

	public Map getSensitiveWordMap() {
		return sensitiveWordMap;
	}

	public void setSensitiveWordMap(Map sensitiveWordMap) {
		this.sensitiveWordMap = sensitiveWordMap;
	}

	public String getMatchType() {
		return matchType;
	}

	public void setMatchType(String matchType) {
		this.matchType = matchType;
	}

	public String getConvertedChar() {
		return convertedChar;
	}

	public void setConvertedChar(String convertedChar) {
		this.convertedChar = convertedChar;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		//自己可以进行扩展
	}

	
	public boolean isContainSensitiveWord(String txt) {
		boolean flag = false;
		for (int i = 0; i < txt.length(); i++) {
			int matchFlag = checkSensitiveWord(txt, i); //判断是否包含敏感字符
			if (matchFlag > 0) {    //大于0存在,返回true
				flag = true;
			}
		}
		return flag;
	}


	
	public Integer checkSensitiveWord(String txt, int beginIndex) {
		boolean flag = false;    //敏感词结束标识位:用于敏感词只有1位的情况
		int matchFlag = 0;     //匹配标识数默认为0
		Map nowMap = sensitiveWordMap;
		for (int i = beginIndex; i < txt.length(); i++) {
			char word = txt.charAt(i);
			nowMap = (Map) nowMap.get(word);     //获取指定key
			if (nowMap != null) {     //存在,则判断是否为最后一个
				matchFlag++;     //找到相应key,匹配标识+1
				if ("1".equals(nowMap.get("isEnd"))) {       //如果为最后一个匹配规则,结束循环,返回匹配标识数
					flag = true;       //结束标志位为true
					if ("min".equals(matchType)) {    //最小规则,直接返回,最大规则还需继续查找
						break;
					}
				}
			} else {     //不存在,直接返回
				break;
			}
		}
		if (!flag) {        //长度必须大于等于1,为词
			matchFlag = 0;
		}
		return matchFlag;
	}


	
	public String changeSensitiveWord(String text) {
		int size = text.length();
		for (int i = 0; i < size; i++) {
			int length = checkSensitiveWord(text, i);    //判断是否包含敏感字符
			if (length > 0) {    //存在,加入list中
				text = converted(text, i, i + length);
				i = i + length - 1;    //减1的原因,是因为for会自增
			}
		}

		return text;
	}

	private String converted(String text, Integer startIndex, Integer endIndex) {

		String start = text.substring(0, startIndex);
		String end = text.substring(endIndex);
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = startIndex; i < endIndex; i++) {
			stringBuilder.append(convertedChar);
		}
		return new StringBuilder().append(start).append(stringBuilder.toString()).append(end).toString();
	}

	public static void main(String[] args) {

		SensitiveWordFilter sensitiveWordFilter = new SensitiveWordFilter();
		Set set = new HashSet<>();
		set.add("地上霜");
		set.add("举头");
		sensitiveWordFilter.setSensitiveWordMap(ProcessWordMapUtil.process(set));
		Boolean flag = sensitiveWordFilter.isContainSensitiveWord("床前明月光,疑是地上霜,举头望明月,低头思故乡");
		System.out.println(sensitiveWordFilter.changeSensitiveWord("床前明月光,疑是地上霜,举头望明月,低头思故乡"));

		System.out.println(flag);

	}


}

最后就是通过这个bean来进行敏感词匹配的 

springboot自动装配到此就完成了

最后进行打包就可以了

注意 :  打包时在pom文件中要加这个配置

 springboot自动装配引用

在deploy项目中, 引入刚刚打包好的jar包

 导入之后 ,  我们就可以在包库中找到我们导入的jar包了

 

之后就可以在配置文件中配置变量了

 我们再写一个测试的controller

 到此springboot自动装配和使用讲解完毕

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691025.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存