pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy1</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>.\lib</outputDirectory>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
IValidator.java
package com.compare;
public interface IValidator<T> {
boolean isValid(T instance);
}
GreaterThanFourValidator.java
package com.compare;
public class GreaterThanFourValidator implements IValidator<Integer> {
@Override
public boolean isValid(Integer instance) {
return instance > 4;
}
}
IntegerOnlyValidator.java
package com.compare;
public class IntegerOnlyValidator implements IValidator<String>{
@Override
public boolean isValid(String instance) {
int i = 0;
boolean b = false;
try {
Integer.parseInt(instance);
b = true;
}
catch (Exception e)
{
}
return b;
}
}
GreaterThanFourValidatorTest.java
package com.compare;
public class GreaterThanFourValidatorTest {
IValidator<Integer> validator;
public IValidator<Integer> getValidator() {
return validator;
}
public void setValidator(IValidator<Integer> validator) {
this.validator = validator;
}
}
IntegerOnlyValidatorTest.java
package com.compare;
public class IntegerOnlyValidatorTest {
IValidator<String> validator;
public IValidator<String> getValidator() {
return validator;
}
public void setValidator(IValidator<String> validator) {
this.validator = validator;
}
}
Main.java
package com.compare;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
try(ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml")) {
IntegerOnlyValidatorTest intOnly = (IntegerOnlyValidatorTest) context.getBean("IntegerOnlyTest");
System.out.println(intOnly.validator.isValid("5554"));
System.out.println(intOnly.validator.isValid("123ab"));
GreaterThanFourValidatorTest greater4Only = (GreaterThanFourValidatorTest) context.getBean("GreaterThanFourTest");
System.out.println(greater4Only.validator.isValid(10));
System.out.println(greater4Only.validator.isValid(1));
}
}
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="GreaterThanFour" class="com.compare.GreaterThanFourValidator" />
<bean id="IntegerOnly" class="com.compare.IntegerOnlyValidator" />
<bean id="GreaterThanFourTest" class="com.compare.GreaterThanFourValidatorTest">
<property name="validator">
<ref local="GreaterThanFour"/>
</property>
</bean>
<bean id="IntegerOnlyTest" class="com.compare.IntegerOnlyValidatorTest">
<property name="validator">
<ref local="IntegerOnly"/>
</property>
</bean>
</beans>