[KSUG 세미나 2012 - Part 1] 스프링은 지금 : Spring 3.1에 대한 기능 소개와 @MVC 이야기
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/main
.gitignore
README.md
pom.xml

README.md

[KSUG 세미나 2012 - Part 1] Spring 3.1에 대한 기능 요약과 @MVC 이야기



Spring Framework 3.1: Key Themes

  • 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

◎ Environment profiles for bean definitions

✔ Environment Abstraction

  • org.springframework.core.env.PropertyResolver
  • org.springframework.core.env.Environment

✔ Environment Profiles && Configuration

  • 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

◎ Java-based application configuration

  • @Configuration

  • @ComponentScan

  • @Bean

  • @Enable*

    @EnableTransactionManagement

    @EnableWebMvc

  • @PropertySource("classpath:META-INF/app.properties")

  • @ActiveProfiles("dev")

◎ 'c:' namespace

<bean class="" c:age="10" c:name="myName"/>

<bean class="" c:name-ref="nameBean" c:spouse-ref="spouseBean"/>

◎ Cache abstraction & declarative caching

  • 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

◎ Explicit support for Servlet 3.0

  • 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

◎ @MVC processing & flash attributes

  • New @MVC Infrastructure
  • FlashMap support and FlashMapManager abstraction

◎ Refined JPA support

  • Package scanning without persistence.xml
  • Consistent JPA setup by persistence unit name

◎ Hibernate 4.0 & Quartz 2.0

◎ Support for Java SE 7

  • 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

◎ Java Config

✔ support for XML-free web application setup (no web.xml)

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");
    }

}

✔ Simple Starting Point for WebApplicationContext

// Equivalent to <mvc:annotation:driven/>

@EnableWebMvc
@Configuration
public class WebConfig {

}

✔ Built-in Customizations

@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

}

◎ Consumes/Produces

✔ Input Media Type

@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) {

}

✔ Output Media Type

@ResponseBody
@RequestMapping(
    method=RequestMethod.GET
    header="Accept=application/json")
public JavaBean get() {

}

@ResponseBody
@RequestMapping(
    method=RequestMethod.GET, 
    produces="application/json") 
public JavaBean get() {

}

◎ URI Variables

✔ Data Binding & URI Variables

@RequestMapping(value="/people/{firstName}/{lastName}/SSN")
public void search(Person person) {
    // person.getFirstName() is populated
    // person.getLastName()  is populated
}

✔ Rendering & Path Variables

@RequestMapping("/apps/edit/{slug}")
public String editForm(@PathVariable String slug){
    // No need to add "slug" to the model
}

✔ "redirect:" & URI Vars

@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}";
}

✔ Model Attributes & URI Vars

@RequestMapping(
        value="/{account}", 
        method = RequestMethod.PUT)
public String update(@ModelAttribute Account account) {

    // Account was retrieved from DB 
    // via Converter<String, Account>

}

◎ Redirect & Flash Attributes

✔ RedirectAttributes

// "redirect:/account/" + account.getId()

@RequestMapping(method=POST)
public String save(Account account, RedirectAttributes redirectAttrs){

  redirectAttrs.addAttribute("id", account.getId);

  return "redirect:/action/{id}";

}

✔ Flash Attributes

@RequestMapping(method=POST)
public String save(Entity entity, RedirectAttributes redirectAttrs){

	redirectAttrs.addFlashAttribute("message", "Success!");

	return "redirect:/show";
}
alert('${message}');

◎ Multipart Request Support

✔ org.springframework.web.multipart.MultipartFile

@RequestMapping(method = RequestMethod.POST)
public void create(@RequestParam("file") MultipartFile file){

    InputStream in = file.getInputStream();

    // ...

}

✔ javax.servlet.http.Part

@RequestMapping(method = RequestMethod.POST)
public void create(@RequestParam("file") Part part){

    InputStream in = part.getInputStream();

    // ...

}

✔ @RequestPart

@RequestMapping(
    method = RequestMethod.POST, 
    consumes = { "multipart/form-data" })
public ResponseEntity<Object> void create(
    @RequestPart("json-data") @Valid JavaBean javaBean, 
    @RequestPart("file-data") MultipartFile file) {

   // ...

} 

◎ UriComponents / UriComponentsBuilder

// /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

◎ New @MVC Infrastructure

✔ DispatcherServlet과 Spring 3.0.x MVC 아키텍쳐

MVC 아키텍쳐

(그림 출처: http://chanwook.tistory.com/784)

➤ HandlerMapping 구현체 목록

  • BeanNameUrlHandlerMapping (1.0)
  • SimpleUrlHandlerMapping (1.0)
  • ControllerClassNameHandlerMapping (2.0)
  • DefaultAnnotationHandlerMapping (2.5)
  • ControllerBeanNameHandlerMapping (2.5.3)

➤ HandlerAdapter 구현체 목록

  • SimpleControllerHandlerAdapter (1.0)
  • SimpleServletHandlerAdapter (1.1.5)
  • HttpRequestHandlerAdapter (2.0)
  • AnnotationMethodHandlerAdapter (2.5)

➤ HandlerExceptionResolver 구현체 목록

  • AnnotationMethodHandlerExceptionResolver

    @ExceptionHandler

  • ResponseStatusExceptionResolver

  • DefaultHandlerExceptionResolver

  • SimpleMappingExceptionResolver

✔ New @MVC Infrastructure

New @MVC Infrastructure

(그립 출처 : http://rstoyanchev.github.com/spring-mvc-31-update/#25)

➤ New @MVC Class

  • HandlerMapping : RequestMappingHandlerMapping
  • HandlerAdapter : RequestMappingHandlerAdapter
  • HandlerExceptionResolver : ExceptionHandlerExceptionResolver

➤ New Abstractions

  • HandlerMethod
  • HandlerMethodArgumentResolver
  • HandlerMethodReturnValueHandler



참고자료