Reputation: 23
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.
Upvotes: 1
Views: 3727
Reputation: 135992
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");
}
...
Upvotes: 4
Reputation: 2611
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).
Upvotes: 0