Presentation is loading. Please wait.

Presentation is loading. Please wait.

Anti Orgla, Nortal AS Spring Framework 08.04.2014.

Similar presentations


Presentation on theme: "Anti Orgla, Nortal AS Spring Framework 08.04.2014."— Presentation transcript:

1 Anti Orgla, Nortal AS Spring Framework 08.04.2014

2 Web appication framework? How do you handle your requests? How do you access your database? How do you manage your session? Code reuse? Maybe it’s already been done?

3 Web application framework Typically frameworks provide libraries: Database Access Session management Request management Transaction management UI Templates Cache, Security, Ajax, Web Services, etc…

4 How to get a String from DB? public static String getName(Connection con, int id) { Statement stmt = null; String query = "select name from user_info where id = ?"; try { stmt = con.createStatement(); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(query); if(rs.next()) { return rs.getString("name"); }else{ return null; } } catch (SQLException e ) { //handle the exception } finally { if (stmt != null) { stmt.close(); }

5 Easier way? public static String getName(int id) { return jdbcTemplate.queryForObject("select name from user_info where id = ?", String.class, id); }

6 Spring Framework One of the most popular Open source application framework Very flexible Available since 2002 (v4.0 dets.2013) Did I say it is very flexible?

7 Spring Framework Inversion of Control Dependency Injection static vs runtime abstractions vs implementations

8 Dependency injection public interface UserDao { User getByUserName(String name); } @Repository public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB } @Resource private UserDao userDao; Universal abstraction

9 Dependency injection public interface UserDao { User getByUserName(String name); } @Repository public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB } @Resource private UserDao userDao; One possible implementation. Spring will create and register it

10 Dependency injection public interface UserDao { User getByUserName(String name); } @Repository public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB } @Resource private UserDao userDao; Injection is made as an abstraction

11 IoC in Spring Spring handles the infrastructure (bean creation, dependency lookup and injection) Developer focuses on application specific logic You can describe how your application is coupled, not couple it yourself!

12 XML based configuration Bean can also be defined and injected in XML.

13 MVC – What was that? http://java.sun.com/blueprints/patterns/MVC-detailed.html

14 Model-View-Controller Controller: executes business logic, data actions etc. Forms a model object and passes it to view for rendering Model: object for passing data between controller and view View: takes model data from the controller and presents it to the end user

15 Model-View-Controller Separation of different layers An application might have more than one user interface Different developers may be responsible for different aspects of the application.

16 Spring MVC IoC again – framework handles the infrastructure, you focus on application specific things.

17 Architecture - DispatcherServlet http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

18 Let’s write some MVC! Code demo..

19 Built-in conversion There are some standard built-in converters. @RequestMapping("/foo") public String foo( @RequestParam("param1") int intParam, @RequestParam("param2") long longParam) {...

20 Type conversion You can also define your own PropertyEditors PropertyEditorSupport implements PropertyEditor

21 Custom types public class DateRange { private Date start; private Date end; public DateRange(Date start, Date end) { this.start = start; this.end = end; } public int getDayDifference() { // calculate }

22 Custom type editor public class DateRangeEditor extends PropertyEditorSupport { private DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); public void setAsText(String text) throws IllegalArgumentException { String[] parts = text.split("-"); Date start = dateFormat.parse(parts[0]); Date end = dateFormat.parse(parts[1]); setValue(new DateRange(start, end)); } public String getAsText() { DateRange dateRange = (DateRange) getValue(); return dateFormat.format(dateRange.getStart()) + "-" + dateFormat.format(dateRange.getEnd()); } String to custom type Custom type to String

23 Register and use @Controller public class MyController { @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(DateRange.class, new DateRangeEditor()); } @RequestMapping(value="/dateRange") public String dateRange(@RequestParam("range") DateRange range) {... }

24 Non-intrusive Very important feature of a framework is non-intrusiveness. Spring MVC normally lets you do stuff according to MVC pattern. But it doesn’t prevent you from violating MVC if you really want to.

25 Code examples.. Code demo..

26 Sources of wisdom Spring has great documentation: http://www.springsource.org/spring-framework#documentation http://www.springsource.org/spring-framework#documentation Java BluePrints - Model-View-Controller http://www.oracle.com/technetwork/java/mvc-detailed- 136062.html http://www.oracle.com/technetwork/java/mvc-detailed- 136062.html Model-View-Controller http://www.oracle.com/technetwork/java/mvc-140477.html http://www.oracle.com/technetwork/java/mvc-140477.html Inversion of control http://en.wikipedia.org/wiki/Inversion_of_control http://en.wikipedia.org/wiki/Inversion_of_control


Download ppt "Anti Orgla, Nortal AS Spring Framework 08.04.2014."

Similar presentations


Ads by Google