Spring I/O 2016報告会
- Spring 4.3の新機能
- Spring Data、Spring Integrationの紹介
2016/6/22 JSUG
2016/6/22 JSUG
分類 | 新機能 |
---|---|
Container | コンストラクタの@Autowired 省略に対応 |
Web | 各種合成アノテーションの提供 |
暗黙的なHEADとOPTIONSのレスポンス作成 | |
@SessionAttribute や@RequestAttribute の追加 |
|
Boot/Test | 別セッションで |
@Autowired
省略@Service
public class MyBookService {
private MyBookRepository repository;
// @Autowired <- 省略可能となった
public MyBookService(MyBookRepository repository) {
this.repository = repository;
}
}
@Autowired
省略// @RequiredArgsConstructor(onConstructor = @__(@Autowired))
// @Autowiredをコンストラクタに付けるために必要だった呪文が不要に
@Service
public class MyBookService {
private MyBookRepository repository;
// コンストラクタがLombokによりコンパイル時に生成される
}
@RequestMapping
の合成アノテーション
@GetMapping
, @PostMapping
, @PutMapping
, @DeleteMapping
, @PatchMapping
// Before
@RequestMapping(value = "/books", method = RequestMethod.GET)
public String getBooks() {...}
// After
@GetMapping("/books")
public String getBooks() {...}
@Scope
の合成アノテーション
@RequestScope
, @SessionScope
, @ApplicationScope
// Before
@Scope(value = WebApplicationContext.SCOPE_SESSION,
target = ScopedProxyMode.TARGET_CLASS)
public class SessionScopedService { ... }
// After
@SessionScope
public class SessionScopedService { ... }
@HeadMapping
と@OptionsMapping
が無い?
@GetMapping("/books")
public String getBooks() {...}
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Allow: GET,HEAD
...
@SessionAttribute
や@RequestAttribute
の追加HttpSession
などへのアクセスを宣言的に実現@SessionAttributes
と間違えないように// Before
@RequestMapping(value = "/books", method = RequestMethod.GET)
public String getBooks(HttpSession session) {
String id = (String) session.getAttribute("bookId");
// ...
}
// After
@RequestMapping("/books")
public String getBooks(@SessionAttribute("bookId") String bookId) {
String id = bookId;
// ...
}
Release | Spring | Commons | JPA | GemFire | MongoDB |
---|---|---|---|---|---|
Ingalls | 4.3? | 1.13.x? | 1.11.x? | 1.9.x? | 1.10.x? |
Hopper | 4.2 | 1.12.x | 1.10.x | 1.8.x | 1.9.x |
Gosling | 4.1 | 1.11.x | 1.9.x | 1.7.x | 1.8.x |
Fowler | 4.0 | 1.10.x | 1.8.x | 1.6.x | 1.7.x |
public interface BookRepository extends CrudRepository<Book, Long> {}
public class BookService {
@Autowired
BookRepository repository;
@Transactional(readOnly = true)
public Book getBook(Long id) {
return repository.findOne(id);
}
}
Predicates
のWebサポート(Gosling~)List
-> Stream
によりStream APIの恩恵を受けられるpublic interface PersonRepository
extends CrudRepository<Person, String> {
// Before
@Query("SELECT p FROM person p")
List<Person> findAllWithList();
// After
@Query("SELECT p FROM person p")
Stream<Person> findAllWithStream();
}
// Before (All records are fetched before streaming)
List<Person> list = repository.findAllWithList();
list.stream().forEach(System.out::println);
// After (Fetched one by one)
repository.findAllWithStream().forEach(System.out::println);
println
の出力タイミングを見比べていた
// last nameが"White"のユーザを検索するクエリ
Example<User> example = Example.of(new User(null, "White", null));
int countMatches = userRepository.count(example);
List<User> usersMatches = userRepository.findAll(example);
public interface UserRepository
extends CrudRepository<User, String>,
QueryByExampleExecutor<User> {
}
@Entity
public class Person {
@Id @GeneratedValue private Long id;
private String firstName, lastName;
private String street, state, country;
// …
}
// Before
Person person = personRepository.findOne(id);
String fullName = person.getFirstName() + " " + person.getLastName();
public interface FullName {
@Value("#{target.firstName} #{target.lastName}")
String getFullName();
}
public interface PersonRepository
extends CrudRepository<Person, Long> {
FullName findProjectedById(Long id);
}
// After
String fullName = personRepository.findProjectedById(id).getFullName();
<file:inbound-channel-adapter id="filesIn"
directory="file:xxx/input">
<integration:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>
<integration:service-activator input-channel="filesIn"
output-channel="filesOut"
ref="handler" method="handle" />
<file:outbound-channel-adapter id="filesOut"
directory="file:xxx/output"
delete-source-files="true"/>
<bean id="handler" class="com.example.Handler"/>
public class Handler {
public String handle(String payload) {
System.out.println("Copying text: " + payload);
return payload.toUpperCase();
}
}