Created
May 26, 2011 17:01
-
-
Save karanmalhi/993515 to your computer and use it in GitHub Desktop.
Example of multiple criterias in custom qualifier and interface with multiple imps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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