1

I am trying to define a bean and @Autowire org.springframework.jdbc.object.StoredProcedure which required 2 constructor. Is there a way I can pass constructor argument while wiring these beans ? Below is my code:

@Component("procedure")
public class ExecuteStoreProcedure extends AbstractImutableDAO{

    @Autowired
    private StoredProcedure procedure;

......
}

Here StoredProcedure have a constructor to pass jdbctemplate and procedure name, which is dynamic.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
user1917435
  • 23
  • 1
  • 5
  • In which way do you plan to pass jdbctemplate and procedure name? Why didn't you create specific beans in spring XML configuration? As hardcore solution you are able to inject configured FactoryBean(http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean). – Taky Jan 09 '13 at 12:07
  • I am looking to configure with with annotation rather than a xml file (not sure this option is available in Spring) also my procedure name is dynamic which will be a argument to a method in the class – user1917435 Jan 09 '13 at 12:12
  • 1
    So your question is how to add StoredProcedure into ApplicationContext? If StoredProcedure is third party class you are able to extend it and annotate new class with appropriate. – Taky Jan 09 '13 at 12:16

2 Answers2

4

Maybe I do not understand the question, but you don't need constructor params while wiring, you configire your bean (StoredProcedure) in context.xml

<bean id="proc1" class="org.springframework.jdbc.object.StoredProcedure">
    <constructor-arg name="ds" ref="ds" />
    <constructor-arg name="name" value="proc1" />
</bean>

Spring creates it with the given constructor args and injects the bean into your field

@Autowired
private StoredProcedure procedure;

If dont want to use xml it does not change the idea

@Configuration
@PropertySource("spring.properties")
@EnableTransactionManagement
public class Test3 {
    @Autowired 
    Environment env;  

    @Bean 
    public ExecuteStoreProcedure getExecuteStoreProcedure() {
        ...
    }

    @Bean 
    public DataSource getDataSource() {
       ...
    }

    @Bean 
    public StoredProcedure getStoredProcedure() {
        return new MyStoredProcedure(getDataSource(), "proc1");
    }
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • +1 this is correct but the name of the field should be "proc1" – Jerome Cance Jan 09 '13 at 12:29
  • Thanks for the response. But, I was looking to configure the same using annotation, also in my case the procedure name will be dynamic during run time, the invoker class can pass any procedure name and get it executed – user1917435 Jan 09 '13 at 12:46
  • @user1917435 I attached an example with AnnotationConfigApplicationContext, but 'procedure name will be dynamic during run time' is still not clear – Evgeniy Dorofeev Jan 09 '13 at 13:02
  • Thanks, this answer some of my question. The other confusion I have is do we require to create bean like @Bean public StoredProcedure getStoredProcedure() { return new MyStoredProcedure(getDataSource(), "proc1"); } can't we avoid the new operator ? – user1917435 Jan 10 '13 at 12:18
  • I dont think you need it, Spring knows that this is a singleton and will return an instance from cache – Evgeniy Dorofeev Jan 10 '13 at 12:24
0

When you @Autowire a field, you're assuming that a bean of the required type already exists in the ApplicationContext. So what you need to do in order to get this code to work is to declare a such a bean (either in XML or, if you want to configure it programmatically, using @Bean- see the Spring documentation here).

Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27