spring的自動注入功能很是強大,近年來,使用註解的方式更是讓人喜悅,大大簡化了代碼,同時,配合 java
- <context:annotation-config/>
- <context:component-scan base-package="com.liu.mongodb.dao"/>
能夠方便的進行自動注入!spring
使用註解時,須要mongodb
- <context:component-scan base-package="com.liu.mongodb.dao"/>
將須要注入的bean們歸納,同時,在類前加@Service、@Controller、@Component等等,其餘方法就能夠使用做爲spring容器類的bean的形式使用它,很是方便this
值得注意的是,諸如在spring xml文件中配置的mongoTemplate容器,彷佛不能很是優雅的在本身寫的bean中自動注入,好比筆者使用了set注入,不能成功spa
- // @Autowired
- // public void setMongoTemplate(@Qualifier("mongoTemplate")MongoTemplate mongoTemplate) {
- // this.mongoTemplate = mongoTemplate;
- // }
報的異常是code
- 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
- @Autowired
- private MongoTemplate mongoTemplate;
如下是主要文件blog
Dao文件get
- import com.liu.mongodb.pojo.Soul;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import javax.annotation.Resource;
- /**
- * Created by IntelliJ IDEA.
- * User: liu
- * Date: 12-8-6
- * Time: 下午8:56
- * To change this template use File | Settings | File Templates.
- */
- @Service("soulDao")
- public class SoulDao {
- @Autowired
- private MongoTemplate mongoTemplate;
- /* @Autowired
- public void setMongoTemplate(@Qualifier("mongoTemplate")MongoTemplate mongoTemplate) {
- this.mongoTemplate = mongoTemplate;
- } */
- public void saveSoul(Soul soul){
- mongoTemplate.save(soul,"soul");
- }
- }
Spring:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/util
- 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">
- <!--<util:properties id="jdbcProps" location="classpath:prop/jdbc.properties"/>-->
- <util:properties id="mongodbProps" location="classpath:prop/mongodb.properties"/>
- <!--<util:properties id="crawlerProps" location="classpath:prop/crawler.properties"/>-->
- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
- <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
- <property name="host" value="#{mongodbProps['mongo_statistic.host1']}"/>
- <property name="port" value="#{mongodbProps['mongo_statistic.port1']}"/>
- </bean>
- <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
- <constructor-arg name="username" value="#{mongodbProps['mongo_statistic.username1']}"/>
- <constructor-arg name="password" value="#{mongodbProps['mongo_statistic.password1']}"/>
- </bean>
- <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
- <constructor-arg name="mongo" ref="mongo"/>
- <constructor-arg name="databaseName" value="#{mongodbProps['mongo_statistic.dbname1']}"/>
- <constructor-arg name="userCredentials" ref="userCredentials"/>
- </bean>
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- <context:annotation-config/>
- <context:component-scan base-package="com.liu.mongodb.dao"/>
- </beans>
本文出自 「沐浴心情」 博客,請務必保留此出處http://lj3331.blog.51cto.com/5679179/956547