Spring命名空间

Spring Data Elasticsearch模块包含一个自定义的命名空间,它允许我们定义repository bean和初始化一个ElasticsearchServer

下面,我们像创建Repository实例中描述的那样使用repositories元素查找Spring Data repository。

Example 32. Setting up Elasticsearch repositories using Namespace(使用命名空间创建Elasticsearch repositories)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/data/elasticsearch
  8. http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
  9. <elasticsearch:repositories base-package="com.acme.repositories" />
  10. </beans>

使用Transport ClientNode Client元素注册一个Elasticsearch Server实例。

Example 33. Transport Client using Namespace(使用Transport Client)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/data/elasticsearch
  8. http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
  9. <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />
  10. </beans>

Example 34. Node Client using Namespace(使用Node Client)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/data/elasticsearch
  8. http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
  9. <elasticsearch:node-client id="client" local="true"" />
  10. </beans>