Exception in spring + ibatis + resin Integration

Source: Internet
Author: User

1. An exception is reported during spring + ibatis integration: No SqlMapClient specified

Extension: similar errors will be reported during spring + hibernate integration.

Exception: java. lang. IllegalArgumentException: No SqlMapClient specified

Cause: newxxxDaoImpl () or new xxxServiceImpl () is used when creating DAO or service objects. The main reason is that new objects cannot be used when using spring containers.

Solution:

(1). Use the dependency injection method, namely set injection. Note that the object name in the configuration file must be the same as the set injection name. For example:

Private staticWikiSentMsgDAOWmd;

PublicWikiSentMsgDAOgetWmd (){

ReturnWmd;

}

Public voidSetWmd (WikiSentMsgDAO wmd ){

This.Wmd= Wmd;

}

In the Xml configuration file:

(2) The context object should be used to obtain the dao or service object. Example:

Practice 1:

WebApplicationContext beanFactory = WebApplicationContextUtils. getRequiredWebApplicationContext (SC );

ItemCheckTaskDao itemCheckTaskDao = (ItemCheckTaskDao) beanFactory. getBean ("itemCheckTaskDao ");

Practice 2:

HelloDao helloDao2 = (HelloDao) context. getBean ("helloDaoImpl", HelloDaoImpl.Class);

HelloDaoImpl helloDaoImpl2 = (HelloDaoImpl) context. getBean ("helloDaoImpl ");

Practice 3:

It is no problem to use the above two methods for testing the General wen project, but for the resin server, and the data source is configured in the resin configuration file through the jndi, at this time, you need to use unit testing, and use the spring annotation method to load the configuration file, you also need to configure an app under the META-INF folder. the properties File Stores data source information. The procedure is as follows:

Test class packagecom. hudong. postmessage. dao. test; importorg. junit. test; importorg. springframework. beans. factory. annotation. autowired; importorg. springframework. beans. factory. annotation. qualifier; importorg. springframework. test. context. contextConfiguration; importorg. springframework. test. context. junit4.AbstractJUnit4SpringContextTests; importcom. hudong. postmessage. dao. wikiSentMsgDAO; importcom. hudong. postmessage. dao. data. dependencies; @ ContextConfiguration (locations = {"classpath:/bean/test-postmsg-dao-env-bean.xml"}) publicclass TestPostsendSMs extends extensions {@ Autowired @ Qualifier ("wikiSendSmsDAO") private WikiSentMsgDAO wikiSendSmsDAO; public WikiSentMsgDAO getWikiSendSmsDAO () {return wikiSendSmsDAO;} public voidsetWikiSendSmsDAO (WikiSentMsgDAO wikiSendSmsDAO) {this. wikiSendSmsDAO = wikiSendSmsDAO;} @ Test public void test () {try {WikiSendSmsDO wikiSendSms = newWikiSendSmsDO (); wikiSendSms. setSmsMobile (Long. valueOf ("12345678911"); wikiSendSms. setSmsContent ("yk"); wikiSendSms. setSmsKey ("10"); wikiSendSms. setSoftwareSerialNo ("100012"); wikiSendSms. setSmsState (0); wikiSendSms. setSmsPriority (1); wikiSendSmsDAO. saveSms (wikiSendSms);} catch (Exception e) {e. printStackTrace () ;}} Spring configuration file
 
        
         
        
        
             
              
               
          
         
              
                 
               
                      
       
        
       
         Classpath:/ibatis/postmsg-sqlmap.xml
        
                            
        
        
         
          
         
        
       
      
     
    
   
  
 App. properties file apps. mysql. jdbc. driverClassName = com. mysql. jdbc. Driverapps. mysql. jdbc. url = jdbc: mysql: // 172.16.1.201: 3306/apps? UseUnicode = true & characterEncoding = UTF-8apps.mysql.jdbc.username = wiki_write0000.mysql.jdbc.password = wiki_write


2. Error: Caused by: org. xml. sax. SAXParseException: Attribute "resultMap" must be declared for element type "insert ".

Error cause: Write one more resultMap attribute for the add, delete, modify, and query fields in the configuration file of ibatis:

Solution: Remove resultMap "=" RM-SAVE ";

Final Result:

Note:One SQL configuration file of ibatis can have multiple resultMap attributes. That is to say, the result set configuration of multiple beans can be in the same SQL configuration file, without special differences, you only need to specify which method to use when inserting a call.

3. the test shows that two objects of the same bean cannot point to the same bean entity.

Error exception:

Caused by: org. springframework. beans. factory. noSuchBeanDefinitionException: No unique bean of type [com. hudong. postmessage. dao. wikiSentMsgDAO] is defined: expected single matching bean but found 2: [wikiSentMsgDAO, wikiSendSmsDAO]

Error cause:When @ Autowired label is used in Spring framework, @ Autowired annotation is used by default for automatic injection, there must be only one matching candidate Bean in Spring container. If no matching Bean is found, the Spring container will throw a BeanCreationException and point out that at least one matching Bean must exist.

Spring allows us to specify the name of the injected Bean through @ Qualifier annotation, so that ambiguity is eliminated. The following method can be used to solve the exception.

Through the interpretation of @ Autowired above, it is not difficult to find that if the number of your candidate beans is greater than 1 or less than 1, The BeanCreationException will be thrown. The above error exception is the detailed stack track of the exception.

Solution error: UseQualifier annotation method, specifying that the bean object can only be used in one place, and another object in other places. The specific solution is as follows:

@ Autowired

@ Qualifier ("wikiSendSmsDAO ")

Private WikiSendSmsDAO wikiSendSmsDAO;

@ Autowired

@ Qualifier ("wikiSentMsgDAO ")

Private WikiSendSmsDAO wikiSentMsgDAO

Reprinted please mark: http://blog.csdn.net/tianyazaiheruan

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.