13 Ocak 2020 Pazartesi

SpringMVC @GetMapping Anotasyonu - Rest

Giriş
Şu satırı dahil ederiz.
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@RequestMapping(method = RequestMethod.GET) için kısayoldur (alias).

Rest yapıyorsak @RestController olarak işaretli sınıfın metodunda kullanılır.
SpringMVC yapıyorsak @Controller olarak işaretli sınıfın metodunda kullanılır

Örnek
Şöyle yaparız.
@GetMapping
public String getFooBarred(@RequestParam(value = "params") String requestItem) {
  ...
}
Örnek - @PathVariable
Şöyle yaparız.
@GetMapping("/{id}")
public String getMarketDataById(@PathVariable Long id) {
  // ...
}
headers Alanı
Get isteğindeki bir header değerine göre farklı metodları işletmek için şöyle yaparız.
@GetMapping(value = "/student/header", headers = {"X-API-VERSION=1"})
public StudentV1 headerV1() {
    return serviceImpl.headerV1();
}

@GetMapping(value = "/student/header", headers = {"X-API-VERSION=2"})
public StudentV1 headerV2() {
    return serviceImpl.headerV2();
}

SpringBatch StepExecution Sınıfı

Örnek
Şöyle yaparız.
@Value("#{stepExecution}")
private StepExecution stepExecution;

SpringData @EnableTransactionManagement Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement anotasyonu proxy kodlarını üretilmesini sağlar.

XML Olarak Tanımlama
Örnek
Tanımlamayı XML ile şöyle yaparız
<context:annotation-config />

<tx:annotation-driven />
Kodla Tanımlama
Örnek
Şöyle yaparız.
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
public class PersistenceContext
{
     @Bean
     public AuditorAware<AccountModel> auditorAware()
     {
         return new AuditorAwareImpl();
     }
}
mode Alanı
Örnek
Şöyle yaparız.
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
Örnek
Şöyle yaparız.
<tx:annotation-driven mode="aspectj"/>
order Alanı
Örnek
Şöyle yaparız.
@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableTransactionManagement(order = AspectPrecedence.TRANSACTION_MANAGEMENT_ORDER)
public class AppConfiguration {..}

10 Ocak 2020 Cuma

SpringScheduling ThreadPoolExecutorFactoryBean Sınıfı

constructor
Şöyle yaparız.
@Bean
public ThreadPoolExecutorFactoryBean myExecutor() {
 ThreadPoolExecutorFactoryBean executorFactoryBean = new ThreadPoolExecutorFactoryBean();
 // configuration of your choice
 return executorFactoryBean;
}

8 Ocak 2020 Çarşamba

SpringBatch StaxEventItemReader Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.batch.item.xml.StaxEventItemReader;

setUnmarshaller metodu
Şöyle yaparız.
@Bean
public StaxEventItemReader<Customer> customerItemReader(){

  Map<String, Class> aliases = new HashMap<>();
  aliases.put("customer", Customer.class);


  CustomerConverter converter = new CustomerConverter();
  XStreamMarshaller ummarshaller = new XStreamMarshaller();
  ummarshaller.setAliases(aliases);
  ummarshaller.setConverters(converter);


  StaxEventItemReader<Customer> reader = new StaxEventItemReader<>();
  reader.setResource(new ClassPathResource("/data/customer.xml"));
  reader.setFragmentRootElementName("customer");
  reader.setUnmarshaller(ummarshaller);
  return reader;
}

SpringBatch MultiResourceItemWriter Sınıfı

Giriş
Açıklaması şöyle.
This item writer will create multiple output files based on item number: whenever the number of written items exceeds a given limit, it will create a new file.

SpringLDAP LdapContextSource Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.ldap.core.support.LdapContextSource;
Açıklaması şöyle.
ContextSource is used for creating the LdapTemplate.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.ldap</groupId>
  <artifactId>spring-ldap-core</artifactId>
</dependency>
constructor

Elimizde application.yml dosyası olsun.
ldap:
 contextSource:
   url: ldap://192.168.113:389 #Local
   base: dc=test,dc=test
   userDn: cn=admin,dc=test,dc=test
   password: test2019!@
Şöyle yaparız.
@Configuration
public class LdapConfiguration {
  @Bean
  @ConfigurationProperties(prefix="ldap.context-source")
  public LdapContextSource contextSource() {
    return new LdapContextSource();
  }

 @Bean
 public ContextSource poolingLdapContextSource() {
   PoolingContextSource pcs = new PoolingContextSource();
   pcs.setDirContextValidator(new DefaultDirContextValidator());
   pcs.setContextSource(contextSource());

  TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(pcs);

   return proxy;
  }
  // other configs like ldaptemplate
}
setBase metodu
Şöyle yaparız.
@Bean
public ContextSource contextSource() {
  LdapContextSource contextSource = new LdapContextSource();

  contextSource.setUrl("ldap://...");
  contextSource.setBase("dc=test,dc=test");
  contextSource.setUserDn("cn=admin,dc=test,dc=test");
  contextSource.setPassword("test2019!@");
  contextSource.afterPropertiesSet();

  PoolingContextSource pcs = new PoolingContextSource();
  pcs.setDirContextValidator(new DefaultDirContextValidator());
  pcs.setContextSource(contextSource);

  TransactionAwareContextSourceProxy proxy = new TransactionAwareContextSourceProxy(pcs);

  return proxy;
}
setUrl metodu
Şöyle yaparız.
contextSource.setUrl("ldap://192.168.113.12:389");