spring的成功配置樣式

spring的自動注入功能很是強大,近年來,使用註解的方式更是讓人喜悅,大大簡化了代碼,同時,配合 java

 

 
  1. <context:annotation-config/> 
  2. <context:component-scan base-package="com.liu.mongodb.dao"/> 

能夠方便的進行自動注入!spring

使用註解時,須要mongodb

 
  1. <context:component-scan base-package="com.liu.mongodb.dao"/> 

將須要注入的bean們歸納,同時,在類前加@Service、@Controller、@Component等等,其餘方法就能夠使用做爲spring容器類的bean的形式使用它,很是方便this

值得注意的是,諸如在spring xml文件中配置的mongoTemplate容器,彷佛不能很是優雅的在本身寫的bean中自動注入,好比筆者使用了set注入,不能成功spa

 

 
  1. //   @Autowired 
  2. //    public void setMongoTemplate(@Qualifier("mongoTemplate")MongoTemplate mongoTemplate) { 
  3. //        this.mongoTemplate = mongoTemplate; 
  4. //    } 

報的異常是code

 
  1. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.liu.mongodb.dao.SoulDao.setMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate); nested exception is java.lang.NoSuchMethodError: org.springframework.core.MethodParameter.getNestedParameterType()Ljava/lang/Class; 

不太清楚緣由,網上查了資料,發現是springcore版本和其餘spring包不一致,這裏提醒你們,要保證springcore的版本大於其餘spring包,否則就會出現諸如 -報core包沒有某類沒有某個方法之類的錯誤。component

使用成員變量注入卻輕鬆的成功了   xml

 

 
  1. @Autowired 
  2. private MongoTemplate mongoTemplate; 

 

如下是主要文件blog

Dao文件get

 
  1. import com.liu.mongodb.pojo.Soul; 
  2. import org.springframework.beans.factory.annotation.Autowired; 
  3. import org.springframework.beans.factory.annotation.Qualifier; 
  4. import org.springframework.data.mongodb.core.MongoTemplate; 
  5. import org.springframework.stereotype.Service; 
  6.  
  7. import javax.annotation.PostConstruct; 
  8. import javax.annotation.PreDestroy; 
  9. import javax.annotation.Resource; 
  10.  
  11. /** 
  12. * Created by IntelliJ IDEA. 
  13. * User: liu 
  14. * Date: 12-8-6 
  15. * Time: 下午8:56 
  16. * To change this template use File | Settings | File Templates. 
  17. */ 
  18. @Service("soulDao"
  19. public class SoulDao { 
  20.     @Autowired 
  21.     private MongoTemplate mongoTemplate; 

  22.   /* @Autowired 
  23.     public void setMongoTemplate(@Qualifier("mongoTemplate")MongoTemplate mongoTemplate) { 
  24.         this.mongoTemplate = mongoTemplate; 
  25.     } */
  26.  
  27.     public void saveSoul(Soul soul){ 
  28.         mongoTemplate.save(soul,"soul"); 
  29.     } 

Spring:

 

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xmlns:util="http://www.springframework.org/schema/util" 
  5.        xmlns:context="http://www.springframework.org/schema/context" 
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  7.        http://www.springframework.org/schema/beans/spring-beans.xsd 
  8.        http://www.springframework.org/schema/util 
  9.        http://www.springframework.org/schema/util/spring-util-2.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
  10.     <!--<util:properties id="jdbcProps" location="classpath:prop/jdbc.properties"/>--> 
  11.     <util:properties id="mongodbProps" location="classpath:prop/mongodb.properties"/> 
  12.     <!--<util:properties id="crawlerProps" location="classpath:prop/crawler.properties"/>--> 
  13.  
  14.     <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 
  15.  
  16.     <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean"> 
  17.         <property name="host" value="#{mongodbProps['mongo_statistic.host1']}"/> 
  18.         <property name="port" value="#{mongodbProps['mongo_statistic.port1']}"/> 
  19.     </bean> 
  20.  
  21.     <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> 
  22.         <constructor-arg name="username" value="#{mongodbProps['mongo_statistic.username1']}"/> 
  23.         <constructor-arg name="password" value="#{mongodbProps['mongo_statistic.password1']}"/> 
  24.     </bean> 
  25.  
  26.     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
  27.         <constructor-arg name="mongo" ref="mongo"/> 
  28.         <constructor-arg name="databaseName" value="#{mongodbProps['mongo_statistic.dbname1']}"/> 
  29.         <constructor-arg name="userCredentials" ref="userCredentials"/> 
  30.     </bean> 
  31.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
  32.     <context:annotation-config/> 
  33.     <context:component-scan base-package="com.liu.mongodb.dao"/> 
  34. </beans> 

 

本文出自 「沐浴心情」 博客,請務必保留此出處http://lj3331.blog.51cto.com/5679179/956547

相關文章
相關標籤/搜索