Anti Orgla, Nortal AS Spring Framework 08.04.2014.

Post on 12-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

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?

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 ControlDependency Injection

static vs runtime

abstractions vs implementations

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

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

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

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.

<bean id="userDao" class="example.JdbcUserDao" />

<bean id="userService" class="example.UserService"><property name="userDao" ref="userDao" />

</bean>

MVC – What was that?

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

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

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

Let’s write some MVC!

Code demo..

Built-in conversion

There are some standard built-in converters.

@RequestMapping("/foo")public String foo(

@RequestParam("param1") int intParam,

@RequestParam("param2") 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 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) {

... }}

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 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

top related