0

I am new to Spring framework. Maybe this is more of a Java EE bean design question than related to Spring framework. Anyway, I just shoot it and see how clear I can make myself.

So I have a service. The service takes a connection string as constructor parameter. Then you can use the service to upload files to the location indicated by the connection string.

So you will start with something like:

public class MyService{
    public MyService(String connectionStr){ ... }
}

When you need such a service, you call:

MyService service = new MyService("xxx");
...

That's what I used to do. Nothing fancy. Now if I do it in Java under Spring, I somehow want the service to be a bean. I need to do this:

@Component
public class MyService{
    @Autowired
    public MyService(@Value(...some connection string...) String connectionStr) {...}
}

But I get confused how you can inject dependency in compile time? I never know what connection string I will pass to create the service. When I read Spring tutorials, most of them have parameters coded in XML config file. Can I design a Spring bean like the one above but require the parameters to be passed in runtime?

Thanks.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Steve
  • 4,935
  • 11
  • 56
  • 83
  • Similar question.This could help you to understand more.http://stackoverflow.com/questions/317687/inject-property-value-into-spring-bean – SRy Dec 15 '12 at 07:16
  • Sorry I think that post isn't a similar question. What I try to ask is what if my bean isn't a singleton and is a prototype whose parameters are inputed in runtime, say, by a user input from a textbox control. Then I cannot configure it in XML because the time I am writing the code, I have no idea what connection string my user is going to type in the textbox. – Steve Dec 15 '12 at 23:15

2 Answers2

2

You can design a method like this:

void upload(String location,XXX other parameters);
BlackJoker
  • 3,099
  • 2
  • 20
  • 27
  • Since the Service class will take the connection string, setup things, in this design the Service will have nothing. Then I should really define static void upload() and let the method create a Service using the location param inside. This then becomes a funny thing, doesn't it? – Steve Dec 15 '12 at 16:44
  • Spring is a container,that means you do not need to new XXXService(),Spring's @Service will help you for that. – BlackJoker Dec 17 '12 at 11:26
  • If this service need other information to work correctly,You can inject them by setter method configured in spring's xml file,the injection following java bean spec. If some information must be set in runtime,they can be pass in through method parameters,either one by one or encapsulated in a Value Object. – BlackJoker Dec 17 '12 at 11:33
0

I didnt really get your question but will try to answer.

Check here or google to check if you really want to go for spring. Coming to your query, For your service you would have to define some thing like this in your spring context.

<bean id="myService" class="com.blah.MyService">
    <constructor-arg>
        <value>http://HOST/test/</value>
    </constructor-arg>
</bean>

Your service class will be,

public class MyService{

    public MyService(String connectionString) {...}
}

This is how you will call your service in your application

ApplicationContext context = new ClassPathXmlApplicationContext(
                                new String[] { "context.xml" });
                MyService service = (MyService) context
                                .getBean("myService");

The above can be implemented using annotations also. Check here for more details

Community
  • 1
  • 1
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • That is correct if the host connection string is fixed before deployment and remained unchanged. But what if the host connection is unknown beforehand and will be passed in in runtime? What if there can be multiple connection services co-existing at the same time? Normally I just create multiple instances using different hosts. How to do in Spring? – Steve Dec 15 '12 at 16:41