DATAREDIS-425 - Add JSR-310 support, CDI extension and Update reference documentation.
We ship converters for JSR-310 types (LocalDate/Time, ZonedDateTime, Period, Duration and ZoneId) to map between UTF-8-encoded byte[] and JDK 8 date/time types. We also export Redis Repositories in a CDI environment. Repositories can be injected using @Inject. The CDI extension requires at least RedisOperations to be provided. Other beans like RedisKeyValueAdapter and RedisKeyValueTemplate can be provided by the user. If no RedisKeyValueAdapter/RedisKeyValueTemplate beans are found, the CDI extension creates own managed instances. Original Pull Request: #156
This commit is contained in:
committed by
Christoph Strobl
parent
4362c58a8a
commit
92274a8450
@@ -15,6 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core.convert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -50,6 +58,15 @@ public class ConversionTestEntities {
|
||||
Boolean alive;
|
||||
Date birthdate;
|
||||
|
||||
LocalDate localDate;
|
||||
LocalDateTime localDateTime;
|
||||
LocalTime localTime;
|
||||
Instant instant;
|
||||
ZonedDateTime zonedDateTime;
|
||||
ZoneId zoneId;
|
||||
Duration duration;
|
||||
Period period;
|
||||
|
||||
Address address;
|
||||
|
||||
Map<String, String> physicalAttributes;
|
||||
|
||||
@@ -27,6 +27,14 @@ import static org.springframework.data.redis.test.util.IsBucketMatcher.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -182,8 +190,8 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
RedisData target = write(rand);
|
||||
|
||||
assertThat(target.getBucket(),
|
||||
isBucket().containingUtf8String("address.city", "two rivers").containingUtf8String("address.country", "andora"));
|
||||
assertThat(target.getBucket(), isBucket().containingUtf8String("address.city", "two rivers")
|
||||
.containingUtf8String("address.country", "andora"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,10 +215,11 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
RedisData target = write(rand);
|
||||
|
||||
assertThat(target.getBucket(), isBucket().containingUtf8String("coworkers.[0].firstname", "mat") //
|
||||
.containingUtf8String("coworkers.[0].nicknames.[0]", "prince of the ravens") //
|
||||
.containingUtf8String("coworkers.[1].firstname", "perrin") //
|
||||
.containingUtf8String("coworkers.[1].address.city", "two rivers"));
|
||||
assertThat(target.getBucket(),
|
||||
isBucket().containingUtf8String("coworkers.[0].firstname", "mat") //
|
||||
.containingUtf8String("coworkers.[0].nicknames.[0]", "prince of the ravens") //
|
||||
.containingUtf8String("coworkers.[1].firstname", "perrin") //
|
||||
.containingUtf8String("coworkers.[1].address.city", "two rivers"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -509,6 +518,191 @@ public class MappingRedisConverterUnitTests {
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("age", "20"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesLocalDateTimeValuesCorrectly() {
|
||||
|
||||
rand.localDateTime = LocalDateTime.parse("2016-02-19T10:18:01");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("localDateTime", "2016-02-19T10:18:01"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsLocalDateTimeValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("localDateTime", "2016-02-19T10:18:01"))));
|
||||
|
||||
assertThat(target.localDateTime, is(LocalDateTime.parse("2016-02-19T10:18:01")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesLocalDateValuesCorrectly() {
|
||||
|
||||
rand.localDate = LocalDate.parse("2016-02-19");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("localDate", "2016-02-19"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsLocalDateValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("localDate", "2016-02-19"))));
|
||||
|
||||
assertThat(target.localDate, is(LocalDate.parse("2016-02-19")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesLocalTimeValuesCorrectly() {
|
||||
|
||||
rand.localTime = LocalTime.parse("11:12:13");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("localTime", "11:12:13"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsLocalTimeValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("localTime", "11:12"))));
|
||||
|
||||
assertThat(target.localTime, is(LocalTime.parse("11:12:00")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesZonedDateTimeValuesCorrectly() {
|
||||
|
||||
rand.zonedDateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
|
||||
|
||||
assertThat(write(rand).getBucket(),
|
||||
isBucket().containingUtf8String("zonedDateTime", "2007-12-03T10:15:30+01:00[Europe/Paris]"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsZonedDateTimeValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class, new RedisData(Bucket
|
||||
.newBucketFromStringMap(Collections.singletonMap("zonedDateTime", "2007-12-03T10:15:30+01:00[Europe/Paris]"))));
|
||||
|
||||
assertThat(target.zonedDateTime, is(ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesInstantValuesCorrectly() {
|
||||
|
||||
rand.instant = Instant.parse("2007-12-03T10:15:30.01Z");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("instant", "2007-12-03T10:15:30.010Z"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsInstantValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("instant", "2007-12-03T10:15:30.01Z"))));
|
||||
|
||||
assertThat(target.instant, is(Instant.parse("2007-12-03T10:15:30.01Z")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesZoneIdValuesCorrectly() {
|
||||
|
||||
rand.zoneId = ZoneId.of("Europe/Paris");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("zoneId", "Europe/Paris"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsZoneIdValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("zoneId", "Europe/Paris"))));
|
||||
|
||||
assertThat(target.zoneId, is(ZoneId.of("Europe/Paris")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesDurationValuesCorrectly() {
|
||||
|
||||
rand.duration = Duration.parse("P2DT3H4M");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("duration", "PT51H4M"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsDurationValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("duration", "PT51H4M"))));
|
||||
|
||||
assertThat(target.duration, is(Duration.parse("P2DT3H4M")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void writesPeriodValuesCorrectly() {
|
||||
|
||||
rand.period = Period.parse("P1Y2M25D");
|
||||
|
||||
assertThat(write(rand).getBucket(), isBucket().containingUtf8String("period", "P1Y2M25D"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void readsPeriodValuesCorrectly() {
|
||||
|
||||
Person target = converter.read(Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("period", "P1Y2M25D"))));
|
||||
|
||||
assertThat(target.period, is(Period.parse("P1Y2M25D")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@@ -592,10 +786,8 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
Date date = cal.getTime();
|
||||
|
||||
Person target = converter.read(
|
||||
Person.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("birthdate", Long.valueOf(date.getTime())
|
||||
.toString()))));
|
||||
Person target = converter.read(Person.class, new RedisData(
|
||||
Bucket.newBucketFromStringMap(Collections.singletonMap("birthdate", Long.valueOf(date.getTime()).toString()))));
|
||||
|
||||
assertThat(target.birthdate, is(date));
|
||||
}
|
||||
@@ -614,9 +806,10 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
RedisData target = write(rand);
|
||||
|
||||
assertThat(target.getBucket(), isBucket().containingUtf8String("location", "locations:1") //
|
||||
.without("location.id") //
|
||||
.without("location.name"));
|
||||
assertThat(target.getBucket(),
|
||||
isBucket().containingUtf8String("location", "locations:1") //
|
||||
.without("location.id") //
|
||||
.without("location.name"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -661,9 +854,10 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
RedisData target = write(rand);
|
||||
|
||||
assertThat(target.getBucket(), isBucket().containingUtf8String("coworkers.[0].location", "locations:1") //
|
||||
.without("coworkers.[0].location.id") //
|
||||
.without("coworkers.[0].location.name"));
|
||||
assertThat(target.getBucket(),
|
||||
isBucket().containingUtf8String("coworkers.[0].location", "locations:1") //
|
||||
.without("coworkers.[0].location.id") //
|
||||
.without("coworkers.[0].location.name"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -713,9 +907,10 @@ public class MappingRedisConverterUnitTests {
|
||||
|
||||
RedisData target = write(rand);
|
||||
|
||||
assertThat(target.getBucket(), isBucket().containingUtf8String("visited.[0]", "locations:1") //
|
||||
.containingUtf8String("visited.[1]", "locations:2") //
|
||||
.containingUtf8String("visited.[2]", "locations:3"));
|
||||
assertThat(target.getBucket(),
|
||||
isBucket().containingUtf8String("visited.[0]", "locations:1") //
|
||||
.containingUtf8String("visited.[1]", "locations:2") //
|
||||
.containingUtf8String("visited.[2]", "locations:3"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -882,8 +1077,8 @@ public class MappingRedisConverterUnitTests {
|
||||
address.country = "andor";
|
||||
rand.address = address;
|
||||
|
||||
assertThat(write(rand).getIndexedData(), hasItem(new SimpleIndexedPropertyValue(KEYSPACE_PERSON, "address.country",
|
||||
"andor")));
|
||||
assertThat(write(rand).getIndexedData(),
|
||||
hasItem(new SimpleIndexedPropertyValue(KEYSPACE_PERSON, "address.country", "andor")));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1151,8 +1346,8 @@ public class MappingRedisConverterUnitTests {
|
||||
species.name = new String(source.get("species-name"), Charset.forName("UTF-8"));
|
||||
}
|
||||
if (source.containsKey("species-nicknames")) {
|
||||
species.alsoKnownAs = Arrays.asList(StringUtils.commaDelimitedListToStringArray(new String(source
|
||||
.get("species-nicknames"), Charset.forName("UTF-8"))));
|
||||
species.alsoKnownAs = Arrays.asList(StringUtils
|
||||
.commaDelimitedListToStringArray(new String(source.get("species-nicknames"), Charset.forName("UTF-8"))));
|
||||
}
|
||||
return species;
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public class RedisRepositoryIntegrationTests {
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void simpleFindSouldReturnEntitiesCorrectly() {
|
||||
public void simpleFindShouldReturnEntitiesCorrectly() {
|
||||
|
||||
Person rand = new Person();
|
||||
rand.firstname = "rand";
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.spi.Bean;
|
||||
|
||||
import org.apache.webbeans.cditest.CdiTestContainer;
|
||||
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Integration tests for Spring Data Redis CDI extension.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CdiExtensionIntegrationTests {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(CdiExtensionIntegrationTests.class);
|
||||
|
||||
static CdiTestContainer container;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
|
||||
container = CdiTestContainerLoader.getCdiContainer();
|
||||
container.bootContainer();
|
||||
|
||||
LOGGER.debug("CDI container bootstrapped!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void beanShouldBeRegistered() {
|
||||
|
||||
Set<Bean<?>> beans = container.getBeanManager().getBeans(PersonRepository.class);
|
||||
|
||||
assertThat(beans, hasSize(1));
|
||||
assertThat(beans.iterator().next().getScope(), is(equalTo((Class) ApplicationScoped.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void saveAndFindUnqualified() {
|
||||
|
||||
RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class);
|
||||
repositoryConsumer.deleteAll();
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("foo");
|
||||
repositoryConsumer.getUnqualifiedRepo().save(person);
|
||||
List<Person> result = repositoryConsumer.getUnqualifiedRepo().findByName("foo");
|
||||
|
||||
assertThat(result, contains(person));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void saveAndFindQualified() {
|
||||
|
||||
RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class);
|
||||
repositoryConsumer.deleteAll();
|
||||
|
||||
Person person = new Person();
|
||||
person.setName("foo");
|
||||
repositoryConsumer.getUnqualifiedRepo().save(person);
|
||||
List<Person> result = repositoryConsumer.getQualifiedRepo().findByName("foo");
|
||||
|
||||
assertThat(result, contains(person));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-425
|
||||
*/
|
||||
@Test
|
||||
public void callMethodOnCustomRepositoryShouldSuceed() {
|
||||
|
||||
RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class);
|
||||
|
||||
int result = repositoryConsumer.getUnqualifiedRepo().returnOne();
|
||||
assertThat(result, is(1));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.redis.core.RedisHash;
|
||||
import org.springframework.data.redis.core.index.Indexed;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RedisHash
|
||||
class Person {
|
||||
|
||||
@Id private String id;
|
||||
|
||||
@Indexed private String name;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Person))
|
||||
return false;
|
||||
|
||||
Person person = (Person) o;
|
||||
|
||||
if (id != null ? !id.equals(person.id) : person.id != null)
|
||||
return false;
|
||||
return name != null ? name.equals(person.name) : person.name == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id != null ? id.hashCode() : 0;
|
||||
result = 31 * result + (name != null ? name.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
|
||||
@interface PersonDB {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.cdi.Eager;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Eager
|
||||
public interface PersonRepository extends CrudRepository<Person, String>, PersonRepositoryCustom {
|
||||
|
||||
List<Person> findAll();
|
||||
|
||||
List<Person> findByName(String name);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
interface PersonRepositoryCustom {
|
||||
|
||||
int returnOne();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class PersonRepositoryImpl implements PersonRepositoryCustom {
|
||||
|
||||
@Override
|
||||
public int returnOne() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@PersonDB
|
||||
public interface QualifiedPersonRepository extends PersonRepository {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import javax.enterprise.inject.Disposes;
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisKeyValueAdapter;
|
||||
import org.springframework.data.redis.core.RedisKeyValueTemplate;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.mapping.RedisMappingContext;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class RedisCdiDependenciesProducer {
|
||||
|
||||
/**
|
||||
* Provides a producer method for {@link RedisConnectionFactory}.
|
||||
*/
|
||||
@Produces
|
||||
public RedisConnectionFactory redisConnectionFactory() {
|
||||
|
||||
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
|
||||
jedisConnectionFactory.setHostName(SettingsUtils.getHost());
|
||||
jedisConnectionFactory.setPort(SettingsUtils.getPort());
|
||||
jedisConnectionFactory.afterPropertiesSet();
|
||||
return jedisConnectionFactory;
|
||||
}
|
||||
|
||||
public void closeRedisConnectionFactory(@Disposes RedisConnectionFactory redisConnectionFactory) throws Exception {
|
||||
|
||||
if (redisConnectionFactory instanceof DisposableBean) {
|
||||
((DisposableBean) redisConnectionFactory).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a producer method for {@link RedisOperations}.
|
||||
*/
|
||||
@Produces
|
||||
public RedisOperations<byte[], byte[]> redisOperationsProducer(RedisConnectionFactory redisConnectionFactory) {
|
||||
|
||||
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
|
||||
template.setConnectionFactory(redisConnectionFactory);
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
|
||||
// shortcut for managed KeyValueAdapter/Template.
|
||||
@Produces
|
||||
@PersonDB
|
||||
public RedisOperations<byte[], byte[]> redisOperationsProducerQualified(RedisOperations<byte[], byte[]> instance) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void closeRedisOperations(@Disposes RedisOperations<byte[], byte[]> redisOperations) throws Exception {
|
||||
|
||||
if (redisOperations instanceof DisposableBean) {
|
||||
((DisposableBean) redisOperations).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a producer method for {@link RedisKeyValueTemplate}.
|
||||
*/
|
||||
@Produces
|
||||
public RedisKeyValueTemplate redisKeyValueAdapterDefault(RedisOperations<?, ?> redisOperations) {
|
||||
|
||||
RedisKeyValueAdapter redisKeyValueAdapter = new RedisKeyValueAdapter(redisOperations);
|
||||
RedisKeyValueTemplate keyValueTemplate = new RedisKeyValueTemplate(redisKeyValueAdapter, new RedisMappingContext());
|
||||
return keyValueTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.repository.cdi;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class RepositoryConsumer {
|
||||
|
||||
@Inject PersonRepository unqualifiedRepo;
|
||||
@Inject @PersonDB PersonRepository qualifiedRepo;
|
||||
|
||||
public PersonRepository getUnqualifiedRepo() {
|
||||
return unqualifiedRepo;
|
||||
}
|
||||
|
||||
public PersonRepository getQualifiedRepo() {
|
||||
return qualifiedRepo;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
|
||||
unqualifiedRepo.deleteAll();
|
||||
qualifiedRepo.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user