26
Anti Orgla, Nortal AS Spring Framework 08.04.2014

Anti Orgla, Nortal AS Spring Framework 08.04.2014

Embed Size (px)

Citation preview

Page 1: Anti Orgla, Nortal AS Spring Framework 08.04.2014

Anti Orgla, Nortal AS

Spring Framework

08.04.2014

Page 2: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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

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

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

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

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

Spring Framework

Inversion of ControlDependency Injection

static vs runtime

abstractions vs implementations

Page 8: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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

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

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

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

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

MVC – What was that?

http://java.sun.com/blueprints/patterns/MVC-detailed.html

Page 14: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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

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

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

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

Let’s write some MVC!

Code demo..

Page 19: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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

Type conversion

You can also define your own PropertyEditors

PropertyEditorSupport implements PropertyEditor

Page 21: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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

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

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

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

Code examples..

Code demo..

Page 26: Anti Orgla, Nortal AS Spring Framework 08.04.2014

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