3

I can configure Spring beans with primitive values such as String/int/long/etc. How do I get the following to work:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">

    <bean class="springtest.SomeBeanTest">
        <constructor-arg index="0" type="java.nio.file.Path" value="/Users/admin/test.txt"/>
    </bean>
</beans>

UPDATE: the following works. The empty list is required for the second varargs parameter.

<bean class="springtest.SomeBeanPath">
    <constructor-arg index="0">
        <bean class="java.nio.file.Paths" factory-method="get">
            <constructor-arg index="0" value="/Users/admin/test.txt" />
            <constructor-arg index="1"><list></list></constructor-arg>
        </bean>
    </constructor-arg>
</bean>
clay
  • 18,138
  • 28
  • 107
  • 192

2 Answers2

4

The bean definition XML syntax can instanciate virtually any class.

You can look at the Spring documentation for a complete reference here: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-class, out of which I extracted the following samples.

For example, you can instanciate any class with an empty constructor this way:

<bean id="exampleBean" class="examples.ExampleBean"/>

If the constructor is a static method, you can call it this way

<bean id="clientService"
class="examples.ClientService" factory-method="createInstance"/>

If you need to call a constructor with an argument, look at: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#beans-constructor-injection

<bean id="foo" class="x.y.Foo">
    <constructor-arg ref="bar"/>
    <constructor-arg ref="baz"/>
</bean>

Where bar and baz should be ids of other spring beans. If instead, you use the XML attribute value, then you can use a primitive type directly. For example :

<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>

You can also opt for calling an empty constructor first and the call setters on your beans (or mix the two methods).

<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property>

<!-- setter injection using the neater ref attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>

There are very advanced configuration options, allowing you to override the way Spring interprets the XML to interpret ref/value attributes. There is also a dedicated syntax for Maps, Lists, ... An expression language called Spring-EL in that you can use for dynamic evaluation at runtime inside your XML. Plus various pre/post processor options for more advanced scenarios (BeanFactoryAware, BeanFactoryPostProcessor, ...)

So, in your case, you could try to decompose like this :

<bean class="springtest.SomeBeanTest">
    <constructor-arg index="0">
        <bean class="java.nio.file.Paths" factory-method="get">
           <constructor-arg index="0" value="your path" />
           <!-- Edit: see @clay's edit to the question, the following is necessary too -->
           <constructor-arg index="1"><list></list></constructor-arg>
        </bean>
    </constructor-arg>
</bean>

Note that when there is no ambiguity, the index attribute is not necessary.

GPI
  • 9,088
  • 2
  • 31
  • 38
  • The "get" method is on j.n.f.Paths. The resulting type is j.n.f.Path with no trailing s. The example you give does not work. – clay Jul 21 '14 at 22:56
  • Unfortunately, I have no Java7 to test it right now, and I do not know what your class excepts as its argument, but I am confident that the sample code is close to what you expect. The fact that I use the `Paths` (trailing s) class if because the static method `get` is on this class. This static method in turn produces a `Path` instance which I (can only) guess is the type of argument that your are expecting for your constructor... What kind of exception / error do you get ? – GPI Jul 21 '14 at 23:19
  • thanks. I finally got it. I needed to add a second empty list for the vararg parameter. – clay Jul 21 '14 at 23:24
0

Pass String from configuration file and convert String into Path in SomeBeanTest class.

You can just use the Paths class:

public class SomeBeanTest(){
     public SomeBeanTest(String textPath){
         Path path = Paths.get(textPath); // String to Path
     }
}

XML:

<bean class="springtest.SomeBeanTest">
    <constructor-arg value="/Users/admin/test.txt"/>
</bean>

Note: Assuming Users/admin/test.txt is directly accessed form class-path.

Read more Create a Path from String in Java7

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Yes, but this would require that I change the underlying class to use String and not a typed Path... – clay Jul 21 '14 at 22:48