22 Ağustos 2017 Salı

SpringBatch JdbcCursorItemReader Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcCursorItemReader;
constructor
Şöyle yaparız.
JdbcCursorItemReader<Foo> databaseReader = new JdbcCursorItemReader<>();
setDataSource metodu
Şöyle yaparız.
DataSource dataSource = ...;
databaseReader.setDataSource(dataSource);
setRowMapper metodu
Şu satırı dahil ederiz.
import org.springframework.jdbc.core.BeanPropertyRowMapper;
Şöyle yaparız.
databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Foo.class));
setSql metodu
Örnek
Şöyle yaparız.
private static final String QUERY_FIND_STUDENTS =
        "SELECT " +
            "email_address, " +
            "name, " +
            "purchased_package " +
        "FROM STUDENTS " +
        "ORDER BY email_address ASC";

databaseReader.setSql(QUERY_FIND_STUDENTS);
Örnek
Şöyle yaparız.
@Autowired
public DataSource dataSource;


@Bean
public JdbcCursorItemReader<User> reader() {
  JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
  reader.setDataSource(dataSource);
  reader.setSql("SELECT id,name FROM employee");
  reader.setRowMapper(new UserRowMapper());
  return reader;
}

public class UserRowMapper implements RowMapper<User> {

  @Override
  public User mapRow(ResultSet rs, int rowNum) throws SQLException {
    User user = new User();
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));

    return user;
  }

}

8 Ağustos 2017 Salı

SpringMVC RequestContextHolder Sınıfı

Giriş
Spring MVC projesinde kullanılır. Açıklaması şöyle
This will be populated by the standard spring mvc dispatch servlet.
currentRequestAttributes metodu
ServletRequestAttributes nesnesi döner. 
Örnek
Şöyle yaparız.
ServletRequestAttributes attr = (ServletRequestAttributes)
  RequestContextHolder.currentRequestAttributes();
getRequestAttributes metodu
Örnek
Şöyle yaparız
@Component
public class RequestUtils {

  private final ObjectMapper objectMapper;

  private static final String APP_CONTEXT_HEADER = "X-App-Context";
  private static final String REQUEST_ATTRIBUTE_APP_CONTEXT = "appContext";

  public RequestUtils(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }

  public void storeContextInformationAsRequestAttribute(HttpServletRequest request)
throws IOException {
    String jsonHeader = request.getHeader(APP_CONTEXT_HEADER);

    if (jsonHeader != null) {
      request.setAttribute(REQUEST_ATTRIBUTE_APP_CONTEXT,
        objectMapper.readValue(jsonHeader, ContextInformation.class));
      }
  }

  public Optional<ContextInformation> getContextInformation() {
  return getAttributeFromRequest(REQUEST_ATTRIBUTE_APP_CONTEXT, ContextInformation.class);
  }

  private <T> Optional<T> getAttributeFromRequest(String attributeName, Class<T> type) {
    Object attribute = RequestContextHolder.getRequestAttributes()
.getAttribute(attributeName, 0);
    if (attribute != null && type.isAssignableFrom(attribute.getClass())) {
      return Optional.of((T) attribute);
    }
    return Optional.empty();
  }
}

22 Haziran 2017 Perşembe

Gateway

Giriş
Spring Integration projesinin bir parçası. Açıklaması şöyle
Any bidirectional, or request-reply, adapter is known as a Gateway.
Açıklaması şöyle
Whereas the JMS Channel Adapters are intended for unidirectional Messaging (send-only or receive-only), Spring Integration also provides inbound and outbound JMS Gateways for request/reply operations.


5 Haziran 2017 Pazartesi

SpringContext @ApplicationScope Anotasyonu - Bean'in Yaşamı Servlet'e Bağlıdır

Giriş
Web uygulamalarında kullanılır. Açıklaması şöyle
singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
globalSession Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
@Scope Anotasyonu yazısına bakabilirsiniz.

Örnek
Şöyle yaparız.
@ApplicationScope
@Component
public class App {
    // ...
}

SqlSessionTemplate Sınıfı

Giriş
Şu satırı dahil ederiz
import org.mybatis.spring.SqlSessionTemplate;
Bu sınıf MyBatis projesine ait.

application.properties
Şöyle yaparız
spring.datasource.url=jdbc:mysql://localhost:3306/airportdb
spring.datasource.username=username
spring.datasource.password=passoword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver

mybatis.configuration.map-underscore-to-camel-case = true
mybatis.configuration.defaultStatementTimeout = 10
DataMapper Örüntüsü
MyBatis "Patterns of Enterprise Application Architecture" kitabındaki DataMapper örüntüsünü kullanılır.

constructor
Şöyle yaparız.
@Autowired
private SqlSessionTemplate sqlSession;
delete metodu
Şöyle yaparız.
public Object delete(String queryId, Object params){
  return sqlSession.delete(queryId, params);
}
insert metodu
Şöyle yaparız.
public Object insert(String queryId, Object params){
  return sqlSession.insert(queryId, params);
}
update metodu
Şöyle yaparız.
public Object update(String queryId, Object params){
  return sqlSession.update(queryId, params);
}

selectList metodu
Şöyle yaparız.
@SuppressWarnings("rawtypes")
public List selectList(String queryId){
  return sqlSession.selectList(queryId);
}

@SuppressWarnings("rawtypes")
public List selectList(String queryId, Object params){
  return sqlSession.selectList(queryId,params);
}
selectOne metodu
Şöyle yaparız.
public Object selectOne(String queryId){
  return sqlSession.selectOne(queryId);
}

public Object selectOne(String queryId, Object params){
  return sqlSession.selectOne(queryId, params);
}



28 Mayıs 2017 Pazar

SpringMVC WebApplicationContext Sınıfı

Giriş
ApplicationContext arayüzünü gerçekleştirir. Açıklaması şöyle
Interface to provide configuration for a web application.
Şeklen şöyle
Açıklaması şöyle
Sometimes circumstances do not allow you to completely switch to a different framework. The Spring Framework does not force you to use everything within it; it is not an all-or-nothing solution. Existing front-ends built with Struts, Tapestry, JSF or other UI frameworks can be integrated with a Spring- based middle-tier, which allows you to use Spring transaction features. You simply need to wire up your business logic using an ApplicationContext and use a WebApplicationContext to integrate your web layer.
Spring ile 3 çeşit WebApplication yapılabilir. Açıklaması şöyle
For REACTIVE , it will create AnnotationConfigReactiveWebServerApplicationContext
For SERVLET , it will create AnnotationConfigServletWebServerApplicationContext
For NONE, it will create AnnotationConfigApplicationContext

constructor
Spring tarafından yaratılmayan HttpServlet sınıfında erişmek için şöyle yaparız.
WebApplicationContext appContext = WebApplicationContextUtils
  .getWebApplicationContext(getServletContext());