anti orgla, nortal as spring framework 08.04.2014
TRANSCRIPT
![Page 1: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/1.jpg)
Anti Orgla, Nortal AS
Spring Framework
08.04.2014
![Page 2: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/2.jpg)
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?
![Page 3: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/3.jpg)
Web application framework
Typically frameworks provide libraries:
Database Access
Session management
Request management
Transaction management
UI Templates
Cache, Security, Ajax, Web Services, etc…
![Page 4: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/4.jpg)
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();
}
}
}
![Page 5: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/5.jpg)
Easier way?
public static String getName(int id) {
return jdbcTemplate.queryForObject("select name from user_info where id = ?", String.class, id);
}
![Page 6: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/6.jpg)
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?
![Page 7: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/7.jpg)
Spring Framework
Inversion of ControlDependency Injection
static vs runtime
abstractions vs implementations
![Page 8: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/8.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
Universal abstraction
![Page 9: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/9.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
One possible implementation.
Spring will create and register it
![Page 10: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/10.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
Injection is made as an abstraction
![Page 11: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/11.jpg)
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!
![Page 12: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/12.jpg)
XML based configuration
Bean can also be defined and injected in XML.
<bean id="userDao" class="example.JdbcUserDao" />
<bean id="userService" class="example.UserService"><property name="userDao" ref="userDao" />
</bean>
![Page 13: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/13.jpg)
MVC – What was that?
http://java.sun.com/blueprints/patterns/MVC-detailed.html
![Page 14: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/14.jpg)
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
![Page 15: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/15.jpg)
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.
![Page 16: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/16.jpg)
Spring MVC
IoC again – framework handles the infrastructure, you focus on application specific things.
![Page 17: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/17.jpg)
Architecture - DispatcherServlet
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
![Page 18: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/18.jpg)
Let’s write some MVC!
Code demo..
![Page 19: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/19.jpg)
Built-in conversion
There are some standard built-in converters.
@RequestMapping("/foo")public String foo(
@RequestParam("param1") int intParam,
@RequestParam("param2") long longParam) {...
![Page 20: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/20.jpg)
Type conversion
You can also define your own PropertyEditors
PropertyEditorSupport implements PropertyEditor
![Page 21: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/21.jpg)
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
}}
![Page 22: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/22.jpg)
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
![Page 23: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/23.jpg)
Register and use
@Controllerpublic class MyController {
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(DateRange.class, new DateRangeEditor()); }
@RequestMapping(value="/dateRange") public String dateRange(@RequestParam("range") DateRange range) {
... }}
![Page 24: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/24.jpg)
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.
![Page 25: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/25.jpg)
Code examples..
Code demo..
![Page 26: Anti Orgla, Nortal AS Spring Framework 08.04.2014](https://reader035.vdocuments.site/reader035/viewer/2022070409/56649e985503460f94b9b082/html5/thumbnails/26.jpg)
Sources of wisdomSpring has great documentation: http://www.springsource.org/spring-framework#documentation
Java BluePrints - Model-View-Controllerhttp://www.oracle.com/technetwork/java/mvc-detailed-136062.html
Model-View-Controllerhttp://www.oracle.com/technetwork/java/mvc-140477.html
Inversion of controlhttp://en.wikipedia.org/wiki/Inversion_of_control