- Environment profiles for bean definitions
- Java-based application configuration
- Overhaul of the test context framework
- 'c:' namespace
- Cache abstraction & declarative caching
- Explicit support for Servlet 3.0
- @MVC processing & flash attributes
- Refined JPA support
- Hibernate 4.0 & Quartz 2.0
- Support for Java SE 7
- org.springframework.core.env.PropertyResolver
- org.springframework.core.env.Environment
- XML 'profile' attribute on element
<beans profiles="dev">
<jdbc:embedded-database id="dataSource" type="H2"/>
</beans>
<beans profiles="prod">
<bean id="dataSource" class="..."/>
</beans>
- @Profile annotation on configuration classes or individual component classes
@Configuration
@Profile("dev")
public class AppConfig {
@Bean
public DataSource embeddedDatabase() { ... }
}
-
Activating specific profiles by name
Environment Variable : export spring.profiles.active=dev
JVM Parameter : -Dspring.profiles.active=prod
Web.xml : context-param, init-param
-
@Configuration
-
@ComponentScan
-
@Bean
-
@Enable*
@EnableTransactionManagement
@EnableWebMvc
-
@PropertySource("classpath:META-INF/app.properties")
-
@ActiveProfiles("dev")
<bean class="…" c:age="10" c:name="myName"/>
<bean class="…" c:name-ref="nameBean" c:spouse-ref="spouseBean"/>
- Declarative Caching
<cache:annotation-driven>
@Cacheable
public Owner loadOwner(int id);
@Cacheable(condition="name.length < 10")
public Owner loadOwner(String name);
@CacheEvict
public void deleteOwner(int id);
-
Backend adapters for EhCache, GemFire, Coherence, etc
EhCacheCacheManager
GemFireCacheManager
- support for XML-free web application setup (no web.xml)
- support for asynchronous request processing
- standard Servlet 3.0 file upload support behind Spring's MultipartResolver abstraction
- New @MVC Infrastructure
- FlashMap support and FlashMapManager abstraction
- Package scanning without persistence.xml
- Consistent JPA setup by persistence unit name
- making best use of JRE 7 at runtime
- support for JDBC 4.1
- support for fork-join framework
Spring @MVC 3.1: Key Themes ================================================================================================
- Java Config
- Consumes/Produces
- URI Variables
- Redirect & Flash Attributes
- Multipart Request Support
- UriComponentsBuilder
- HDIV Integration
- New @MVC Infrastructure
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext appContext = new XmlWebApplicationContext()
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/main");
}
}
// Equivalent to <mvc:annotation:driven/>
@EnableWebMvc
@Configuration
public class WebConfig {
}
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
protected void addFormatters(FormatterRegistry registry) {
// ...
}
@Override
public void addInterceptors(InterceptorRegistry reg){
// Equivalent to <mvc:interceptors>
}
// ... more available
}
@ResponseBody
@RequestMapping(
method=RequestMethod.POST,
header="Content-Type=application/json")
public String save(@RequestBody JavaBean javaBean) {
}
@ResponseBody
@RequestMapping(
method=RequestMethod.POST,
consumes="application/json")
public String save(@RequestBody JavaBean javaBean) {
}
@ResponseBody
@RequestMapping(
method=RequestMethod.GET
header="Accept=application/json")
public JavaBean get() {
}
@ResponseBody
@RequestMapping(
method=RequestMethod.GET,
produces="application/json")
public JavaBean get() {
}
@RequestMapping(value="/people/{firstName}/{lastName}/SSN")
public void search(Person person) {
// person.getFirstName() is populated
// person.getLastName() is populated
}
@RequestMapping("/apps/edit/{slug}")
public String editForm(@PathVariable String slug){
// No need to add "slug" to the model
}
@RequestMapping(
value="/{year}/{month}/{slug}/rooms",
method=RequestMethod.POST)
public String createRoom() {
// No need to add "year", "month", & "slug"
// They will be used in RedirectView
return "redirect:/{year}/{month}/{slug}";
}
@RequestMapping(
value="/{account}",
method = RequestMethod.PUT)
public String update(@ModelAttribute Account account) {
// Account was retrieved from DB
// via Converter<String, Account>
}
// "redirect:/account/" + account.getId()
@RequestMapping(method=POST)
public String save(Account account, RedirectAttributes redirectAttrs){
redirectAttrs.addAttribute("id", account.getId);
return "redirect:/action/{id}";
}
@RequestMapping(method=POST)
public String save(Entity entity, RedirectAttributes redirectAttrs){
redirectAttrs.addFlashAttribute("message", "Success!");
return "redirect:/show";
}
alert('${message}');
@RequestMapping(method = RequestMethod.POST)
public void create(@RequestParam("file") MultipartFile file){
InputStream in = file.getInputStream();
// ...
}
@RequestMapping(method = RequestMethod.POST)
public void create(@RequestParam("file") Part part){
InputStream in = part.getInputStream();
// ...
}
@RequestMapping(
method = RequestMethod.POST,
consumes = { "multipart/form-data" })
public ResponseEntity<Object> void create(
@RequestPart("json-data") @Valid JavaBean javaBean,
@RequestPart("file-data") MultipartFile file) {
// ...
}
// /book/search?title=Noje.js
UriComponentsBuilder.fromPath("/{product}/search")
.query("title={title}")
.build()
.expand("book", "Node.js")
.encode().toUriString();
◎ HDIV Integration
- In order to solve web application vulnerabilities we have created HDIV (HTTP Data Integrity Validator) open source project.
- Java Web Application Security Framework
- interface RequestDataValueProcessor
(그림 출처: http://chanwook.tistory.com/784)
- BeanNameUrlHandlerMapping (1.0)
- SimpleUrlHandlerMapping (1.0)
- ControllerClassNameHandlerMapping (2.0)
- DefaultAnnotationHandlerMapping (2.5)
- ControllerBeanNameHandlerMapping (2.5.3)
- SimpleControllerHandlerAdapter (1.0)
- SimpleServletHandlerAdapter (1.1.5)
- HttpRequestHandlerAdapter (2.0)
- AnnotationMethodHandlerAdapter (2.5)
-
AnnotationMethodHandlerExceptionResolver
@ExceptionHandler
-
ResponseStatusExceptionResolver
-
DefaultHandlerExceptionResolver
-
SimpleMappingExceptionResolver
(그립 출처 : http://rstoyanchev.github.com/spring-mvc-31-update/#25)
- HandlerMapping : RequestMappingHandlerMapping
- HandlerAdapter : RequestMappingHandlerAdapter
- HandlerExceptionResolver : ExceptionHandlerExceptionResolver
- HandlerMethod
- HandlerMethodArgumentResolver
- HandlerMethodReturnValueHandler