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