spring的成功配置样式
2015-12-20 00:00
281 查看
spring的自动注入功能非常强大,近年来,使用注解的方式更是让人喜悦,大大简化了代码,同时,配合
可以方便的进行自动注入!
使用注解时,需要
将需要注入的bean们概括,同时,在类前加@Service、@Controller、@Component等等,其他方法就可以使用作为spring容器类的bean的形式使用它,非常方便
值得注意的是,诸如在spring xml文件中配置的mongoTemplate容器,似乎不能非常优雅的在自己写的bean中自动注入,比如笔者使用了set注入,不能成功
报的异常是
不太清楚原因,网上查了资料,发现是springcore版本和其他spring包不一致,这里提醒大家,要保证springcore的版本大于其他spring包,不然就会出现诸如 -报core包没有某类没有某个方法之类的错误。
使用成员变量注入却轻松的成功了
以下是主要文件
Dao文件
Spring:
本文出自 “沐浴心情” 博客,请务必保留此出处http://lj3331.blog.51cto.com/5679179/956547
<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>
本文出自 “沐浴心情” 博客,请务必保留此出处http://lj3331.blog.51cto.com/5679179/956547
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- java教程之java注解annotation使用方法
- spring boot实战之内嵌容器tomcat配置
- 浅析Java的Spring框架中IOC容器容器的应用