Resolve the Spring Framework's DAO layer to use @repository annotations instead of the jdbcdaosupport problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.