Anti Orgla, Nortal AS Spring Framework
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?
Web application framework Typically frameworks provide libraries: Database Access Session management Request management Transaction management UI Templates Cache, Security, Ajax, Web Services, etc…
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(); }
Easier way? public static String getName(int id) { return jdbcTemplate.queryForObject("select name from user_info where id = ?", String.class, id); }
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?
Spring Framework Inversion of Control Dependency Injection static vs runtime abstractions vs implementations
Dependency injection public interface UserDao { User getByUserName(String name); public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB private UserDao userDao; Universal abstraction
Dependency injection public interface UserDao { User getByUserName(String name); public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB private UserDao userDao; One possible implementation. Spring will create and register it
Dependency injection public interface UserDao { User getByUserName(String name); public class JdbcUserDao implements UserDao { public User getByUserName(String name) { // load user from DB private UserDao userDao; Injection is made as an abstraction
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!
XML based configuration Bean can also be defined and injected in XML.
MVC – What was that?
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
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.
Spring MVC IoC again – framework handles the infrastructure, you focus on application specific things.
Architecture - DispatcherServlet
Let’s write some MVC! Code demo..
Built-in conversion There are some standard built-in public String int long longParam) {...
Type conversion You can also define your own PropertyEditors PropertyEditorSupport implements PropertyEditor
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 }
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
Register and public class MyController public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(DateRange.class, new DateRangeEditor()); public String DateRange range) {... }
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.
Code examples.. Code demo..
Sources of wisdom Spring has great documentation: Java BluePrints - Model-View-Controller html html Model-View-Controller Inversion of control