Skip to content

Instantly share code, notes, and snippets.

@karanmalhi
Created May 26, 2011 17:01
Show Gist options
  • Save karanmalhi/993515 to your computer and use it in GitHub Desktop.
Save karanmalhi/993515 to your computer and use it in GitHub Desktop.
Example of multiple criterias in custom qualifier and interface with multiple imps
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.lq.interfaceimpl"></context:component-scan>
<!-- Below is example for multiple attributes of qualifiers -->
<bean id="hw1" class="com.lq.interfaceimpl.HardWorker">
<property name="name" value="HARD-LOW"/>
<qualifier type="com.lq.interfaceimpl.Criteria">
<attribute key="type" value="HARD"/>
<attribute key="rate" value="LOW"/>
</qualifier>
</bean>
<bean id="lw1" class="com.lq.interfaceimpl.HardlyWorks">
<property name="name" value="LAZY-LOW"/>
<qualifier type="com.lq.interfaceimpl.Criteria">
<attribute key="type" value="LAZY"/>
<attribute key="rate" value="LOW"/>
</qualifier>
</bean>
<bean id="hw2" class="com.lq.interfaceimpl.HardWorker">
<property name="name" value="HARD-MEDIUM"/>
<qualifier type="com.lq.interfaceimpl.Criteria">
<attribute key="type" value="HARD"/>
<attribute key="rate" value="MEDIUM"/>
</qualifier>
</bean>
<bean id="lw2" class="com.lq.interfaceimpl.HardlyWorks">
<property name="name" value="LAZY-MEDIUM"/>
<qualifier type="com.lq.interfaceimpl.Criteria">
<attribute key="type" value="LAZY"/>
<attribute key="rate" value="MEDIUM"/>
</qualifier>
</bean>
</beans>
package com.lq.interfaceimpl;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.beans.factory.annotation.Qualifier;
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Criteria {
WorkerType type();
PayRate rate();
enum WorkerType{HARD,LAZY}
enum PayRate{LOW,MEDIUM,HIGH}
}
package com.lq.interfaceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.lq.interfaceimpl.Criteria.PayRate;
import com.lq.interfaceimpl.Criteria.WorkerType;
public class InterfaceImplTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"annotated-injection.xml");
Task task = (Task) context.getBean("task");
task.execute();
SpecialTask specialTask = (SpecialTask) context.getBean("specialTask");
specialTask.execute();
}
}
interface Worker {
void doWork();
String getName();
void setName(String name);
}
@Component
class HardlyWorks implements Worker {
private String name="Colleen";
@Override
public void doWork() {
System.out.println(name+" HARDLY WORKS");
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
@Component
class HardWorker implements Worker {
private String name = "Sarah";
@Override
public void doWork() {
System.out.println(name+" WORKS VERY HARD");
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
@Component
class Task {
@Autowired
@Qualifier("hardWorker")
private Worker worker;
public void execute(){
worker.doWork();
}
}
@Component
class SpecialTask {
@Autowired
@Criteria(rate=PayRate.LOW,type=WorkerType.HARD)
private Worker worker;
public void execute(){
worker.doWork();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment