我对Spring Framework还是很陌生,并且在设置我当前正在研究的项目时遇到了一些麻烦。我需要能够连接到两个不同的数据库,一个是MongoDB,另一个是MSSQL。我正在使用JPA连接到MSSQL。
我遇到的问题是,当我希望它对MSSQL进行调用时,它似乎正在尝试对Mongo数据库进行调用,而我不确定如何告诉它要读取的内容。我看到过一些建议使用@Qualifier
批注将其定向到正确实现的帖子,但我认为这对我的情况不起作用。
@RestController
@RequestMapping("/software")
public class SoftwareEndpoint {
@Autowired
SoftwareRepository repo;
/**********************************************************************************
********************************MSSQL calls****************************************
***********************************************************************************/
@RequestMapping(value="/all",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON)
String getAllSoftware(){
System.out.println("Here1");
List<Software> allSoftware = (List<Software>) repo.findAll();
System.out.println("Here2");
//rest of method and class
上面显示了我的控制器类的代码片段,其中包含我的SoftwareRepository的一个实例。在db调用之前和之后,我还打印到out流。
输出流仅显示“ Here1”,然后继续打印以下行:
2016-10-04 07:35:39.810 INFO 4236 --- [nio-8080-exec-2] org.mongodb.driver.cluster : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]}. Waiting for 30000 ms before timing out
然后在超时时引发异常。
我没有在本地运行的mongo实例,但是会在其中部署应用程序,但是我不认为这是问题所在,因为在到达该端点时,它不应调用Mongo数据库,应该尝试与MSSQL进行联系。
TLDR:如何指定用于特定存储库或数据库调用的Spring数据库实现?
最佳答案
您可以根据上下文中的配置在Spring中连接到不同的数据库。
以下代码用于连接到MySql和Mongo DB。如果您拥有JDBC,则可以用MSSQL代替MySql。检查http://jdbforms.sourceforge.net/UsersGuide/html/ch20s02.html以了解JDBC连接的属性的含义。
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="mySqldataSource" /> <!-- Change the datasource to MSSQL-->
</bean>
<bean id="mySqldataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>30</value>
</property>
<property name="driverClassName">
<value>MSSQL_DRIVER_CLASS_NAME</value>
</property>
<property name="url">
<value>MSSQL_DATABASE_URL</value>
</property>
<property name="username">
<value>MSSQL_DB_USER_NAME</value>
</property>
<property name="password">
<value>MSSQL_DB_PASSWORD</value>
</property>
<property name="maxIdle">
<value>10</value>
</property>
<property name="maxActive">
<value>10</value>
</property>
<property name="maxWait">
<value>100000</value>
</property>
<property name="testOnBorrow">
<value>false</value>
</property>
<property name="testWhileIdle">
<value>false</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>60000</value>
</property>
<property name="minEvictableIdleTimeMillis">
<value>60000</value>
</property>
<property name="numTestsPerEvictionRun">
<value>1</value>
</property>
<property name="defaultTransactionIsolation" value="1" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="1" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"></bean>
下面是用于连接mongodb
<mongo:db-factory dbname="mongoDbName" host="mongoServer" port="mongoPort"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<mongo:repositories base-package="com.test.repoPackage"/> <!-- Package containing the mongo repository interfaces -->
现在,您可以使用spring提供的存储库。
编辑1:假设config的名称是springConfig.properties。在上面的示例中,针对mongo:db-factory中的属性dbname,host和port,您需要在springConfig.properties中配置这些值。因此,让他们在下面命名:
mongoServer = xxx.xx.xxx.xxx
mongoPort = 27017
mongoDb = testDb
现在,需要修改上下文文件以导入springConfig.properties。这是在上下文文件中按以下方式完成的:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations" >
<list>
<value>classpath:/log4j.properties</value>
<value>classpath:/springConfig.properties</value>
</list>
</property>
</bean>
豆mongo:db-factory现在看起来像:
<mongo:db-factory dbname="${mongoDb}" host="${mongoServer}" port="${mongoPort}"/>
请注意,配置中的“键”(dbname,主机和端口)用insde $ {}表示。这将替换为config中的键值。