15 Ocak 2018 Pazartesi

@Service Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.springframework.stereotype.Service;
constructor - string
Şöyle yaparız.
@Service("fooImpl")
public class FooImpl implements Foo {
  ...
}
Şöyle yaparız.
ApplicationContext applicationContext = ...;

Foo foo = (Foo) applicationContext.getBean("fooImpl");

11 Ocak 2018 Perşembe

SpringBoot EmbeddedServletContainerCustomizer Arayüzü

Giriş
Şu satırı dahil ederiz.
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
Spring Boot projesine ait bir arayüz. @Configuration veya @SpringBootApplication anotasyonuna sahip bir sınıfın @Bean ile işaretli metodu ile kullanılır.

Projenin şöyle olması gerekir.
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
setPort metodu
Şöyle yaparız.
@Configuration
public class ServletConfig {
  @Bean
  public EmbeddedServletContainerCustomizer containerCustomizer() {
    return (container -> {
      container.setPort(8012);
    });
  }
}

26 Aralık 2017 Salı

SpringMVC @EnableWebMvc Anotasyonu

Giriş
Açıklaması şöyleç
Using @EnableWebMvc disables MVC autoconfiguration.

21 Aralık 2017 Perşembe

TransactionSynchronizationManager Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.transaction.support.TransactionSynchronizationManager;
Açıklaması şöyle.
Central helper that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.
isCurrentTransactionReadOnly metodu
Şöyle yaparız
@Transactional
void foo(){
  log.info("Is readonly -  " + TransactionSynchronizationManager
  .isCurrentTransactionReadOnly());
  ..
}
getCurrentTransactionName metodu
Şöyle yaparız
@Transactional
void foo(){
  log.info("Current Transaction Name -  " + TransactionSynchronizationManager
  .getCurrentTransactionName());
  ..
}
registerSynchronization metodu
registerSynchronization metodu yazısına taşıdım

setCurrentTransactionName metodu
Şöyle yaparız
@Transactional
void foo(){
  TransactionSynchronizationManager.setCurrentTransactionName("TestTransaction");
  ...
}

20 Aralık 2017 Çarşamba

SpringSecurity HttpSessionSecurityContextRepository Sınıfı

loadContext metodu
SecurityContext nesnesi döner. Şöyle yaparız.
@Override
public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
  SecurityContext context = super.loadContext(requestResponseHolder);

  Authentication auth = context.getAuthentication();
  if (auth instanceof OAuth2Authentication) {
    ...
  }
  return context;
}

19 Aralık 2017 Salı

SpringBeanAutowiringSupport Sınıfı

Giriş
Soyut bir sınıftır. Spring dışında yaratılan nesnelerin Spring @Autowire anotasyonu ile işaretli alanlarının dolmasını sağlar. Bu sınıftan kalıtan WebApplicationObjectSupport sınıfıdır.

processInjectionBasedOnCurrentContext metodu
Şöyle yaparız.
@PostConstruct
public void init(){
   SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

13 Aralık 2017 Çarşamba

SpringContext LinkedMultiValueMap Sınıfı - Multimap

Giriş
Şu satırı dahil ederiz.
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
constructor
Şöyle yaparız.
MultiValueMap<String, Object> MulMap = new LinkedMultiValueMap<>();
Şöyle yaparız.
MultiValueMap<String, Integer> multiValueMap=new LinkedMultiValueMap<>();
add metodu
Elimizde şöyle bir kod olsun.
MultiValueMap<String, String> headers; new LinkedMultiValueMap<>();
Şöyle yaparız.
String key = ...;String value = ...;
headers.add(key, value);