提交 1ee5dae2 编写于 作者: 武汉红喜's avatar 武汉红喜

whatsmars-spring-data

上级 864dbe7c
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>whatsmars-parent</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S7</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-spring-data</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.hongxi</groupId>
<artifactId>whatsmars-common</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.spring.data;
import org.hongxi.whatsmars.common.profile.ProfileUtils;
import org.hongxi.whatsmars.spring.data.config.PropertyConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(PropertyConfiguration.class);
context.scan("org.hongxi.whatsmars.spring.data");
context.getEnvironment().setActiveProfiles(ProfileUtils.getProfile());
context.refresh();
}
}
package org.hongxi.whatsmars.spring.data.config;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import java.util.Properties;
@Configuration
public class EsConfiguration {
@Value("${es.cluster.name}")
private String clusterName;
@Value("${es.cluster.nodes}")
private String clusterNodes;
@Bean
public TransportClient transportClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterNodes(clusterNodes);
factory.setProperties(createProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
private Properties createProperties() {
Properties properties = new Properties();
properties.put("cluster.name", clusterName);
return properties;
}
}
package org.hongxi.whatsmars.spring.data.config;
import org.hongxi.whatsmars.common.profile.ProfileUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class PropertyConfiguration {
@Bean
public PropertyPlaceholderConfigurer configurer() {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
String path = String.format("application-%s.properties", ProfileUtils.getProfile());
configurer.setLocation(new ClassPathResource(path));
return configurer;
}
}
package org.hongxi.whatsmars.spring.data.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
public class RedisConfiguration {
@Value("${redis.host}")
private String hostName;
@Value("${redis.port}")
private int port;
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
return redisTemplate;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName(hostName);
connectionFactory.setPort(port);
return connectionFactory;
}
}
package org.hongxi.whatsmars.spring.data.service;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class DataService {
@Resource
private RedisTemplate<Integer, String> redisTemplate;
@Resource
private TransportClient transportClient;
}
redis.host=127.0.0.1
redis.port=6379
es.cluster.name=elasticsearch
es.cluster.nodes=127.0.0.1:9300
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册