27 Ekim 2017 Cuma

SpringData QueryDslPredicateExecutor Sınıfı - Repository Sınıfı İçindir

Giriş
Şu satırı dahil ederiz.
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
Repository sınıfımız bu sınıftan kalıtır
Örnek
Şöyle yaparız
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

import com.abhicodes.querydsldynamicquery.entity.Post;

@Repository
public interface PostRepository extends JpaRepository<Post, Integer>, 
  QuerydslPredicateExecutor<Post> {

}

findAll metodu
Örnek
Şöyle yaparız.
public interface SomeRepository extends JpaRepository<Some, Long>,
  PagingAndSortingRepository<Some, Long>, QueryDslPredicateExecutor<Some>{

}
Query dsl kullanarak şöyle yaparız.
@Controller
class SomeController {

  private final SomeRepository repository;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  String index(Model model,
               @QuerydslPredicate(root = Some.class) Predicate predicate,
               Pageable pageable) {

    model.addAttribute("data", repository.findAll(predicate, pageable));
    return "index";
  }
}

24 Ekim 2017 Salı

TcpNetClientConnectionFactory Sınıfı

setTcpSocketFactorySupport metodu
Şöyle yaparız.
private static final int SERIALIZER_HEADER_SIZE = 2;

@Bean
public ByteArrayLengthHeaderSerializer byteArrayLengthHeaderSerializer() {
  return new ByteArrayLengthHeaderSerializer(SERIALIZER_HEADER_SIZE);
}

@Bean
public AbstractClientConnectionFactory tcpClientConnectionFactory() {
  TcpNetClientConnectionFactory connFactory =
    new TcpNetClientConnectionFactory(props.getUrl(), props.getPort());
  connFactory.setSerializer(byteArrayLengthHeaderSerializer());
  connFactory.setDeserializer(byteArrayLengthHeaderSerializer());
  connFactory.setSoTimeout(props.getSoTimeout());

  if (props.isUseSSL()) {
    connFactory.setTcpSocketFactorySupport(new DefaultTcpNetSSLSocketFactorySupport(()-> {
      return SSLContext.getDefault();
    }));
  }

  return connFactory;
}

26 Eylül 2017 Salı

JndiObjectFactoryBean Sınıfı

Giriş
JNDI nesnesini Spring Bean haline getirir.

Kullanım
Şöyle yaparız
<bean id="dbServiceBean" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="#{T(com.foo.DbAccessUtils).DB_SERVICE_BEAN_JNDI}" />
  <property name="proxyInterface" value="com.foo.IDbServiceLocal" />
</bean>
setLookupOnStartup metodu
Örnek
Lazy init için XML ile şöyle yaparız.
<jee:jndi-lookup id="datasource" jndi-name="java:/comp/env/jdbc/Tomcat8Database"
  destroy-method="close" expected-type="javax.sql.DataSource" lookup-on-startup="false"
  proxy-interface="javax.sql.DataSource"/>

24 Eylül 2017 Pazar

SpringSecurity SecurityContextLogoutHandler Sınıfı

Giriş
SecurityContextHolder aracılığı ile SecurityContext nesnesi üzerinde değişikli yapabilmeyi sağlar

logout metodu
Şöyle yaparız
@GetMapping("/logout")
public String getLogoutPage(HttpServletRequest request,
  HttpServletResponse response){

  Authentication authentication = SecurityContextHolder.getContext()
    .getAuthentication();
  if (authentication != null)
    new SecurityContextLogoutHandler().logout(request, response, authentication);

  return "redirect:/login";
}