Objective:
Instead, use the spring Framework's powerful annotation feature (@Repository annotations) to automate the function of registering DAO's beans (originally configured in XML). After the change, run a direct error:
Error creating Bean with Name ' Uservodao ' defined in file [D:\JSP_MyEclipse\.metadata\.me_tcat7\webapps\XYG_WEBDEV_ Sample\web-inf\classes\com\xinyiglass\springsample\dao\impl\uservodaoimpl. class]: Invocation of Init method failed; Nested exception is java.lang.IllegalArgumentException: ' DataSource ' or ' jdbctemplate ' is required
Then it's about how to solve the problem. Online to find some information, combined with their own understanding, the cause of the error is: After the modification, XML configuration of the DataSource can not be injected into the spring container active into the DAO! Thus, when the DAO is initialized (this DAO has inherited the Jdbcdaosupport class), if it does not get datasource or JdbcTemplate, it will error: The data source cannot be found.
Note: Before you modify it, you are actively injecting it with an XML configuration:
class abstract= "true" ><property name= "DataSource" ref= "DataSource"/></bean>
I found a few articles on the Internet, well written:
Conversion of Jdbcdaosupport and spring annotations http://www.verydemo.com/demo_c143_i2948.html
The datasource injection problem of Jdbcdaosupport in spring http://www.tuicool.com/articles/JZnqIb
@Repository can not directly use the analytic http://splinter-2.iteye.com/blog/1069765
Using an example of @repository: http://blog.csdn.net/show_me_the_world/article/details/47055841
Finally, you have to find a solution in a foreign site!! Baidu for 1 hours, in the domestic blog looked for a time and can not find the solution I want.
http://www.mkyong.com/spring/how-to-autowire-datasource-in-jdbcdaosupport/
Http://stackoverflow.com/questions/10570521/spring3-annotation-jdbcdaosupport
Note: Search keywords: jdbcdaosupport @Repository
Attach the Final Solution:
Method 1: The most concise, with the method of overriding the class constructor to automatically assemble the Daosupport need to use the data source!
@Autowired Uservodaoimpl (DataSource DataSource) { setdatasource (DataSource); }
Method 2: Also similar, when initializing the data source automatically assembled.
@Autowired private DataSource DataSource; @PostConstruct privatevoid Initialize () { setdatasource (dataSource); }
Resolve the Spring Framework's DAO layer to use @repository annotations instead of the jdbcdaosupport problem