is there a mean to specify a URL property directly to inject with XML definition file in Spring DI?

31 Views Asked by At

I try to instanciate a bean which has a dependency with another bean with Spring. I have seen examples where the property can be specified in xml tags. With standard ype int, String this works but there is a field with URL type and this does not work. Now I tried to face this with a String new File(name).toURI().toURL() conversion but withoutsucess

code:

@Configuration public class RemoteServiceConfiguration {

//@Value("jdbc:mysql://localhost/eltechdb")
private URL url;

///@Value("1000")
private int connectionTimeout;

public URL getUrl() {
    return url;
}

public int getConnectionTimeout() {
    return connectionTimeout;
}

public RemoteServiceConfiguration(URL url, int connectionTimeout) {
    this.url = url;
    this.connectionTimeout = connectionTimeout;
}

}

@Service

public class RemoteService {

@Value("#{remoteServiceConfiguration.url}")
private URL url;
@Value("#{remoteServiceConfiguration.connectionTimeout}")
private int connectionTimeout;

@PostConstruct
public void connect() throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.setConnectTimeout(connectionTimeout);
}

public void init() {
    System.out.println("RemoteService created with url "+url);
}

}

public class RemoteDemo {

public static void main(String args[]) {
   try {
    ApplicationContext ac = new ClassPathXmlApplicationContext( new String[] {"bean_config.xml"} );
    
    RemoteServiceConfiguration rmcf = (RemoteServiceConfiguration) ac.getBean("RemoteServiceConfiguration");
    
    System.out.println("Remote Config URL is "+rmcf.getUrl());
    
   } catch (Exception e) {
       System.out.println("Error dependency Inject from config_demo.xml");
       e.printStackTrace();
   }
}

}

 <bean id="remoteServiceConfiguration" class="dev.gayerie.RemoteServiceConfiguration">
 <constructor-arg name="url" value="jdbc:mysql://localhost/eltechdb"></constructor-arg>
 <constructor-arg name="connectionTimeout" value="1000"></constructor-arg>
 </bean>
     
 <bean id="remoteService" class="dev.gayerie.RemoteService" init-method="init">
 <property name="url" value="#{remoteServiceConfiguration.url}"/>
 <property name="connectionTimeout" value="#{remoteServiceConfiguration.connectionTimeout}"/>
 </bean>
AVERTISSEMENT: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'remoteServiceConfiguration' defined in class path resource [bean_config.xml]: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [java.net.URL]: Failed to convert value of type 'java.lang.String' to required type 'java.net.URL'; nested exception is java.lang.IllegalArgumentException: Could not retrieve URL for class path resource [jdbc:mysql://localhost/eltechdb]: class path resource [jdbc:mysql://localhost/eltechdb] cannot be resolved to URL because it does not exist

Error dependency Inject from config_demo.xml org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'remoteServiceConfiguration' defined in class path resource [bean_config.xml]: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [java.net.URL]: Failed to convert value of type 'java.lang.String' to required type 'java.net.URL'; nested exception is java.lang.IllegalArgumentException: Could not retrieve URL for class path resource [jdbc:mysql://localhost/eltechdb]: class path resource [jdbc:mysql://localhost/eltechdb] cannot be resolved to URL because it does not exist at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:691) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1269) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1126) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:95) at dev.gayerie.RemoteDemo.main(RemoteDemo.java:10)

Is there a turn around or pertinent spring expression language to define my bean property of RemoteServiceConfiguration?

0

There are 0 best solutions below