spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.4k stars 38.06k forks source link

New Java 6 ServiceLoader support in the springframework core [SPR-3269] #7954

Closed spring-projects-issues closed 12 years ago

spring-projects-issues commented 17 years ago

Ben Tels opened SPR-3269 and commented

L.S.

The Java SE 6 platform introduces the concept of a ServiceLoader, to replace the class.forName() construct. I think it would be useful to have a FactoryBean in the Spring Framework that would make it possible to inject services defined in a loader configuration file transparently, so clients don't have to know the details of the ServiceLoader. Something like this perhaps:

public class ServiceLoaderProxyFactory implements FactoryBean {

private Class serviceType;
private List serviceList;

public synchronized Object getObject() throws Exception {
    if (serviceList == null) {
        loadServices();
    }
    return serviceList;
}

@SuppressWarnings("unchecked")
private void loadServices() {
    serviceList = new ArrayList();
    ServiceLoader<?> loader = ServiceLoader.load(serviceType);
    for (Object service : loader) {
        serviceList.add(service);
    }
}

public Class getObjectType() {
    // TODO Auto-generated method stub
    return List.class;
}

public boolean isSingleton() {
    // TODO Auto-generated method stub
    return true;
}

public void setServiceType(final Class serviceType) {
    this.serviceType = serviceType;
}

}

Which could then obviously be used like this:

\<?xml version="1.0" encoding="UTF-8"?>

[The interface being implemented by the services] Thanks! BenTels --- No further details from [SPR-3269](https://jira.spring.io/browse/SPR-3269?redirect=false)
spring-projects-issues commented 17 years ago

Juergen Hoeller commented

I'll schedule this for Spring 2.1, where we plan to have general JDK 1.6 support (including JDBC 4.0, the JSR-223 scripting API, etc).

Juergen

spring-projects-issues commented 17 years ago

Ben Tels commented

Thanks!

BenTels

spring-projects-issues commented 17 years ago

Juergen Hoeller commented

Thanks for the suggestion! I've added corresponding ServiceLoaderFactoryBean (exposing the java.util.ServiceLoader itself), ServiceListFactoryBean (exposing a List of service objects) and ServiceFactoryBean (exposing the 'primary' service object) classes to the new "org.springframework.beans.factory.serviceloader" package. This will be released in 2.1 M2, and should be available in the next nightly snapshots already.

Juergen