Want to use spring-data-redis with standalone maven java project
e.g
package com.test.mvn.redis.test_redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Hello world!
*
*/
public class App
{
@Autowired
private StudentRepository studentRepository;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory
= new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(6379);
return jedisConFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
public void saveData()
{
Student student = new Student(
"Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.save(student);
System.out.println("Data saved successfully");
}
public static void main( String[] args )
{
System.out.println( "Hello World!" );
App app =new App();
app.saveData();
}
}
package com.test.mvn.redis.test_redis;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends CrudRepository<Student, String> {
}
My pom.xml 4.0.0
<groupId>com.test.mvn.redis</groupId>
<artifactId>test-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-redis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- source code support dependencies -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Please let me know is it correct approach or do i need to use different redis java library for standalone java project. Want to add data from java to redis without rest controller.Basic requirement is to add data from java plain (maven) project to redis.
CodePudding user response:
You already use @Autowired
, @Bean
and some more, so you definitely need spring-core
artifact.
But for those type of examples like what you mention in your question (a simple command line java program with just a main
method), spring has created another approach.
Read this article on how to creat a simple command line program with features of spring available.
Important to note. The use of property spring.main.web-application-type=NONE
is a must, so that spring does not start any embedded web server like tomcat when the jar file is executed.