Topics: - What is an Enterprise Java Framework - Overview of popular Enterprise Java Frameworks - The most popular Enterprise Java framework - Spring Framework - Conclusion
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• A software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software.
• A software framework is a universal, reusable software platform used to develop applications, products and solutions.
• Software Frameworks include support programs, compilers, code libraries, an application programming interface (API) and tool sets that bring together all the different components to enable development of a project or solution.
1. Inversion of control - In a framework, unlike in libraries or normal user applications, the overall program's flow of control is not dictated by the caller, but by the framework
2. Default behavior - A framework has a default behavior. This default behavior must actually be some useful behavior and not a series of no-ops.
3. Extensibility - A framework can be extended by the user usually by selective overriding or specialized by user code providing specific functionality.
4. Non-modifiable framework code - The framework code, in general, is not allowed to be modified. Users can extend the framework, but not modify its code.
Frameworks contain key distinguishing features that separate them from normal libraries:
An application framework consists of a software framework used by software developers to implement the standard structure of an application for a specific development environment (such as a standalone program or a web application).
Application frameworks became popular with the rise of multi-tiers enterprise applications since these tended to promote a standard structure for applications. Programmers find it much simpler to program when using a standard framework, since this defines the underlying code structure of the application in advance. Developers usually use object-oriented programming techniques to implement frameworks such that the unique parts of an application can simply inherit from pre-existing classes in the framework.
An application framework designed for the implementation of enterprise class applications.
In addition to an application framework, an enterprise application framework should supports the development of a layered architecture, and provide services that can address the requirements on performance, scalability, and availability.
Services: - Security - Transaction (local and global transactions) - Caching - Task scheduling and asynchronous task execution - Management and monitoring - Testing - and many more …
At its core, IOC, and therefore DI, aims to offer a simpler mechanism for provisioning component dependencies (often referred to as an object’s collaborators) and managing these dependencies throughout their life cycles. A component that requires certain dependencies is often referred to as the dependent object or, in the case of IOC, the target.
Contextualized Dependency Lookup (CDL) is similar, in some respects, to Dependency Pull, but in CDL, lookup is performed against the container that is managing the resource, not from some central registry, and it is usually performed at some set point.
IOC Container Configuration of application components and lifecycle management of Java objects, done mainly via Dependency Injection
AOP Enables implementation of cross-cutting routines
Data Access Working with relational database management systems on the Java platform using JDBC and object-relational mapping tools and with NoSQL databases
Spring Data projects
Transaction Management
Unifies several transaction management APIs (JDBC, JPA, JTA, etc.) and coordinates transactions for Java objects.
Model-view-controller
An HTTP- and servlet-based framework providing hooks for extension and customization for web applications and RESTful Web Services.
Authentication & Authorization
Configurable security processes that support a range of standards, protocols, tools and practices via the Spring Security sub-project
Spring Security
Remote Management
Configurative exposure and management of Java objects for local or remote configuration via JMX
Messaging Configurative registration of message listener objects for transparent message-consumption from message queues via JMS, improvement of message sending over standard JMS APIs
Testing support classes for writing unit tests and integration tests
Java-based Configuration Use Java classes to configure Spring’s ApplicationContext (Spring JavaConfig was merged into the core Spring Framework since 3.0).
3.0
Embedded JDBC DataSource
Embedded database support (by using the <jdbc:embedded-database id="dataSource" type="H2"> tag)
3.1
Validation with Type Conversion and Formatting
Spring 3 introduce a new type conversion and formatting system, and support of JSR303 Bean Validation API.
3.0
Persistence with Spring Data JPA
Spring Data JPA’s Repository abstraction greatly simplifies the development of persistence layer with JPA.
3.0
Spring MVC Improved support of RESTful-WS. 3.1
Spring Expression Language A powerful expression language that supports querying and manipulating an object graph at run time.
3.0
Profiles A profile instructs Spring to configure only the ApplicationContext that was defined when the specified profile was active
3.1
Cache Abstraction A caching abstraction layer that allows consistent use of various caching solutions with minimal impact on the code.
3.1
TaskScheduler Abstraction Provides a simple way to schedule tasks and supports most typical requirements.
Spring Security Configurable security processes that support a range of standards, protocols, tools and practices
3.1.2
Spring Data An umbrella project includes many modules that simplifies the development of persistence layer with various data sources (e.g. JPA, NoSQL, JDBC, Hadoop, etc.)
1.X
Spring Batch Provides a standard template and framework for implementing and executing various kinds of batch jobs in an enterprise environment.
2.1.8
Spring Integration Provides an excellent integration environment for enterprise applications.
2.1.3
Spring WebFlow Building on top of Spring MVC’s foundation, Spring Web Flow was designed to provide advanced support for organizing the flows inside a web application.
2.3.1
Spring Mobile An extension to Spring MVC that aims to simplify the development of mobile web applications.
1.0.0
Spring Roo A rapid application development solution for Spring-based enterprise applications
1.2.2
SpringSource Tool Suite
An IDE tools with Eclipse and Spring IDE bundled, together witn numerous tools for developing Spring applications.
public interface MessageRenderer { public void render(); public void setMessageProvider(MessageProvider provider); public MessageProvider getMessageProvider(); }
public class StandardOutMessageRenderer implements MessageRenderer { private MessageProvider messageProvider = null; public void render() { if (messageProvider == null) { throw new RuntimeException( "You must set the property messageProvider of class:"+ StandardOutMessageRenderer.class.getName()); } System.out.println(messageProvider.getMessage()); } public void setMessageProvider(MessageProvider provider) { this.messageProvider = provider; } public MessageProvider getMessageProvider() { return this.messageProvider; } }
Interface and implementation of a dependent object.
• Configure Spring beans for different application environments
• Platform (dev, sit, uat, prd)
• Database (oracle, mysql)
• Application specific (kindergarten, highschool)
• When setting active profile with JVM argument, the same application file (e.g. jar, war, ear file, etc.) can be deployed to different environments, which simplifies the deployment process.
Joinpoints A well-defined point during the execution of your application. Typical examples of joinpoints include a call to a method, the Method Invocation itself, class initialization, and object instantiation. Joinpoints are a core concept of AOP and define the points in your application at which you can insert additional logic using AOP.
Advice The code that is executed at a particular joinpoint is the advice. There are many different types of advice, such as before, which executes before the joinpoint, and after, which executes after it.
Pointcuts A pointcut is a collection of joinpoints that you use to define when advice should be executed. A typical joinpoint is a Method Invocation. A typical pointcut is the collection of all Method Invocations in a particular class.
Aspects An aspect is the combination of advice and pointcuts. This combination results in a definition of the logic that should be included in the application and where it should execute.
Weaving This is the process of actually inserting aspects into the application code at the appropriate point. Mechanisms include compile-time weaving and load-time weaving.
Target An object whose execution flow is modified by some AOP process is referred to as the target object. Often you see the target object referred to as the advised object.
Before Using before advice, you can perform custom processing before a joinpoint executes. Because a joinpoint in Spring is always a Method Invocation, this essentially allows you to perform preprocessing before the method executes.
After returning After-returning advice is executed after the Method Invocation at the joinpoint has finished executing and has returned a value.
After (finally) After-returning advice is executed only when the advised method completes normally. However, the after (finally) advice will be executed no matter the result of the advised method. The advice is executed even when the advised method fails and an exception is thrown.
Around In Spring, around advice is modeled using the AOP Alliance standard of a method interceptor. Your advice is allowed to execute before and after the Method Invocation, and you can control the point at which the Method Invocation is allowed to proceed. You can choose to bypass the method altogether if you want, providing your own implementation of the logic.
Throws Throws advice is executed after a Method Invocation returns, but only if that invocation threw an exception. It is possible for a throws advice to catch only specific exceptions, and if you choose to do so, you can access the method that threw the exception, the arguments passed into the invocation, and the target of the invocation.
The MyDependency Class: public class MyDependency { public void foo(int intValue) { System.out.println("foo(int): " + intValue); } public void bar() { System.out.println("bar()"); } }
The MyBean Class: public class MyBean { private MyDependency dep; public void execute() { dep.foo(100); dep.foo(101); dep.bar(); } public void setDep(MyDependency dep) { this.dep = dep; } }
Define named queries for the Contact domain class:
@Table(name = "contact") @NamedQueries({ @NamedQuery(name="Contact.findAll", query="select c from Contact c"), @NamedQuery(name="Contact.findById", query="select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h where c.id = :id"), @NamedQuery(name="Contact.findAllWithDetail", query="select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h") }) public class Contact implements Serializable { // Other code omitted }
public interface ContactService { // Find all contacts public List<Contact> findAll(); // Find all contacts with telephone and hobbies public List<Contact> findAllWithDetail(); // Find a contact with details by id public Contact findById(Long id); // Insert or update a contact public Contact save(Contact contact); // Delete a contact public void delete(Contact contact); }
Spring Data – Modern Data Access for Enterprise Java
Category Sub-Project Description
Common Infrastructure
Commons Provides shared infrastructure for use across various data access projects.
RDBMS JPA Spring Data JPA - Simplifies the development of creating a JPA-based data access layer
JDBC Extensions Support for Oracle RAC, Advanced Queuing, and Advanced datatypes. Support for using QueryDSL with JdbcTemplate.
Big Data Apache Hadoop The Apache Hadoop project is an open-source implementation of frameworks for reliable, scalable, distributed computing and data storage.
Data Grid GemFire VMware vFabric GemFire is a distributed data management platform providing dynamic scalability, high performance, and database-like persistence. It blends advanced techniques like replication, partitioning, data-aware routing, and continuous querying.
HTTP REST Spring Data REST - Perform CRUD operations of your persistence model using HTTP and Spring Data Repositories.
Key Value Store Redis Redis is an open source, advanced key-value store.
Document Store MongoDB MongoDB is a scalable, high-performance, open source, document-oriented database.
public interface ContactService { // Find all contacts public List<Contact> findAll(); // Find contacts by first name public List<Contact> findByFirstName(String firstName); // Find contacts by first name and last name public List<Contact> findByFirstNameAndLastName(String firstName, String lastName); }
ExpressionParser parser = new SpelExpressionParser(); double value = parser.parseExpression(“6.0221415E+23”).getValue(Double.class); int value = parser.parseExpression(“0x7FFFFFFF”).getValue(Integer.class); Date someDate = parser.parseExpression(“’2012/07/09’”).getValue(Date.class); boolean result = parser.parseExpression(“true”).getValue(); Object someObj = parser.parseExpression(“null”).getValue();
Evaluation Expressions Against an Object Instance Expression is evaluated against a specific object instance (called the root object) 2 options
Use EvaluationContext (root object is the same for every invocation) Pass the object instance to getValue() method
Use EvaluationContext:
ExpressionParser parser = new SpelExpressionParser(); Contact contact = new Contact("Scott", "Tiger", 30); // Contact(firstName, lastName, age) // SpEL against object with EvaluationContext Expression exp = parser.parseExpression("firstName"); EvaluationContext context = new StandardEvaluationContext(contact); // contact is the root object String firstName = exp.getValue(context, String.class);
Calling getValue() and pass in the root object:
ExpressionParser parser = new SpelExpressionParser(); // SpEL against object by calling getValue() contact.setFirstName("Clarence"); firstName = exp.getValue(contact, String.class); // pass in contact as the root object
A model represents the business data as well as the “state” of the application within the context of the user. For example, in an e-commerce web site, the model will include the user profile information, shopping cart data, and order data if users purchase goods on the site.
View
This presents the data to the user in the desired format, supports interaction with users, and supports client-side validation, i18n, styles, and so on.
Controller
The controller handles requests for actions performed by users in the frontend, interacting with the service layer, updating the model, and directing users to the appropriate view based on the result of execution.