Spring Framework DI Foo f new FooCleopatra Bar
Spring Framework Чистяков Сергей
Внедрение зависимостей (DI) Пример кода Foo f = new Foo("Cleopatra"); Bar b = new Bar("Arthur", 26); b. set. Foo(f); 8
Аспектно-ориентированное программирование (АОП) Пример кода • @Before – Run before the method execution Pointcut (Aspect. J Pointcut Expression Language) • @After – Run after the method returned a result • @After. Returning – Run after the method returned a result, intercept the returned result as well. • @Around – Run around the method execution, combine all three advices above. • @After. Throwing – Run after the method throws an exception Join Point Advice 14
Модули в Spring Проекты • Spring XD • Spring Data • Spring Integration • Spring Batch • Spring Security • Spring Cloud • Spring AMQP • Spring Grails • Spring Mobile • Spring Social • Spring for Android • Spring Web Flow • Spring LDAP • Spring Groovy • Spring Hateoas • Spring Security OAuth 18
Spring MVC The Dispatcher. Servlet • Фреймворк Spring Web model-view-controller (MVC) построен вокруг Dispatcher. Servlet, который распределяет запросы по обработчикам 21
Spring MVC Web. xml • You need to map requests that you want the Dispatcher. Servlet to handle, by using a URL mapping in the web. xml file. <? xml version="1. 0" encoding="UTF-8"? > <web-app> <display-name>Example</display-name> <context: component-scan base-package="com. proj. web" /> <context-param> <param-name>context. Config. Location</param-name> <param-value> /WEB-INF/context/example-general-context. xml </param-value> </context-param> <listener-class> org. springframework. web. context. Context. Loader. Listener </listener-class> </listener> <servlet-name>spring</servlet-name> <servlet-class> org. springframework. web. servlet. Dispatcher. Servlet </servlet-class> <init-param> <param-name>context. Config. Location</param-name> <param-value>/WEB-INF/example-servlet. xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 22
Spring MVC Servlet. xml • Now, let us check the required configuration for example-servlet. xml file, placed in your web application's Web. Content/WEB-INF directory: <context: component-scan base-package="com. proj. web. controller" /> <bean id="view. Resolver" class="org. springframework. web. servlet. view. Url. Based. View. Resolver"> <property name="view. Class" value="org. springframework. web. servlet. view. Jstl. View" /> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=". jsp" /> </bean> <mvc: annotation-driven /> </beans> 23
Spring MVC Controller & View • Home. Controller. java: • Home. jsp @Controller public class Home. Controller { @Request. Mapping(value = { "/", "/home" }, method = Request. Method. GET) public String show. Home. Page( Model. Map model) { model. add. Attribute("message", "Hello Spring MVC Framework!"); <html> <head> <title>Here is home page</title> </head> <body> <h 2>${message}</h 2> </body> </html> return "home" ; } } 24
Spring RESTful Services Do not forget to implement serializable in your entity 28
Spring RESTful Services • This code uses Spring 4’s new @Rest. Controller annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @Response. Body rolled together. • The Greeting object must be converted to JSON. Thanks to Spring’s HTTP message converter support, you don’t need to do this conversion manually. 29
Spring Security Authentication & Authorization 31
Spring Security Authentication & Authorization 32
Spring Security Authentication & Authorization Lastly, forcing application to use secure channel (https) is easy to implement in Spring Security. 33
Spring Security Authentication & Authorization Spring special naming for Spring supported Authentication 34
Spring Security Authentication & Authorization • The authorize & authentication tag 35
Spring Security Authentication & Authorization • Вы можете получить доступ до Authentication object из вашего MVC контролера (используя Security. Context. Holder. get. Context(). get. Authentication()) и добавить данные прямо в вашу модель для отображения. 36
Spring Security Authentication & Authorization • Authorization with annotations in RESTful Web Service 37
Spring Test Unit Testing 39
Spring Test Unit Testing 40
Spring Test Unit Testing 41
Spring Test Integration Testing 42
Spring Test Integration Testing 43
Spring Test Integration Testing 44
Ссылки http: //docs. spring. io/spring/docs/current/spring-framework-reference/html/ http: //projects. spring. io/spring-security/ http: //www. mkyong. com/tutorials/spring-mvc-tutorials/ http: //www. mkyong. com/tutorials/spring-security-tutorials/ http: //www. tutorialspoint. com/spring/ http: //www. mkyong. com/tutorials/spring-tutorials/ http: //www. slideshare. net/rstoya 05/testing-web-apps-with-spring-framework 32 • http: //www. petrikainulainen. net/programming/spring-framework/integrationtesting-of-spring-mvc-applications-security/ • • 45
- Slides: 46