New features of Spring 2.5: Configuration simplification and annotation-based features

Source: Internet
Author: User
Tags java se

Brief introduction

From the very beginning, the Spring framework adheres to its purpose: simplifying enterprise application development while providing powerful, non-intrusive solutions to complex problems. Spring 2.0, released a year ago, pushes these topics to a new level. The support of XML schemas and the use of custom namespaces greatly reduce the xml-based configuration. Developers using Java 5 and newer Java can now take advantage of the spring libraries implanted with new language features such as generics (generic) and annotations. Most recently, the tight integration with the ASPECTJ expression language makes it possible to add a non-intrusive way to grouping the well-defined spring management objects.

The new spring 2.5 continues to adhere to this trend, especially for developers who use Java 5 or newer versions of Java to provide further simplified and powerful new features. These new features include annotation-driven dependency injection (Annotation-driven Dependency injection), the use of annotations instead of XML metadata to automatically detect the spring component on classpath, and annotations support the lifecycle approach, A new web controller model maps requests to annotated methods, supports new additions to the junit4,spring XML namespaces in the test framework, and so on.

This article is the first of a series of 3 articles exploring these new features. This article will focus on simplified configurations and new annotation-based features that are added to the Spring application context (application) core, while the second article will cover the features available to the Web tier, and the final article will focus on new performance for integration and testing. The examples cited in this series of three articles are based on the spring petclinic application paradigm. This example was recently refactored to showcase spring's newest features and is included in the release download package for Spring 2.5, which downloads Web downloads from the spring Framework. Looking at the "Readme.txt" file in the "Samples/petclinic" directory to learn about how to build and deploy petclinic applications, the best way to master the new technology mentioned in this article might be to experiment with the features shown in the Petclinic application.

Spring supports JSR-250 annotations

The public annotations of the Java platform (Common annotations for the Java Platform) are introduced in the Java EE5, and the public annotations are included from Java SE 61. In May 2006, the BEA system announced their collaboration with INTERFACE21 on a project called Pitchfork, which provides implementations of the Java EE 5 programming model based on spring, including support for injection (injection), interception ( interception) and transaction processing (transactions) JSR-250 annotations and EJB 3 annotations (JSR-220). In version 2.5, the core of the spring framework now supports the following JSR-250 annotations:

@ Resource

@ postconstruct

@ Predestroy

With spring, these annotations can be used in any development environment-whether there is an application server or even an integrated test environment. Activating such support is just registering a separate spring post-processor thing:

class= "Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

@Resource annotations

@Resource annotations are used to activate dependency injection of a named resource (named Resource), which is typically converted to an object bound to the Jndi context in Java EE applications. Spring does support using @resource to resolve objects through Jndi lookup, which by default is injected with a spring management object that has a "bean name" that matches the name provided by the @resource annotation. In the following example, spring passes a reference to the annotated setter method for the spring management object with the bean named "DataSource."

@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

It is also possible to use the @resource annotation directly as a field (field). By not exposing the setter method, the code becomes more compact and provides additional benefits that cannot be modified by the domain. As will be shown below, @Resource annotations do not even require an explicit string value, and the domain name is treated as a default value without providing any value.

@Resource
private DataSource dataSource; // inject the bean named 'dataSource'

When applied to the setter method, the default name is derived from the corresponding attribute, in other words, the method named ' Setdatasource ' is used to handle the attribute named ' DataSource '.

private DataSource dataSource;
@Resource
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.