Reputation: 37
I am new to spring. I want to write a bean definition for the code below.
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;
}
}
I am stuck after this line, i dont know how to include index or name? A little more clarification on index and values would be more than helpful.
<bean id="regexfilter" class="com.abc.common.filter.RegexFilter" />
<constructor-arg name="regexString" />
</bean>
Upvotes: 1
Views: 72
Reputation: 4229
in annotation based manor with pure java you can do something like this:
@Configuration
public class ApplicationConfig {
@Bean
public Filter regexFilter() {
return new RegexFilter("value_for_the_regex_filter");
}
}
or simply:
@Service
public class RegexFilter implements Filter<String> {
...
}
Upvotes: 0
Reputation: 8324
You can define your bean
<bean id="regexfilter" class="com.abc.common.filter.RegexFilter" />
<constructor-arg type="java.lang.String" value="valueforregexstring"/>
</bean>
You can see more about bean definitions here http://www.tutorialspoint.com/spring/constructor_based_dependency_injection.htm
Upvotes: 1