我是春天新手。我想为下面的代码编写一个bean定义。
package com.abc.common.filter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class RegexFilter implements Filter<String> {
Logger logger = Logger.getLogger(RegexFilter.class);
private Pattern regex;
private String lastMatch;
public RegexFilter(String regexString) {
this.lastMatch = null;
regex = Pattern.compile(regexString, Pattern.UNICODE_CHARACTER_CLASS);
}
@Override
public boolean matches(String text) {
text = text.toLowerCase();
if (text == null) {
logger.error("No text set for matching!");
return false;
}
Matcher matcher = regex.matcher(text);
if (matcher.find(0)) {// always start at index 0
this.lastMatch = matcher.group();
if (logger.isDebugEnabled()) {
logger.debug(matcher.group() + " found!");
}
return true;
}
return false;
}
public String getLastMatch(){
return this.lastMatch;
}
}
我被困在这一行之后,我不知道如何包含索引或名称?对指数和价值的更多澄清将是非常有用的。
<bean id="regexfilter" class="com.abc.common.filter.RegexFilter" />
<constructor-arg name="regexString" />
</bean>
答案 0 :(得分:1)
您可以定义您的bean
<bean id="regexfilter" class="com.abc.common.filter.RegexFilter" />
<constructor-arg type="java.lang.String" value="valueforregexstring"/>
</bean>
您可以在此处查看有关bean定义的更多信息 http://www.tutorialspoint.com/spring/constructor_based_dependency_injection.htm
答案 1 :(得分:0)
在基于注释的庄园中使用纯java,你可以这样做:
@Configuration
public class ApplicationConfig {
@Bean
public Filter regexFilter() {
return new RegexFilter("value_for_the_regex_filter");
}
}
或简单地说:
@Service
public class RegexFilter implements Filter<String> {
...
}