Moved spring-4.3 project into spring-all (#500)
* Moved spring-4.3 project into spring-all * Removed spring-4.3 project
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
eb1afcbb2c
commit
b5a3fabb75
+14
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring43.attributeannotations;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
public class AttributeAnnotationsTestController {
|
||||
|
||||
@GetMapping
|
||||
public String get(@SessionAttribute String login, @RequestAttribute String query) {
|
||||
return String.format("login = %s, query = %s", login, query);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.spring43.attributeannotations;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
public class ParamInterceptor extends HandlerInterceptorAdapter {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
request.getSession().setAttribute("login", "john");
|
||||
request.setAttribute("query", "invoices");
|
||||
return super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.baeldung.spring43.cache;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Foo {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Foo.class);
|
||||
|
||||
private static final AtomicInteger instanceCount = new AtomicInteger(0);
|
||||
|
||||
|
||||
private final int instanceNum;
|
||||
|
||||
public Foo() {
|
||||
instanceNum = instanceCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public static int getInstanceCount() {
|
||||
return instanceCount.get();
|
||||
}
|
||||
|
||||
public void printInstanceNumber() {
|
||||
log.info("Foo instance number: {}", instanceNum);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring43.cache;
|
||||
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FooService {
|
||||
|
||||
@Cacheable(cacheNames = "foos", sync = true)
|
||||
public Foo getFoo(String id) {
|
||||
return new Foo();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.baeldung.spring43.composedmapping;
|
||||
|
||||
public class Appointment {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.baeldung.spring43.composedmapping;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface AppointmentService {
|
||||
|
||||
Map<String, Appointment> getAppointmentsForToday();
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package org.baeldung.spring43.composedmapping;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/appointments")
|
||||
public class AppointmentsController {
|
||||
|
||||
private final AppointmentService appointmentService;
|
||||
|
||||
@Autowired
|
||||
public AppointmentsController(AppointmentService appointmentService) {
|
||||
this.appointmentService = appointmentService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Map<String, Appointment> get() {
|
||||
return appointmentService.getAppointmentsForToday();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
public class FooRepository {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
public class FooService {
|
||||
|
||||
private final FooRepository repository;
|
||||
|
||||
public FooService(FooRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public FooRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class DateHolder implements IDateHolder {
|
||||
|
||||
private LocalDate localDate;
|
||||
|
||||
@Override
|
||||
public LocalDate getLocalDate() {
|
||||
return localDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocalDate(LocalDate localDate) {
|
||||
this.localDate = localDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public interface IDateHolder {
|
||||
|
||||
LocalDate getLocalDate();
|
||||
|
||||
void setLocalDate(LocalDate localDate);
|
||||
|
||||
default void setStringDate(String stringDate) {
|
||||
setLocalDate(LocalDate.parse(stringDate, DateTimeFormatter.ofPattern("dd.MM.yyyy")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.baeldung.spring43.depresolution;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class FooRepository {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.spring43.depresolution;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FooService {
|
||||
|
||||
private final FooRepository repository;
|
||||
|
||||
public FooService(ObjectProvider<FooRepository> repositoryProvider) {
|
||||
this.repository = repositoryProvider.getIfUnique();
|
||||
}
|
||||
|
||||
public FooRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.annotation.ApplicationScope;
|
||||
|
||||
@Component
|
||||
@ApplicationScope
|
||||
public class AppPreferences extends InstanceCountingService {
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class InstanceCountingService {
|
||||
|
||||
private static final AtomicInteger instanceCount = new AtomicInteger(0);
|
||||
|
||||
private final int instanceNumber = instanceCount.incrementAndGet();
|
||||
|
||||
public int getInstanceNumber() {
|
||||
return instanceNumber;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.annotation.RequestScope;
|
||||
|
||||
@Component
|
||||
@RequestScope
|
||||
public class LoginAction extends InstanceCountingService {
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/appointments")
|
||||
public class ScopeTestController {
|
||||
|
||||
@Autowired
|
||||
private LoginAction loginAction;
|
||||
|
||||
@Autowired
|
||||
private UserPreferences userPreferences;
|
||||
|
||||
@Autowired
|
||||
private AppPreferences appPreferences;
|
||||
|
||||
@GetMapping("/request")
|
||||
public String getRequestNumber() {
|
||||
return Integer.toString(loginAction.getInstanceNumber());
|
||||
}
|
||||
|
||||
@GetMapping("/session")
|
||||
public String getSessionNumber() {
|
||||
return Integer.toString(userPreferences.getInstanceNumber());
|
||||
}
|
||||
|
||||
@GetMapping("/application")
|
||||
public String getApplicationNumber() {
|
||||
return Integer.toString(appPreferences.getInstanceNumber());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.annotation.SessionScope;
|
||||
|
||||
@Component
|
||||
@SessionScope
|
||||
public class UserPreferences extends InstanceCountingService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="dateHolder" class="org.baeldung.spring43.defaultmethods.DateHolder">
|
||||
<property name="stringDate" value="15.10.1982"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean class="org.baeldung.spring43.ctor.FooRepository"/>
|
||||
|
||||
<bean class="org.baeldung.spring43.ctor.FooService"/>
|
||||
|
||||
</beans>
|
||||
@@ -9,6 +9,6 @@
|
||||
<task:annotation-driven executor="myExecutor"/>
|
||||
<task:executor id="myExecutor" pool-size="5"/>
|
||||
|
||||
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncAnnotationExample"/>
|
||||
<bean id="asyncAnnotationExample" class="org.baeldung.async.AsyncComponent"/>
|
||||
|
||||
</beans>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.spring43.attributeannotations;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableWebMvc
|
||||
public class AttributeAnnotationConfiguration extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/WEB-INF/jsp/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new ParamInterceptor());
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package org.baeldung.spring43.attributeannotations;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ContextConfiguration(classes = AttributeAnnotationConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
public class AttributeAnnotationTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInterceptorAddsRequestAndSessionParams_thenParamsInjectedWithAttributesAnnotations() throws Exception {
|
||||
String result = this.mockMvc.perform(get("/test")
|
||||
.accept(MediaType.ALL))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
Assert.assertEquals("login = john, query = invoices", result);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package org.baeldung.spring43.cache;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableCaching
|
||||
public class CacheRefinementsConfiguration {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager manager = new SimpleCacheManager();
|
||||
manager.setCaches(Collections.singletonList(new ConcurrentMapCache("foos")));
|
||||
return manager;
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package org.baeldung.spring43.cache;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ContextConfiguration(classes = CacheRefinementsConfiguration.class)
|
||||
public class CacheRefinementsTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
private ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
|
||||
@Autowired
|
||||
private FooService service;
|
||||
|
||||
@Test
|
||||
public void whenMultipleThreadsExecuteCacheableMethodWithSyncTrue_thenMethodIsExecutedOnlyOnce() throws InterruptedException {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
executorService.execute(() -> service.getFoo("test").printInstanceNumber());
|
||||
}
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
assertEquals(Foo.getInstanceCount(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.baeldung.spring43.composedmapping;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableWebMvc
|
||||
public class ComposedMappingConfiguration {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/WEB-INF/jsp/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AppointmentService appointmentBook() {
|
||||
AppointmentService book = EasyMock.mock(AppointmentService.class);
|
||||
EasyMock.expect(book.getAppointmentsForToday()).andReturn(Collections.emptyMap());
|
||||
replay(book);
|
||||
return book;
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package org.baeldung.spring43.composedmapping;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ContextConfiguration(classes = ComposedMappingConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
public class ComposedMappingTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private AppointmentService appointmentService;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingMethodWithGetMapping_thenReceiving200Answer() throws Exception {
|
||||
this.mockMvc.perform(get("/appointments")
|
||||
.accept(MediaType.ALL))
|
||||
.andExpect(status().isOk());
|
||||
verify(appointmentService);
|
||||
}
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@ContextConfiguration(classes = {FooRepositoryConfiguration.class, FooServiceConfiguration.class})
|
||||
public class ConfigurationConstructorInjectionTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
public FooService fooService;
|
||||
|
||||
@Test
|
||||
public void whenSingleCtorInConfiguration_thenContextLoadsNormally() {
|
||||
assertNotNull(fooService.getRepository());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class FooRepositoryConfiguration {
|
||||
|
||||
@Bean
|
||||
public FooRepository fooRepository() {
|
||||
return new FooRepository();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class FooServiceConfiguration {
|
||||
|
||||
private final FooRepository repository;
|
||||
|
||||
public FooServiceConfiguration(FooRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FooService fooService() {
|
||||
return new FooService(this.repository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.spring43.ctor;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@ContextConfiguration("classpath:implicit-ctor-context.xml")
|
||||
public class ImplicitConstructorTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
|
||||
@Test
|
||||
public void whenBeanWithoutAutowiredCtor_thenInjectIntoSingleCtor() {
|
||||
assertNotNull(fooService.getRepository());
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@ContextConfiguration("classpath:defaultmethods-context.xml")
|
||||
public class DefaultMethodsInjectionTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private IDateHolder dateHolder;
|
||||
|
||||
@Test
|
||||
public void whenInjectingToDefaultInterfaceMethod_thenInjectionShouldHappen() {
|
||||
assertEquals(LocalDate.of(1982, 10, 15), dateHolder.getLocalDate());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
|
||||
public interface ITransactionalTest {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(ITransactionalTest.class);
|
||||
|
||||
@BeforeTransaction
|
||||
default void beforeTransaction() {
|
||||
log.info("Opening transaction");
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
default void afterTransaction() {
|
||||
log.info("Closing transaction");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
|
||||
@ContextConfiguration(classes = TransactionalTestConfiguration.class)
|
||||
public class TransactionalTest extends AbstractTransactionalJUnit4SpringContextTests implements ITransactionalTest {
|
||||
|
||||
@Test
|
||||
public void whenDefaultMethodAnnotatedWithBeforeTransaction_thenDefaultMethodIsExecuted() {
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.spring43.defaultmethods;
|
||||
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
@Configuration
|
||||
public class TransactionalTestConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource getDataSource() {
|
||||
SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
|
||||
simpleDriverDataSource.setDriverClass(org.hsqldb.jdbcDriver.class);
|
||||
simpleDriverDataSource.setUrl("jdbc:hsqldb:mem:app-db");
|
||||
simpleDriverDataSource.setUsername("sa");
|
||||
simpleDriverDataSource.setPassword("");
|
||||
return simpleDriverDataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
return new DataSourceTransactionManager(getDataSource());
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package org.baeldung.spring43.depresolution;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class ObjectProviderConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.baeldung.spring43.depresolution;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@ContextConfiguration(classes = ObjectProviderConfiguration.class)
|
||||
public class ObjectProviderTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
|
||||
@Test
|
||||
public void whenArgumentIsObjectProvider_thenObjectProviderInjected() {
|
||||
|
||||
assertNotNull(fooService.getRepository());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableWebMvc
|
||||
public class ScopeAnnotationsConfiguration {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setPrefix("/WEB-INF/jsp/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package org.baeldung.spring43.scopeannotations;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ContextConfiguration(classes = ScopeAnnotationsConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
public class ScopeAnnotationsTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDifferentRequests_thenDifferentInstancesOfRequestScopedBeans() throws Exception {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
|
||||
String requestScopedServiceInstanceNumber1 = this.mockMvc.perform(get("/appointments/request")
|
||||
.session(session)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
String requestScopedServiceInstanceNumber2 = this.mockMvc.perform(get("/appointments/request")
|
||||
.session(session)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
assertNotEquals(requestScopedServiceInstanceNumber1, requestScopedServiceInstanceNumber2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDifferentSessions_thenDifferentInstancesOfSessionScopedBeans() throws Exception {
|
||||
|
||||
MockHttpSession session1 = new MockHttpSession();
|
||||
MockHttpSession session2 = new MockHttpSession();
|
||||
|
||||
String sessionScopedServiceInstanceNumber1 = this.mockMvc.perform(get("/appointments/session")
|
||||
.session(session1)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
String sessionScopedServiceInstanceNumber2 = this.mockMvc.perform(get("/appointments/session")
|
||||
.session(session1)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
String sessionScopedServiceInstanceNumber3 = this.mockMvc.perform(get("/appointments/session")
|
||||
.session(session2)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
assertEquals(sessionScopedServiceInstanceNumber1, sessionScopedServiceInstanceNumber2);
|
||||
|
||||
assertNotEquals(sessionScopedServiceInstanceNumber1, sessionScopedServiceInstanceNumber3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDifferentSessionsAndRequests_thenAlwaysSingleApplicationScopedBean() throws Exception {
|
||||
|
||||
MockHttpSession session1 = new MockHttpSession();
|
||||
MockHttpSession session2 = new MockHttpSession();
|
||||
|
||||
String applicationScopedServiceInstanceNumber1 = this.mockMvc.perform(get("/appointments/application")
|
||||
.session(session1)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
String applicationScopedServiceInstanceNumber2 = this.mockMvc.perform(get("/appointments/application")
|
||||
.session(session2)
|
||||
.accept(MediaType.ALL)).andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
assertEquals(applicationScopedServiceInstanceNumber1, applicationScopedServiceInstanceNumber2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user