7

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?

Felix
  • 1,253
  • 7
  • 22
  • 41

2 Answers2

6

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);
}
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • Another solution is to inject template (which is thread-safe!!) instead of configuring `DaoSupport`: http://stackoverflow.com/a/21992433/173149 – gavenkoa Feb 24 '14 at 16:00
  • Should start using \@Inject henceforth, rather than \@Autowired annotation. – Blessed Geek Dec 23 '16 at 16:32
1

Another option is this:

    @Autowired
    private DataSource ds;

    @Override
    protected void initDao() throws Exception {
        super.initDao();
        setDataSource(ds);
    }
Udo
  • 2,300
  • 1
  • 23
  • 27