spring的自动注入功能非常强大,近年来,使用注解的方式更是让人喜悦,大大简化了代码,同时,配合
- <context:annotation-config/>
- <context:component-scan base-package="com.liu.mongodb.dao"/>
可以方便的进行自动注入!
使用注解时,需要
- <context:component-scan base-package="com.liu.mongodb.dao"/>
将需要注入的bean们概括,同时,在类前加@Service、@Controller、@Component等等,其他方法就可以使用作为spring容器类的bean的形式使用它,非常方便
值得注意的是,诸如在spring xml文件中配置的mongoTemplate容器,似乎不能非常优雅的在自己写的bean中自动注入,比如笔者使用了set注入,不能成功
- // @Autowired
- // public void setMongoTemplate(@Qualifier("mongoTemplate")MongoTemplate mongoTemplate) {
- // this.mongoTemplate = mongoTemplate;
- // }
报的异常是
- 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包没有某类没有某个方法之类的错误。
使用成员变量注入却轻松的成功了
- @Autowired
- private MongoTemplate mongoTemplate;
以下是主要文件
Dao文件
- 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>