public class Service {
private Dao dao;
@Inject
public Service(Dao dao) {
this.dao = dao;
}
}
public interface Dao {}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
public @interface Oracle {}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
public @interface MySQL {}
public class OracleDao implements Dao {}
public class MySQLDao implements Dao {}
public class Service {
@Inject
@Oracle
private Dao dao;
}
@Singleton
public class SingletonService {
// ...
}
<!-- applicationContext.xml -->
<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">
<context:component-scan base-package="com.example" />
</beans>
// Service.java
@Service
public class Service {
@Autowired
private Dao dao;
// ...
}
// Dao.java
public interface Dao {}
@Repository
public class OracleDao implements Dao {}
@Repository
public class MySQLDao implements Dao {}