1. Through the Java based bean metadata
@Configuration Public class AppConfig {publicthrows unknownhostexceptioin {returnnew Mongo ("localhost");}}
The above method contains exception handling, which is not what we want.
Therefore, should try to use the following way Mongofactorybean, or later XML way.
@Configuration Public class AppConfig {publicnew Mongofactorybean (); Mongo.sethost ("localhost" ); return MONGO;}}
2. Via XML based metadata
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mongo= "Http://www.springframework.org/schema/data/mongo"xsi:schemalocation= "Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/sprin G-context-3.0.xsd Http://www.springframework.org/schema/data/mongo Http://www.springframework.org/schema /data/mongo/spring-mongo-1.0.xsd Http://www.springframework.org/schema/beans http://www.springframework. Org/schema/beans/spring-beans-3.0.xsd "> <!--Default Bean name is ' MONGO ' - <Mongo:mongoHost= "localhost"Port= "27017"/></Beans>
A more detailed configuration with mongooptions can be as follows, but the value is not a recommended value.
<Beans> <Mongo:mongoHost= "localhost"Port= "27017"> <mongo:optionsConnections-per-host= "8"Threads-allowed-to-block-for-connection-multiplier= "4"Connect-timeout= "+"Max-wait-time= "the"Auto-connect-retry= "true"socket-keep-alive= "true"Socket-timeout= " the"Slave-ok= "true"Write-number= "1"Write-timeout= "0"Write-fsync= "true"/> </mongo:mongo/></Beans>
Replica configuration
<id= "Replicasetmongo" replica-set= "127.0.0.1:27017,localhost : 27018 "/>