27 Eylül 2018 Perşembe

SpringData @CreatedDate Anotasyonu

Giriş
@EnableJpaAuditing anotasyonu kodda olmalıdır. Ayrıca sınıfa JPA'nın @EntityListener anotasyonuna Spring'in AuditingEntityListener sınıfı verilir.

Örnek
Şöyle yaparız.
@Entity
@Table
@EntityListeners(AuditingEntityListener.class)
public class Student implements Serializable {
  ...
  @Column(nullable = false, updatable = false)
  @Temporal(TemporalType.TIMESTAMP)
  @CreatedDate
  private Date createdAt;
  ...
}
Örnek
Şöyle yaparız.
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)
public abstract class BaseEntity  {

  @CreatedDate
  @Column(name = "created_at")
  private Instant createdAt;

  @LastModifiedDate
  @Column(name = "updated_at")
  private Instant updatedAt;

  ...

}

SpringIntegration MessageChannels Sınıfı

publishSubscriber metodu
Örnek
Şöyle yaparız.
@Configuration
public class FTPIntegration {


  @Bean
  public MessageChannel outputIntegrationChannel(ErrorHandler errorHandler) {
    return MessageChannels
            .publishSubscribe()
            .errorHandler(errorHandler)
            .get();
  }

  @Bean
  public IntegrationFlow ftpIntegration(ErrorHandler errorHandler,
    MessageHandler messageHandler) {
    return IntegrationFlows
            .from(outputIntegrationChannel(errorHandler))
            .transform(Transformers.toJson())
            .handle(messageHandler)
            .get();
  }
}
Örnek
Şöyle yaparız.
@Bean(name = {"publishCha.input", "publishCha2.input"}) //2 subscribers
public MessageChannel publishAction() {
  PublishSubscribeChannel ps = MessageChannels.publishSubscribe().get();
  ps.setMaxSubscribers(8);
  return ps;
}

26 Eylül 2018 Çarşamba

SpringBoot DataSourceBuilder Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.boot.jdbc.DataSourceBuilder;
Açıklaması şöyle
DataSourceBuilder is a utility or convenience class in spring boot that provides a convenient way to create and configure a datasource.
Using this class, we can create a datasource in using connection parameters, such as database URL, username, and password.
Örnek
Şöyle yaparız
@Configuration
public class DataSourceConfig {
  @Bean
  public DataSource dataSource() {
    return DataSourceBuilder
      .create()
      .url("jdbc:mysql://localhost/appdb")
      .username("user")
      .password("password")
      .driverClassName("com.mysql.jdbc.Driver")
      .build();
  }
}
Açıklaması şöyle
Note that we are marking the class with @Configuration annotation, which marks it as source of beans.
And in the configuration class, we expose a bean of type DataSource.
Örnek
Şöyle yaparız.
@Bean(name = "dataSource")
public DataSource getDataSource() {
  DataSource dataSource = DataSourceBuilder
    .create()
    .username("ALERTTEST")
    .password("ALERTTEST")
    .url("jdbc:oracle:thin:@192.168.0.198:1521:FCUBS")  
    .driverClassName("oracle.jdbc.OracleDriver")
    .build();
  return dataSource;
}

Diğer
Bu sınıfı yaratmak için DataSourceProperties kullanılabilir.

Örnek
Şöyle yaparız.
# first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

# second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver


@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
  return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
  return DataSourceBuilder.create().build();
}
Örnek
Şöyle yaparız.
@Configuration
@EnableTransactionManagement
public class JdbcDatasource {

  @Bean
  @ConfigurationProperties(prefix = "my.datasource")
  public DataSource myDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean
  public JdbcTemplate myJdbcTemplate() {
    return new JdbcTemplate(myDataSource());
  }

  @Bean
  public PlatformTransactionManager myTransactionManager() {
    return new DataSourceTransactionManager(myDataSource());
  }
  ...
}