Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NamedParameterJdbcDaoSupport datasource autowire?

Tags:

java

spring

I have a class extends NamedParameterJdbcDaoSupport. well this superclass has a final setDataSource method on it. How can I wire datasource to it using annotation @autowire?

like image 430
Felix Avatar asked Mar 24 '12 14:03

Felix


2 Answers

You can use @Autowired on a constructor or on a method with another name. Personally I prefer the latter option.

@Autowired 
public MyClass(DataSource dataSource) {
    super();
    setDataSource(dataSource);
}

@Autowired 
public void setDs(DataSource dataSource) {
    setDataSource(dataSource);
}
like image 95
sinuhepop Avatar answered Oct 11 '22 22:10

sinuhepop


Another option is this:

    @Autowired
    private DataSource ds;

    @Override
    protected void initDao() throws Exception {
        super.initDao();
        setDataSource(ds);
    }
like image 40
Udo Avatar answered Oct 11 '22 20:10

Udo