Spring Framework
Spring Framework
Reference Documentation
3.0
Table of Contents
1
2.1. Java 5
2.2. Improved documentation
2.3. New articles and tutorials
2.4. New module organization and build system
2.5. Overview of new features
2.5.1. Core APIs updated for Java 5
2.5.2. Spring Expression Language
2.5.3. The Inversion of Control (IoC) container
2.5.3.1. Java based bean metadata
2.5.3.2. Defining bean metadata within components
2.5.4. General purpose type conversion system and field formatting system
2.5.5. The Data Tier
2.5.6. The Web Tier
2.5.6.1. Comprehensive REST support
2.5.6.2. @MVC additions
2.5.7. Declarative model validation
2.5.8. Early support for Java EE 6
2.5.9. Support for embedded databases
III. Core Technologies
3. The IoC container
3.1. Introduction to the Spring IoC container and beans
3.2. Container overview
3.2.1. Configuration metadata
3.2.2. Instantiating a container
3.2.2.1. Composing XML-based configuration metadata
3.2.3. Using the container
3.3. Bean overview
3.3.1. Naming beans
3.3.1.1. Aliasing a bean outside the bean definition
3.3.2. Instantiating beans
3.3.2.1. Instantiation with a constructor
3.3.2.2. Instantiation with a static factory method
3.3.2.3. Instantiation using an instance factory method
3.4. Dependencies
3.4.1. Dependency injection
3.4.1.1. Constructor-based dependency injection
3.4.1.2. Setter-based dependency injection
3.4.1.3. Dependency resolution process
3.4.1.4. Examples of dependency injection
3.4.2. Dependencies and configuration in detail
3.4.2.1. Straight values (primitives, Strings, and so on)
2
3.4.2.2. References to other beans (collaborators)
3.4.2.3. Inner beans
3.4.2.4. Collections
3.4.2.5. Null and empty string values
3.4.2.6. XML shortcut with the p-namespace
3.4.2.7. Compound property names
3.4.3. Using depends-on
3.4.4. Lazy-initialized beans
3.4.5. Autowiring collaborators
3.4.5.1. Limitations and disadvantages of autowiring
3.4.5.2. Excluding a bean from autowiring
3.4.6. Method injection
3.4.6.1. Lookup method injection
3.4.6.2. Arbitrary method replacement
3.5. Bean scopes
3.5.1. The singleton scope
3.5.2. The prototype scope
3.5.3. Singleton beans with prototype-bean dependencies
3.5.4. Request, session, and global session scopes
3.5.4.1. Initial web configuration
3.5.4.2. Request scope
3.5.4.3. Session scope
3.5.4.4. Global session scope
3.5.4.5. Scoped beans as dependencies
3.5.5. Custom scopes
3.5.5.1. Creating a custom scope
3.5.5.2. Using a custom scope
3.6. Customizing the nature of a bean
3.6.1. Lifecycle callbacks
3.6.1.1. Initialization callbacks
3.6.1.2. Destruction callbacks
3.6.1.3. Default initialization and destroy methods
3.6.1.4. Combining lifecycle mechanisms
3.6.1.5. Startup and shutdown callbacks
3.6.1.6. Shutting down the Spring IoC container gracefully in non-web
applications
3.6.2. ApplicationContextAware and BeanNameAware
3.6.3. Other Aware interfaces
3.7. Bean definition inheritance
3.8. Container extension points
3.8.1. Customizing beans using the BeanPostProcessor Interface
3
3.8.1.1. Example: Hello World, BeanPostProcessor-style
3.8.1.2. Example: The RequiredAnnotationBeanPostProcessor
3.8.2. Customizing configuration metadata with BeanFactoryPostProcessor
interface
3.8.2.1. Example: the PropertyPlaceholderConfigurer
3.8.2.2. Example: the PropertyOverrideConfigurer
3.8.3. Customizing instantiation logic with the FactoryBean Interface
3.9. Annotation-based container configuration
3.9.1. @Required
3.9.2. @Autowired and @Inject
3.9.3. Fine-tuning annotation-based autowiring with qualifiers
3.9.4. CustomAutowireConfigurer
3.9.5. @Resource
3.9.6. @PostConstruct and @PreDestroy
3.10. Classpath scanning and managed components
3.10.1. @Component and further stereotype annotations
3.10.2. Automatically detecting classes and registering bean definitions
3.10.3. Using filters to customize scanning
3.10.4. Defining bean metadata within components
3.10.5. Naming autodetected components
3.10.6. Providing a scope for autodetected components
3.10.7. Providing qualifier metadata with annotations
3.11. Java-based container configuration
3.11.1. Basic concepts: @Configuration and @Bean
3.11.2. Instantiating the Spring container using
AnnotationConfigApplicationContext
3.11.2.1. Simple construction
3.11.2.2. Building the container programmatically using register(Class<?>...)
3.11.2.3. Enabling component scanning with scan(String...)
3.11.2.4. Support for web applications with
AnnotationConfigWebApplicationContext
3.11.3. Composing Java-based configurations
3.11.3.1. Using the @Import annotation
3.11.3.2. Combining Java and XML configuration
3.11.4. Using the @Bean annotation
3.11.4.1. Declaring a bean
3.11.4.2. Injecting dependencies
3.11.4.3. Receiving lifecycle callbacks
3.11.4.4. Specifying bean scope
3.11.4.5. Customizing bean naming
3.11.4.6. Bean aliasing
4
3.11.5. Further information about how Java-based configuration works
internally
3.12. Registering a LoadTimeWeaver
3.13. Additional Capabilities of the ApplicationContext
3.13.1. Internationalization using MessageSource
3.13.2. Standard and Custom Events
3.13.3. Convenient access to low-level resources
3.13.4. Convenient ApplicationContext instantiation for web applications
3.13.5. Deploying a Spring ApplicationContext as a J2EE RAR file
3.14. The BeanFactory
3.14.1. BeanFactory or ApplicationContext?
3.14.2. Glue code and the evil singleton
4. Resources
4.1. Introduction
4.2. The Resource interface
4.3. Built-in Resource implementations
4.3.1. UrlResource
4.3.2. ClassPathResource
4.3.3. FileSystemResource
4.3.4. ServletContextResource
4.3.5. InputStreamResource
4.3.6. ByteArrayResource
4.4. The ResourceLoader
4.5. The ResourceLoaderAware interface
4.6. Resources as dependencies
4.7. Application contexts and Resource paths
4.7.1. Constructing application contexts
4.7.1.1. Constructing ClassPathXmlApplicationContext instances - shortcuts
4.7.2. Wildcards in application context constructor resource paths
4.7.2.1. Ant-style Patterns
4.7.2.2. The classpath*: prefix
4.7.2.3. Other notes relating to wildcards
4.7.3. FileSystemResource caveats
5. Validation, Data Binding, and Type Conversion
5.1. Introduction
5.2. Validation using Spring's Validator interface
5.3. Resolving codes to error messages
5.4. Bean manipulation and the BeanWrapper
5.4.1. Setting and getting basic and nested properties
5.4.2. Built-in PropertyEditor implementations
5.4.2.1. Registering additional custom PropertyEditors
5
5.5. Spring 3 Type Conversion
5.5.1. Converter SPI
5.5.2. ConverterFactory
5.5.3. GenericConverter
5.5.3.1. ConditionalGenericConverter
5.5.4. ConversionService API
5.5.5. Configuring a ConversionService
5.5.6. Using a ConversionService programatically
5.6. Spring 3 Field Formatting
5.6.1. Formatter SPI
5.6.2. Annotation-driven Formatting
5.6.2.1. Format Annotation API
5.6.3. FormatterRegistry SPI
5.6.4. Configuring Formatting in Spring MVC
5.7. Spring 3 Validation
5.7.1. Overview of the JSR-303 Bean Validation API
5.7.2. Configuring a Bean Validation Implementation
5.7.2.1. Injecting a Validator
5.7.2.2. Configuring Custom Constraints
5.7.2.3. Additional Configuration Options
5.7.3. Configuring a DataBinder
5.7.4. Spring MVC 3 Validation
5.7.4.1. Triggering @Controller Input Validation
5.7.4.2. Configuring a Validator for use by Spring MVC
5.7.4.3. Configuring a JSR-303 Validator for use by Spring MVC
6. Spring Expression Language (SpEL)
6.1. Introduction
6.2. Feature Overview
6.3. Expression Evaluation using Spring's Expression Interface
6.3.1. The EvaluationContext interface
6.3.1.1. Type Conversion
6.4. Expression support for defining bean definitions
6.4.1. XML based configuration
6.4.2. Annotation-based configuration
6.5. Language Reference
6.5.1. Literal expressions
6.5.2. Properties, Arrays, Lists, Maps, Indexers
6.5.3. Inline lists
6.5.4. Array construction
6.5.5. Methods
6.5.6. Operators
6
6.5.6.1. Relational operators
6.5.6.2. Logical operators
6.5.6.3. Mathematical operators
6.5.7. Assignment
6.5.8. Types
6.5.9. Constructors
6.5.10. Variables
6.5.10.1. The #this and #root variables
6.5.11. Functions
6.5.12. Bean references
6.5.13. Ternary Operator (If-Then-Else)
6.5.14. The Elvis Operator
6.5.15. Safe Navigation operator
6.5.16. Collection Selection
6.5.17. Collection Projection
6.5.18. Expression templating
6.6. Classes used in the examples
7. Aspect Oriented Programming with Spring
7.1. Introduction
7.1.1. AOP concepts
7.1.2. Spring AOP capabilities and goals
7.1.3. AOP Proxies
7.2. @AspectJ support
7.2.1. Enabling @AspectJ Support
7.2.2. Declaring an aspect
7.2.3. Declaring a pointcut
7.2.3.1. Supported Pointcut Designators
7.2.3.2. Combining pointcut expressions
7.2.3.3. Sharing common pointcut definitions
7.2.3.4. Examples
7.2.3.5. Writing good pointcuts
7.2.4. Declaring advice
7.2.4.1. Before advice
7.2.4.2. After returning advice
7.2.4.3. After throwing advice
7.2.4.4. After (finally) advice
7.2.4.5. Around advice
7.2.4.6. Advice parameters
7.2.4.7. Advice ordering
7.2.5. Introductions
7.2.6. Aspect instantiation models
7
7.2.7. Example
7.3. Schema-based AOP support
7.3.1. Declaring an aspect
7.3.2. Declaring a pointcut
7.3.3. Declaring advice
7.3.3.1. Before advice
7.3.3.2. After returning advice
7.3.3.3. After throwing advice
7.3.3.4. After (finally) advice
7.3.3.5. Around advice
7.3.3.6. Advice parameters
7.3.3.7. Advice ordering
7.3.4. Introductions
7.3.5. Aspect instantiation models
7.3.6. Advisors
7.3.7. Example
7.4. Choosing which AOP declaration style to use
7.4.1. Spring AOP or full AspectJ?
7.4.2. @AspectJ or XML for Spring AOP?
7.5. Mixing aspect types
7.6. Proxying mechanisms
7.6.1. Understanding AOP proxies
7.7. Programmatic creation of @AspectJ Proxies
7.8. Using AspectJ with Spring applications
7.8.1. Using AspectJ to dependency inject domain objects with Spring
7.8.1.1. Unit testing @Configurable objects
7.8.1.2. Working with multiple application contexts
7.8.2. Other Spring aspects for AspectJ
7.8.3. Configuring AspectJ aspects using Spring IoC
7.8.4. Load-time weaving with AspectJ in the Spring Framework
7.8.4.1. A first example
7.8.4.2. Aspects
7.8.4.3. 'META-INF/aop.xml'
7.8.4.4. Required libraries (JARS)
7.8.4.5. Spring configuration
7.8.4.6. Environment-specific configuration
7.9. Further Resources
8. Spring AOP APIs
8.1. Introduction
8.2. Pointcut API in Spring
8.2.1. Concepts
8
8.2.2. Operations on pointcuts
8.2.3. AspectJ expression pointcuts
8.2.4. Convenience pointcut implementations
8.2.4.1. Static pointcuts
8.2.4.2. Dynamic pointcuts
8.2.5. Pointcut superclasses
8.2.6. Custom pointcuts
8.3. Advice API in Spring
8.3.1. Advice lifecycles
8.3.2. Advice types in Spring
8.3.2.1. Interception around advice
8.3.2.2. Before advice
8.3.2.3. Throws advice
8.3.2.4. After Returning advice
8.3.2.5. Introduction advice
8.4. Advisor API in Spring
8.5. Using the ProxyFactoryBean to create AOP proxies
8.5.1. Basics
8.5.2. JavaBean properties
8.5.3. JDK- and CGLIB-based proxies
8.5.4. Proxying interfaces
8.5.5. Proxying classes
8.5.6. Using 'global' advisors
8.6. Concise proxy definitions
8.7. Creating AOP proxies programmatically with the ProxyFactory
8.8. Manipulating advised objects
8.9. Using the "autoproxy" facility
8.9.1. Autoproxy bean definitions
8.9.1.1. BeanNameAutoProxyCreator
8.9.1.2. DefaultAdvisorAutoProxyCreator
8.9.1.3. AbstractAdvisorAutoProxyCreator
8.9.2. Using metadata-driven auto-proxying
8.10. Using TargetSources
8.10.1. Hot swappable target sources
8.10.2. Pooling target sources
8.10.3. Prototype target sources
8.10.4. ThreadLocal target sources
8.11. Defining new Advice types
8.12. Further resources
9. Testing
9.1. Introduction to testing
9
9.2. Unit testing
9.2.1. Mock objects
9.2.1.1. JNDI
9.2.1.2. Servlet API
9.2.1.3. Portlet API
9.2.2. Unit testing support classes
9.2.2.1. General utilities
9.2.2.2. Spring MVC
9.3. Integration testing
9.3.1. Overview
9.3.2. Goals of integration testing
9.3.2.1. Context management and caching
9.3.2.2. Dependency Injection of test fixtures
9.3.2.3. Transaction management
9.3.2.4. Support classes for integration testing
9.3.3. JDBC testing support
9.3.4. Annotations
9.3.5. Spring TestContext Framework
9.3.5.1. Key abstractions
9.3.5.2. Context management and caching
9.3.5.3. Dependency Injection of test fixtures
9.3.5.4. Transaction management
9.3.5.5. TestContext support classes
9.3.6. PetClinic example
9.4. Further Resources
IV. Data Access
10. Transaction Management
10.1. Introduction to Spring Framework transaction management
10.2. Advantages of the Spring Framework's transaction support model
10.2.1. Global transactions
10.2.2. Local transactions
10.2.3. Spring Framework's consistent programming model
10.3. Understanding the Spring Framework transaction abstraction
10.4. Synchronizing resources with transactions
10.4.1. High-level synchronization approach
10.4.2. Low-level synchronization approach
10.4.3. TransactionAwareDataSourceProxy
10.5. Declarative transaction management
10.5.1. Understanding the Spring Framework's declarative transaction
implementation
10.5.2. Example of declarative transaction implementation
10
10.5.3. Rolling back a declarative transaction
10.5.4. Configuring different transactional semantics for different beans
10.5.5. <tx:advice/> settings
10.5.6. Using @Transactional
10.5.6.1. @Transactional settings
10.5.6.2. Multiple Transaction Managers with @Transactional
10.5.6.3. Custom shortcut annotations
10.5.7. Transaction propagation
10.5.7.1. Required
10.5.7.2. RequiresNew
10.5.7.3. Nested
10.5.8. Advising transactional operations
10.5.9. Using @Transactional with AspectJ
10.6. Programmatic transaction management
10.6.1. Using the TransactionTemplate
10.6.1.1. Specifying transaction settings
10.6.2. Using the PlatformTransactionManager
10.7. Choosing between programmatic and declarative transaction
management
10.8. Application server-specific integration
10.8.1. IBM WebSphere
10.8.2. BEA WebLogic Server
10.8.3. Oracle OC4J
10.9. Solutions to common problems
10.9.1. Use of the wrong transaction manager for a specific DataSource
10.10. Further Resources
11. DAO support
11.1. Introduction
11.2. Consistent exception hierarchy
11.3. Annotations used for configuring DAO or Repository classes
12. Data access with JDBC
12.1. Introduction to Spring Framework JDBC
12.1.1. Choosing an approach for JDBC database access
12.1.2. Package hierarchy
12.2. Using the JDBC core classes to control basic JDBC processing and
error handling
12.2.1. JdbcTemplate
12.2.1.1. Examples of JdbcTemplate class usage
12.2.1.2. JdbcTemplate best practices
12.2.2. NamedParameterJdbcTemplate
12.2.3. SimpleJdbcTemplate
11
12.2.4. SQLExceptionTranslator
12.2.5. Executing statements
12.2.6. Running queries
12.2.7. Updating the database
12.2.8. Retrieving auto-generated keys
12.3. Controlling database connections
12.3.1. DataSource
12.3.2. DataSourceUtils
12.3.3. SmartDataSource
12.3.4. AbstractDataSource
12.3.5. SingleConnectionDataSource
12.3.6. DriverManagerDataSource
12.3.7. TransactionAwareDataSourceProxy
12.3.8. DataSourceTransactionManager
12.3.9. NativeJdbcExtractor
12.4. JDBC batch operations
12.4.1. Batch operations with the JdbcTemplate
12.4.2. Batch operations with the SimpleJdbcTemplate
12.5. Simplifying JDBC operations with the SimpleJdbc classes
12.5.1. Inserting data using SimpleJdbcInsert
12.5.2. Retrieving auto-generated keys using SimpleJdbcInsert
12.5.3. Specifying columns for a SimpleJdbcInsert
12.5.4. Using SqlParameterSource to provide parameter values
12.5.5. Calling a stored procedure with SimpleJdbcCall
12.5.6. Explicitly declaring parameters to use for a SimpleJdbcCall
12.5.7. How to define SqlParameters
12.5.8. Calling a stored function using SimpleJdbcCall
12.5.9. Returning ResultSet/REF Cursor from a SimpleJdbcCall
12.6. Modeling JDBC operations as Java objects
12.6.1. SqlQuery
12.6.2. MappingSqlQuery
12.6.3. SqlUpdate
12.6.4. StoredProcedure
12.7. Common problems with parameter and data value handling
12.7.1. Providing SQL type information for parameters
12.7.2. Handling BLOB and CLOB objects
12.7.3. Passing in lists of values for IN clause
12.7.4. Handling complex types for stored procedure calls
12.8. Embedded database support
12.8.1. Why use an embedded database?
12.8.2. Creating an embedded database instance using Spring XML
12
12.8.3. Creating an embedded database instance programmatically
12.8.4. Extending the embedded database support
12.8.5. Using HSQL
12.8.6. Using H2
12.8.7. Using Derby
12.8.8. Testing data access logic with an embedded database
12.9. Initializing a DataSource
12.9.1. Initializing a database instance using Spring XML
12.9.1.1. Initialization of Other Components that Depend on the Database
13. Object Relational Mapping (ORM) Data Access
13.1. Introduction to ORM with Spring
13.2. General ORM integration considerations
13.2.1. Resource and transaction management
13.2.2. Exception translation
13.3. Hibernate
13.3.1. SessionFactory setup in a Spring container
13.3.2. Implementing DAOs based on plain Hibernate 3 API
13.3.3. Declarative transaction demarcation
13.3.4. Programmatic transaction demarcation
13.3.5. Transaction management strategies
13.3.6. Comparing container-managed and locally defined resources
13.3.7. Spurious application server warnings with Hibernate
13.4. JDO
13.4.1. PersistenceManagerFactory setup
13.4.2. Implementing DAOs based on the plain JDO API
13.4.3. Transaction management
13.4.4. JdoDialect
13.5. JPA
13.5.1. Three options for JPA setup in a Spring environment
13.5.1.1. LocalEntityManagerFactoryBean
13.5.1.2. Obtaining an EntityManagerFactory from JNDI
13.5.1.3. LocalContainerEntityManagerFactoryBean
13.5.1.4. Dealing with multiple persistence units
13.5.2. Implementing DAOs based on plain JPA
13.5.3. Transaction Management
13.5.4. JpaDialect
13.6. iBATIS SQL Maps
13.6.1. Setting up the SqlMapClient
13.6.2. Using SqlMapClientTemplate and SqlMapClientDaoSupport
13.6.3. Implementing DAOs based on plain iBATIS API
14. Marshalling XML using O/X Mappers
13
14.1. Introduction
14.2. Marshaller and Unmarshaller
14.2.1. Marshaller
14.2.2. Unmarshaller
14.2.3. XmlMappingException
14.3. Using Marshaller and Unmarshaller
14.4. XML Schema-based Configuration
14.5. JAXB
14.5.1. Jaxb2Marshaller
14.5.1.1. XML Schema-based Configuration
14.6. Castor
14.6.1. CastorMarshaller
14.6.2. Mapping
14.7. XMLBeans
14.7.1. XmlBeansMarshaller
14.7.1.1. XML Schema-based Configuration
14.8. JiBX
14.8.1. JibxMarshaller
14.8.1.1. XML Schema-based Configuration
14.9. XStream
14.9.1. XStreamMarshaller
V. The Web
15. Web MVC framework
15.1. Introduction to Spring Web MVC framework
15.1.1. Features of Spring Web MVC
15.1.2. Pluggability of other MVC implementations
15.2. The DispatcherServlet
15.3. Implementing Controllers
15.3.1. Defining a controller with @Controller
15.3.2. Mapping requests with @RequestMapping
15.3.2.1. URI Templates
15.3.2.2. Advanced @RequestMapping options
15.3.2.3. Supported handler method arguments and return types
15.3.2.4. Binding request parameters to method parameters with
@RequestParam
15.3.2.5. Mapping the request body with the @RequestBody annotation
15.3.2.6. Mapping the response body with the @ResponseBody annotation
15.3.2.7. Using HttpEntity<?>
15.3.2.8. Providing a link to data from the model with @ModelAttribute
15.3.2.9. Specifying attributes to store in a session with @SessionAttributes
15.3.2.10. Mapping cookie values with the @CookieValue annotation
14
15.3.2.11. Mapping request header attributes with the @RequestHeader
annotation
15.3.2.12. Customizing WebDataBinder initialization
15.4. Handler mappings
15.4.1. Intercepting requests - the HandlerInterceptor interface
15.5. Resolving views
15.5.1. Resolving views with the ViewResolver interface
15.5.2. Chaining ViewResolvers
15.5.3. Redirecting to views
15.5.3.1. RedirectView
15.5.3.2. The redirect: prefix
15.5.3.3. The forward: prefix
15.5.4. ContentNegotiatingViewResolver
15.6. Using locales
15.6.1. AcceptHeaderLocaleResolver
15.6.2. CookieLocaleResolver
15.6.3. SessionLocaleResolver
15.6.4. LocaleChangeInterceptor
15.7. Using themes
15.7.1. Overview of themes
15.7.2. Defining themes
15.7.3. Theme resolvers
15.8. Spring's multipart (fileupload) support
15.8.1. Introduction
15.8.2. Using the MultipartResolver
15.8.3. Handling a file upload in a form
15.9. Handling exceptions
15.9.1. HandlerExceptionResolver
15.9.2. @ExceptionHandler
15.10. Convention over configuration support
15.10.1. The Controller ControllerClassNameHandlerMapping
15.10.2. The Model ModelMap (ModelAndView)
15.10.3. The View - RequestToViewNameTranslator
15.11. ETag support
15.12. Configuring Spring MVC
15.12.1. mvc:annotation-driven
15.12.2. mvc:interceptors
15.12.3. mvc:view-controller
15.12.4. mvc:resources
15.12.5. mvc:default-servlet-handler
15.13. More Spring Web MVC Resources
15
16. View technologies
16.1. Introduction
16.2. JSP & JSTL
16.2.1. View resolvers
16.2.2. 'Plain-old' JSPs versus JSTL
16.2.3. Additional tags facilitating development
16.2.4. Using Spring's form tag library
16.2.4.1. Configuration
16.2.4.2. The form tag
16.2.4.3. The input tag
16.2.4.4. The checkbox tag
16.2.4.5. The checkboxes tag
16.2.4.6. The radiobutton tag
16.2.4.7. The radiobuttons tag
16.2.4.8. The password tag
16.2.4.9. The select tag
16.2.4.10. The option tag
16.2.4.11. The options tag
16.2.4.12. The textarea tag
16.2.4.13. The hidden tag
16.2.4.14. The errors tag
16.2.4.15. HTTP Method Conversion
16.3. Tiles
16.3.1. Dependencies
16.3.2. How to integrate Tiles
16.3.2.1. UrlBasedViewResolver
16.3.2.2. ResourceBundleViewResolver
16.3.2.3. SimpleSpringPreparerFactory and SpringBeanPreparerFactory
16.4. Velocity & FreeMarker
16.4.1. Dependencies
16.4.2. Context configuration
16.4.3. Creating templates
16.4.4. Advanced configuration
16.4.4.1. velocity.properties
16.4.4.2. FreeMarker
16.4.5. Bind support and form handling
16.4.5.1. The bind macros
16.4.5.2. Simple binding
16.4.5.3. Form input generation macros
16.4.5.4. HTML escaping and XHTML compliance
16.5. XSLT
16
16.5.1. My First Words
16.5.1.1. Bean definitions
16.5.1.2. Standard MVC controller code
16.5.1.3. Convert the model data to XML
16.5.1.4. Defining the view properties
16.5.1.5. Document transformation
16.5.2. Summary
16.6. Document views (PDF/Excel)
16.6.1. Introduction
16.6.2. Configuration and setup
16.6.2.1. Document view definitions
16.6.2.2. Controller code
16.6.2.3. Subclassing for Excel views
16.6.2.4. Subclassing for PDF views
16.7. JasperReports
16.7.1. Dependencies
16.7.2. Configuration
16.7.2.1. Configuring the ViewResolver
16.7.2.2. Configuring the Views
16.7.2.3. About Report Files
16.7.2.4. Using JasperReportsMultiFormatView
16.7.3. Populating the ModelAndView
16.7.4. Working with Sub-Reports
16.7.4.1. Configuring Sub-Report Files
16.7.4.2. Configuring Sub-Report Data Sources
16.7.5. Configuring Exporter Parameters
16.8. Feed Views
16.9. XML Marshalling View
16.10. JSON Mapping View
17. Integrating with other web frameworks
17.1. Introduction
17.2. Common configuration
17.3. JavaServer Faces 1.1 and 1.2
17.3.1. DelegatingVariableResolver (JSF 1.1/1.2)
17.3.2. SpringBeanVariableResolver (JSF 1.1/1.2)
17.3.3. SpringBeanFacesELResolver (JSF 1.2+)
17.3.4. FacesContextUtils
17.4. Apache Struts 1.x and 2.x
17.4.1. ContextLoaderPlugin
17.4.1.1. DelegatingRequestProcessor
17.4.1.2. DelegatingActionProxy
17
17.4.2. ActionSupport Classes
17.5. WebWork 2.x
17.6. Tapestry 3.x and 4.x
17.6.1. Injecting Spring-managed beans
17.6.1.1. Dependency Injecting Spring Beans into Tapestry pages
17.6.1.2. Component definition files
17.6.1.3. Adding abstract accessors
17.6.1.4. Dependency Injecting Spring Beans into Tapestry pages - Tapestry
4.x style
17.7. Further Resources
18. Portlet MVC Framework
18.1. Introduction
18.1.1. Controllers - The C in MVC
18.1.2. Views - The V in MVC
18.1.3. Web-scoped beans
18.2. The DispatcherPortlet
18.3. The ViewRendererServlet
18.4. Controllers
18.4.1. AbstractController and PortletContentGenerator
18.4.2. Other simple controllers
18.4.3. Command Controllers
18.4.4. PortletWrappingController
18.5. Handler mappings
18.5.1. PortletModeHandlerMapping
18.5.2. ParameterHandlerMapping
18.5.3. PortletModeParameterHandlerMapping
18.5.4. Adding HandlerInterceptors
18.5.5. HandlerInterceptorAdapter
18.5.6. ParameterMappingInterceptor
18.6. Views and resolving them
18.7. Multipart (file upload) support
18.7.1. Using the PortletMultipartResolver
18.7.2. Handling a file upload in a form
18.8. Handling exceptions
18.9. Annotation-based controller configuration
18.9.1. Setting up the dispatcher for annotation support
18.9.2. Defining a controller with @Controller
18.9.3. Mapping requests with @RequestMapping
18.9.4. Supported handler method arguments
18.9.5. Binding request parameters to method parameters with
@RequestParam
18
18.9.6. Providing a link to data from the model with @ModelAttribute
18.9.7. Specifying attributes to store in a Session with @SessionAttributes
18.9.8. Customizing WebDataBinder initialization
18.9.8.1. Customizing data binding with @InitBinder
18.9.8.2. Configuring a custom WebBindingInitializer
18.10. Portlet application deployment
VI. Integration
19. Remoting and web services using Spring
19.1. Introduction
19.2. Exposing services using RMI
19.2.1. Exporting the service using the RmiServiceExporter
19.2.2. Linking in the service at the client
19.3. Using Hessian or Burlap to remotely call services via HTTP
19.3.1. Wiring up the DispatcherServlet for Hessian and co.
19.3.2. Exposing your beans by using the HessianServiceExporter
19.3.3. Linking in the service on the client
19.3.4. Using Burlap
19.3.5. Applying HTTP basic authentication to a service exposed through
Hessian or Burlap
19.4. Exposing services using HTTP invokers
19.4.1. Exposing the service object
19.4.2. Linking in the service at the client
19.5. Web services
19.5.1. Exposing servlet-based web services using JAX-RPC
19.5.2. Accessing web services using JAX-RPC
19.5.3. Registering JAX-RPC Bean Mappings
19.5.4. Registering your own JAX-RPC Handler
19.5.5. Exposing servlet-based web services using JAX-WS
19.5.6. Exporting standalone web services using JAX-WS
19.5.7. Exporting web services using the JAX-WS RI's Spring support
19.5.8. Accessing web services using JAX-WS
19.6. JMS
19.6.1. Server-side configuration
19.6.2. Client-side configuration
19.7. Auto-detection is not implemented for remote interfaces
19.8. Considerations when choosing a technology
19.9. Accessing RESTful services on the Client
19.9.1. RestTemplate
19.9.1.1. Dealing with request and response headers
19.9.2. HTTP Message Conversion
19.9.2.1. StringHttpMessageConverter
19
19.9.2.2. FormHttpMessageConverter
19.9.2.3. ByteArrayMessageConverter
19.9.2.4. MarshallingHttpMessageConverter
19.9.2.5. MappingJacksonHttpMessageConverter
19.9.2.6. SourceHttpMessageConverter
19.9.2.7. BufferedImageHttpMessageConverter
20. Enterprise JavaBeans (EJB) integration
20.1. Introduction
20.2. Accessing EJBs
20.2.1. Concepts
20.2.2. Accessing local SLSBs
20.2.3. Accessing remote SLSBs
20.2.4. Accessing EJB 2.x SLSBs versus EJB 3 SLSBs
20.3. Using Spring's EJB implementation support classes
20.3.1. EJB 2.x base classes
20.3.2. EJB 3 injection interceptor
21. JMS (Java Message Service)
21.1. Introduction
21.2. Using Spring JMS
21.2.1. JmsTemplate
21.2.2. Connections
21.2.2.1. Caching Messaging Resources
21.2.2.2. SingleConnectionFactory
21.2.2.3. CachingConnectionFactory
21.2.3. Destination Management
21.2.4. Message Listener Containers
21.2.4.1. SimpleMessageListenerContainer
21.2.4.2. DefaultMessageListenerContainer
21.2.5. Transaction management
21.3. Sending a Message
21.3.1. Using Message Converters
21.3.2. SessionCallback and ProducerCallback
21.4. Receiving a message
21.4.1. Synchronous Reception
21.4.2. Asynchronous Reception - Message-Driven POJOs
21.4.3. The SessionAwareMessageListener interface
21.4.4. The MessageListenerAdapter
21.4.5. Processing messages within transactions
21.5. Support for JCA Message Endpoints
21.6. JMS Namespace Support
22. JMX
20
22.1. Introduction
22.2. Exporting your beans to JMX
22.2.1. Creating an MBeanServer
22.2.2. Reusing an existing MBeanServer
22.2.3. Lazy-initialized MBeans
22.2.4. Automatic registration of MBeans
22.2.5. Controlling the registration behavior
22.3. Controlling the management interface of your beans
22.3.1. The MBeanInfoAssembler Interface
22.3.2. Using Source-Level Metadata (JDK 5.0 annotations)
22.3.3. Source-Level Metadata Types
22.3.4. The AutodetectCapableMBeanInfoAssembler interface
22.3.5. Defining management interfaces using Java interfaces
22.3.6. Using MethodNameBasedMBeanInfoAssembler
22.4. Controlling the ObjectNames for your beans
22.4.1. Reading ObjectNames from Properties
22.4.2. Using the MetadataNamingStrategy
22.4.3. The <context:mbean-export/> element
22.5. JSR-160 Connectors
22.5.1. Server-side Connectors
22.5.2. Client-side Connectors
22.5.3. JMX over Burlap/Hessian/SOAP
22.6. Accessing MBeans via Proxies
22.7. Notifications
22.7.1. Registering Listeners for Notifications
22.7.2. Publishing Notifications
22.8. Further Resources
23. JCA CCI
23.1. Introduction
23.2. Configuring CCI
23.2.1. Connector configuration
23.2.2. ConnectionFactory configuration in Spring
23.2.3. Configuring CCI connections
23.2.4. Using a single CCI connection
23.3. Using Spring's CCI access support
23.3.1. Record conversion
23.3.2. The CciTemplate
23.3.3. DAO support
23.3.4. Automatic output record generation
23.3.5. Summary
23.3.6. Using a CCI Connection and Interaction directly
21
23.3.7. Example for CciTemplate usage
23.4. Modeling CCI access as operation objects
23.4.1. MappingRecordOperation
23.4.2. MappingCommAreaOperation
23.4.3. Automatic output record generation
23.4.4. Summary
23.4.5. Example for MappingRecordOperation usage
23.4.6. Example for MappingCommAreaOperation usage
23.5. Transactions
24. Email
24.1. Introduction
24.2. Usage
24.2.1. Basic MailSender and SimpleMailMessage usage
24.2.2. Using the JavaMailSender and the MimeMessagePreparator
24.3. Using the JavaMail MimeMessageHelper
24.3.1. Sending attachments and inline resources
24.3.1.1. Attachments
24.3.1.2. Inline resources
24.3.2. Creating email content using a templating library
24.3.2.1. A Velocity-based example
25. Task Execution and Scheduling
25.1. Introduction
25.2. The Spring TaskExecutor abstraction
25.2.1. TaskExecutor types
25.2.2. Using a TaskExecutor
25.3. The Spring TaskScheduler abstraction
25.3.1. The Trigger interface
25.3.2. Trigger implementations
25.3.3. TaskScheduler implementations
25.4. The Task Namespace
25.4.1. The 'scheduler' element
25.4.2. The 'executor' element
25.4.3. The 'scheduled-tasks' element
25.5. Annotation Support for Scheduling and Asynchronous Execution
25.5.1. The @Scheduled Annotation
25.5.2. The @Async Annotation
25.5.3. The <annotation-driven> Element
25.6. Using the OpenSymphony Quartz Scheduler
25.6.1. Using the JobDetailBean
25.6.2. Using the MethodInvokingJobDetailFactoryBean
25.6.3. Wiring up jobs using triggers and the SchedulerFactoryBean
22
25.7. Using JDK Timer support
25.7.1. Creating custom timers
25.7.2. Using the MethodInvokingTimerTaskFactoryBean
25.7.3. Wrapping up: setting up the tasks using the TimerFactoryBean
26. Dynamic language support
26.1. Introduction
26.2. A first example
26.3. Defining beans that are backed by dynamic languages
26.3.1. Common concepts
26.3.1.1. The <lang:language/> element
26.3.1.2. Refreshable beans
26.3.1.3. Inline dynamic language source files
26.3.1.4. Understanding Constructor Injection in the context of dynamic-
language-backed beans
26.3.2. JRuby beans
26.3.3. Groovy beans
26.3.3.1. Customising Groovy objects via a callback
26.3.4. BeanShell beans
26.4. Scenarios
26.4.1. Scripted Spring MVC Controllers
26.4.2. Scripted Validators
26.5. Bits and bobs
26.5.1. AOP - advising scripted beans
26.5.2. Scoping
26.6. Further Resources
VII. Appendices
A. Classic Spring Usage
A.1. Classic ORM usage
A.1.1. Hibernate
A.1.1.1. The HibernateTemplate
A.1.1.2. Implementing Spring-based DAOs without callbacks
A.1.2. JDO
A.1.2.1. JdoTemplate and JdoDaoSupport
A.1.3. JPA
A.1.3.1. JpaTemplate and JpaDaoSupport
A.2. Classic Spring MVC
A.3. JMS Usage
A.3.1. JmsTemplate
A.3.2. Asynchronous Message Reception
A.3.3. Connections
A.3.4. Transaction Management
23
B. Classic Spring AOP Usage
B.1. Pointcut API in Spring
B.1.1. Concepts
B.1.2. Operations on pointcuts
B.1.3. AspectJ expression pointcuts
B.1.4. Convenience pointcut implementations
B.1.4.1. Static pointcuts
B.1.4.2. Dynamic pointcuts
B.1.5. Pointcut superclasses
B.1.6. Custom pointcuts
B.2. Advice API in Spring
B.2.1. Advice lifecycles
B.2.2. Advice types in Spring
B.2.2.1. Interception around advice
B.2.2.2. Before advice
B.2.2.3. Throws advice
B.2.2.4. After Returning advice
B.2.2.5. Introduction advice
B.3. Advisor API in Spring
B.4. Using the ProxyFactoryBean to create AOP proxies
B.4.1. Basics
B.4.2. JavaBean properties
B.4.3. JDK- and CGLIB-based proxies
B.4.4. Proxying interfaces
B.4.5. Proxying classes
B.4.6. Using 'global' advisors
B.5. Concise proxy definitions
B.6. Creating AOP proxies programmatically with the ProxyFactory
B.7. Manipulating advised objects
B.8. Using the "autoproxy" facility
B.8.1. Autoproxy bean definitions
B.8.1.1. BeanNameAutoProxyCreator
B.8.1.2. DefaultAdvisorAutoProxyCreator
B.8.1.3. AbstractAdvisorAutoProxyCreator
B.8.2. Using metadata-driven auto-proxying
B.9. Using TargetSources
B.9.1. Hot swappable target sources
B.9.2. Pooling target sources
B.9.3. Prototype target sources
B.9.4. ThreadLocal target sources
B.10. Defining new Advice types
24
B.11. Further resources
C. XML Schema-based configuration
C.1. Introduction
C.2. XML Schema-based configuration
C.2.1. Referencing the schemas
C.2.2. The util schema
C.2.2.1. <util:constant/>
C.2.2.2. <util:property-path/>
C.2.2.3. <util:properties/>
C.2.2.4. <util:list/>
C.2.2.5. <util:map/>
C.2.2.6. <util:set/>
C.2.3. The jee schema
C.2.3.1. <jee:jndi-lookup/> (simple)
C.2.3.2. <jee:jndi-lookup/> (with single JNDI environment setting)
C.2.3.3. <jee:jndi-lookup/> (with multiple JNDI environment settings)
C.2.3.4. <jee:jndi-lookup/> (complex)
C.2.3.5. <jee:local-slsb/> (simple)
C.2.3.6. <jee:local-slsb/> (complex)
C.2.3.7. <jee:remote-slsb/>
C.2.4. The lang schema
C.2.5. The jms schema
C.2.6. The tx (transaction) schema
C.2.7. The aop schema
C.2.8. The context schema
C.2.8.1. <property-placeholder/>
C.2.8.2. <annotation-config/>
C.2.8.3. <component-scan/>
C.2.8.4. <load-time-weaver/>
C.2.8.5. <spring-configured/>
C.2.8.6. <mbean-export/>
C.2.9. The tool schema
C.2.10. The beans schema
D. Extensible XML authoring
D.1. Introduction
D.2. Authoring the schema
D.3. Coding a NamespaceHandler
D.4. Coding a BeanDefinitionParser
D.5. Registering the handler and the schema
D.5.1. 'META-INF/spring.handlers'
D.5.2. 'META-INF/spring.schemas'
25
D.6. Using a custom extension in your Spring XML configuration
D.7. Meatier examples
D.7.1. Nesting custom tags within custom tags
D.7.2. Custom attributes on 'normal' elements
D.8. Further Resources
E. spring-beans-2.0.dtd
F. spring.tld
F.1. Introduction
F.2. The bind tag
F.3. The escapeBody tag
F.4. The hasBindErrors tag
F.5. The htmlEscape tag
F.6. The message tag
F.7. The nestedPath tag
F.8. The theme tag
F.9. The transform tag
F.10. The url tag
F.11. The eval tag
G. spring-form.tld
G.1. Introduction
G.2. The checkbox tag
G.3. The checkboxes tag
G.4. The errors tag
G.5. The form tag
G.6. The hidden tag
G.7. The input tag
G.8. The label tag
G.9. The option tag
G.10. The options tag
G.11. The password tag
G.12. The radiobutton tag
G.13. The radiobuttons tag
G.14. The select tag
G.15. The textarea tag
26
Part I. Overview of Spring Framework
The Spring Framework is a lightweight solution and a potential one-stop-shop for
building your enterprise-ready applications. However, Spring is modular, allowing
you to use only those parts that you need, without having to bring in the rest. You
can use the IoC container, with Struts on top, but you can also use only
the Hibernate integration code or the JDBC abstraction layer. The Spring
Framework supports declarative transaction management, remote access to your
logic through RMI or web services, and various options for persisting your data. It
offers a full-featured MVC framework, and enables you to
integrate AOP transparently into your software.
This document is a reference guide to Spring Framework features. If you have any
requests, comments, or questions on this document, please post them on the user
mailing list or on the support forums at http://forum.springsource.org/.
Spring enables you to build applications from “plain old Java objects” (POJOs) and
to apply enterprise services non-invasively to POJOs. This capability applies to the
Java SE programming model and to full and partial Java EE.
Examples of how you, as an application developer, can use the Spring platform
advantage:
27
Make a local Java method a management operation without having to deal
with JMX APIs.
Make a local Java method a message handler without having to deal with
JMS APIs.
“The question is, what aspect of control are [they] inverting?” Martin Fowler posed this question about
Inversion of Control (IoC) on his site in 2004. Fowler suggested renaming the principle to make it more
self-explanatory and came up with Dependency Injection.
For insight into IoC and DI, refer to Fowler's article athttp://martinfowler.com/articles/injection.html.
Java applications -- a loose term that runs the gamut from constrained applets to
n-tier server-side enterprise applications -- typically consist of objects that
collaborate to form the application proper. Thus the objects in an application
have dependencies on each other.
1.2 Modules
28
AOP (Aspect Oriented Programming), Instrumentation, and Test, as shown in the
following diagram.
1.2.1 Core Container
29
Beans module and adds support for internationalization (using, for example,
resource bundles), event-propagation, resource-loading, and the transparent
creation of contexts by, for example, a servlet container. The Context module also
supports Java EE features such as EJB, JMX ,and basic remoting.
The ApplicationContext interface is the focal point of the Context module.
1.2.2 Data Access/Integration
The Java Messaging Service (JMS) module contains features for producing and
consuming messages.
30
1.2.3 Web
1.2.5 Test
31
contexts. It also provides mock objects that you can use to test your code in
isolation.
1.3 Usage scenarios
The building blocks described previously make Spring a logical choice in many
scenarios, from applets to full-fledged enterprise applications that use Spring's
transaction management functionality and web framework integration.
32
which lets you choose where to execute validation rules. Spring's ORM support is
integrated with JPA, Hibernate, JDO and iBatis; for example, when using
Hibernate, you can continue to use your existing mapping files and standard
Hibernate SessionFactory configuration. Form controllers seamlessly integrate the
web-layer with the domain model, removing the need for ActionForms or other
classes that transform HTTP parameters to values for your domain model.
33
Remoting usage scenario
When you need to access existing code through web services, you can use
Spring's Hessian-, Burlap-, Rmi- or JaxRpcProxyFactory classes. Enabling remote
access to existing applications is not difficult.
34
EJBs - Wrapping existing POJOs
35
If you are going to use Spring you need to get a copy of the jar libraries that
comprise the pieces of Spring that you need. To make this easier Spring is
packaged as a set of modules that separate the dependencies as much as
possible, so for example if you don't want to write a web application you don't need
the spring-web modules. To refer to Spring library modules in this guide we use a
shorthand naming convention spring-* or spring-*.jar, where "*" represents the
short name for the module (e.g. spring-core, spring-webmvc, spring-jms, etc.). The
actual jar file name that you use may be in this form (see below) or it may not, and
normally it also has a version number in the file name (e.g. spring-core-
3.0.0.RELEASE.jar).
36
So the first thing you need to decide is how to manage your dependencies: most
people use an automated system like Maven or Ivy, but you can also do it
manually by downloading all the jars yourself. When obtaining Spring with Maven
or Ivy you have then to decide which place you'll get it from. In general, if you care
about OSGi, use the EBR, since it houses OSGi compatible artifacts for all of
Spring's dependencies, such as Hibernate and Freemarker. If OSGi does not
matter to you, either place works, though there are some pros and cons between
them. In general, pick one place or the other for your project; do not mix them. This
is particularly important since EBR artifacts necessarily use a different naming
convention than Maven Central artifacts.
No Yes
37
1.3.1.1 Spring Dependencies and Depending on Spring
Although Spring provides integration and support for a huge range of enterprise
and other external tools, it intentionally keeps its mandatory dependencies to an
absolute minimum: you shouldn't have to locate and download (even
automatically) a large number of jar libraries in order to use Spring for simple use
cases. For basic dependency injection there is only one mandatory external
dependency, and that is for logging (see below for a more detailed description of
logging options).
Next we outline the basic steps needed to configure an application that depends
on Spring, first with Maven and then with Ivy. In all cases, if anything is unclear,
refer to the documentation of your dependency management system, or look at
some sample code - Spring itself uses Ivy to manage dependencies when it is
building, and our samples mostly use Maven.
If you are using Maven for dependency management you don't even need to
supply the logging dependency explicitly. For example, to create an application
context and use dependency injection to configure an application, your Maven
dependencies will look like this:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>
That's it. Note the scope can be declared as runtime if you don't need to compile
against Spring APIs, which is typically the case for basic dependency injection use
cases.
We used the Maven Central naming conventions in the example above, so that
works with Maven Central or the SpringSource S3 Maven repository. To use the
S3 Maven repository (e.g. for milestones or developer snaphots), you need to
specify the repository location in your Maven configuration. For full releases:
38
<repositories>
<repository>
<id>com.springsource.repository.maven.release</id>
<url>http://maven.springframework.org/release/</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
For milestones:
<repositories>
<repository>
<id>com.springsource.repository.maven.milestone</id>
<url>http://maven.springframework.org/milestone/</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<repositories>
<repository>
<id>com.springsource.repository.maven.snapshot</id>
<url>http://maven.springframework.org/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
To use the SpringSource EBR you would need to use a different naming
convention for the dependencies. The names are usually easy to guess, e.g. in
this case it is:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>
You also need to declare the location of the repository explicitly (only the URL is
important):
<repositories>
39
<repository>
<id>com.springsource.repository.bundles.release</id>
<url>http://repository.springsource.com/maven/bundles/release/</url>
</repository>
</repositories>
If you are managing your dependencies by hand, the URL in the repository
declaration above is not browseable, but there is a user interface
athttp://www.springsource.com/repository that can be used to search for and
download dependencies. It also has handy snippets of Maven and Ivy
configuration that you can copy and paste if you are using those tools.
If you prefer to use Ivy to manage dependencies then there are similar names and
configuration options.
To configure Ivy to point to the SpringSource EBR add the following resolvers to
your ivysettings.xml:
<resolvers>
<url name="com.springsource.repository.bundles.release">
<ivy pattern="http://repository.springsource.com/ivy/bundles/release/
[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/release/
[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<url name="com.springsource.repository.bundles.external">
<ivy pattern="http://repository.springsource.com/ivy/bundles/external/
[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
<artifact pattern="http://repository.springsource.com/ivy/bundles/external/
[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
</resolvers>
The XML above is not valid because the lines are too long - if you copy-paste then
remove the extra line endings in the middle of the url patterns.
Once Ivy is configured to look in the EBR adding a dependency is easy. Simply
pull up the details page for the bundle in question in the repository browser and
40
you'll find an Ivy snippet ready for you to include in your dependencies section. For
example (in ivy.xml):
<dependency org="org.springframework"
name="org.springframework.core" rev="3.0.0.RELEASE" conf="compile-
>runtime"/>
1.3.2 Logging
The nice thing about commons-logging is that you don't need anything else to make
your application work. It has a runtime discovery algorithm that looks for other
logging frameworks in well known places on the classpath and uses one that it
thinks is appropriate (or you can tell it which one if you need to). If nothing else is
available you get pretty nice looking logs just from the JDK (java.util.logging or JUL
for short). You should find that your Spring application works and logs happily to
the console out of the box in most situations, and that's important.
41
would probably be the Simple Logging Facade for Java (SLF4J), which is also
used by a lot of other tools that people use with Spring inside their applications.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
1.3.2.2 Using SLF4J
SLF4J provides bindings to many common logging frameworks, including JCL, and
it also does the reverse: bridges between other logging frameworks and itself. So
to use SLF4J with Spring you need to replace the commons-logging dependency with
the SLF4J-JCL bridge. Once you have done that then logging calls from within
Spring will be translated into logging calls to the SLF4J API, so if other libraries in
your application use that API, then you have a single place to configure and
manage logging.
42
A common choice might be to bridge Spring to SLF4J, and then provide explicit
binding from SLF4J to Log4J. You need to supply 4 dependencies (and exclude
the existing commons-logging): the bridge, the SLF4J API, the binding to Log4J, and
the Log4J implementation itself. In Maven you would do that like this
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
That might seem like a lot of dependencies just to get some logging. Well it is, but
it is optional, and it should behave better than the vanilla commons-logging with
respect to classloader issues, notably if you are in a strict container like an OSGi
platform. Allegedly there is also a performance benefit because the bindings are at
compile-time not runtime.
43
A more common choice amongst SLF4J users, which uses fewer steps and
generates fewer dependencies, is to bind directly to Logback. This removes the
extra binding step because Logback implements SLF4J directly, so you only need
to depend on two libaries not four (jcl-over-slf4jand logback). If you do that you
might also need to exlude the slf4j-api dependency from other external
dependencies (not Spring), because you only want one version of that API on the
classpath.
1.3.2.3 Using Log4J
To make Log4j work with the default JCL dependency ( commons-logging) all you
need to do is put Log4j on the classpath, and provide it with a configuration file
(log4j.properties or log4j.xml in the root of the classpath). So for Maven users this
is your dependency declaration:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
44
Runtime Containers with Native JCL
Many people run their Spring applications in a container that itself provides an
implementation of JCL. IBM Websphere Application Server (WAS) is the
archetype. This often causes problems, and unfortunately there is no silver bullet
solution; simply excluding commons-logging from your application is not enough in
most situations.
To be clear about this: the problems reported are usually not with JCL per se, or
even with commons-logging: rather they are to do with binding commons-logging to
another framework (often Log4J). This can fail because commons-logging changed
the way they do the runtime discovery in between the older versions (1.0) found in
some containers and the modern versions that most people use now (1.1). Spring
does not use any unusual parts of the JCL API, so nothing breaks there, but as
soon as Spring or your application tries to do any logging you can find that the
bindings to Log4J are not working.
In such cases with WAS the easiest thing to do is to invert the class loader
hierarchy (IBM calls it "parent last") so that the application controls the JCL
dependency, not the container. That option isn't always open, but there are plenty
of other suggestions in the public domain for alternative approaches, and your
mileage may vary depending on the exact version and feature set of the container.
If you have been using the Spring Framework for some time, you will be aware
that Spring has undergone two major revisions: Spring 2.0, released in October
2006, and Spring 2.5, released in November 2007. It is now time for a third
overhaul resulting in Spring 3.0.
Java SE and Java EE Support
The Spring Framework is now based on Java 5, and Java 6 is fully supported.
Furthermore, Spring is compatible with J2EE 1.4 and Java EE 5, while at the same time introducing some
early support for Java EE 6.
45
2.1 Java 5
The entire framework code has been revised to take advantage of Java 5 features
like generics, varargs and other language improvements. We have done our best
to still keep the code backwards compatible. We now have consistent use of
generic Collections and Maps, consistent use of generic FactoryBeans, and also
consistent resolution of bridge methods in the Spring AOP API. Generic
ApplicationListeners automatically receive specific event types only. All callback
interfaces such as TransactionCallback and HibernateCallback declare a generic
result value now. Overall, the Spring core codebase is now freshly revised and
optimized for Java 5.
Spring's TaskExecutor abstraction has been updated for close integration with
Java 5's java.util.concurrent facilities. We provide first-class support for Callables
and Futures now, as well as ExecutorService adapters, ThreadFactory integration,
etc. This has been aligned with JSR-236 (Concurrency Utilities for Java EE 6) as
far as possible. Furthermore, we provide support for asynchronous method
invocations through the use of the new @Async annotation (or EJB 3.1's
@Asynchronous annotation).
2.2 Improved documentation
The Spring reference documentation has also substantially been updated to reflect
all of the changes and new features for Spring 3.0. While every effort has been
made to ensure that there are no errors in this documentation, some errors may
nevertheless have crept in. If you do spot any typos or even more serious errors,
and you can spare a few cycles during lunch, please do bring the error to the
attention of the Spring team by raising an issue.
There are many excellent articles and tutorials that show how to get started with
Spring 3 features. Read them at the Spring Documentation page.
The samples have been improved and updated to take advantage of the new
features in Spring 3. Additionally, the samples have been moved out of the source
tree into a dedicated SVN repository available at:
https://anonsvn.springframework.org/svn/spring-samples/
As such, the samples are no longer distributed alongside Spring 3 and need to be downloaded
separately from the repository mentioned above. However, this documentation will continue to refer to
some samples (in particular Petclinic) to illustrate various features.
46
Note
For more information on Subversion (or in short SVN), see the project homepage
at: http://subversion.apache.org/
The framework modules have been revised and are now managed separately with
one source-tree per module jar:
org.springframework.aop
org.springframework.beans
org.springframework.context
org.springframework.context.support
org.springframework.expression
org.springframework.instrument
org.springframework.jdbc
org.springframework.jms
org.springframework.orm
org.springframework.oxm
org.springframework.test
org.springframework.transaction
org.springframework.web
org.springframework.web.portlet
org.springframework.web.servlet
org.springframework.web.struts
Note:
The spring.jar artifact that contained almost the entire framework is no longer provided.
47
We are now using a new Spring build system as known from Spring Web Flow 2.0.
This gives us:
This is a list of new features for Spring 3.0. We will cover these features in more
detail later in this section.
@MVC additions
T getBean(Class<T> requiredType)
T getBean(String name, Class<T> requiredType)
48
extended AsyncTaskExecutor supports standard Callables with Futures
Typed ApplicationListener<E>
The Spring Expression Language was created to provide the Spring community a
single, well supported expression language that can be used across all the
products in the Spring portfolio. Its language features are driven by the
requirements of the projects in the Spring portfolio, including tooling requirements
for code completion support within the Eclipse based SpringSource Tool Suite.
<bean class="mycompany.RewardsTestDatabase">
<property name="databaseName"
value="#{systemProperties.databaseName}"/>
<property name="keyGenerator"
value="#{strategyBean.databaseKeyGenerator}"/>
</bean>
This functionality is also available if you prefer to configure your components using
annotations:
@Repository
public class RewardsTestDatabase {
@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { … }
@Value("#{strategyBean.databaseKeyGenerator}")
49
public void setKeyGenerator(KeyGenerator kg) { … }
}
Some core features from the JavaConfig project have been added to the Spring
Framework now. This means that the following annotations are now directly
supported:
@Configuration
@Bean
@DependsOn
@Primary
@Lazy
@Import
@ImportResource
@Value
Here is an example of a Java class providing basic configuration using the new
JavaConfig features:
package org.example.config;
@Configuration
public class AppConfig {
private @Value("#{jdbcProperties.url}") String jdbcUrl;
private @Value("#{jdbcProperties.username}") String username;
private @Value("#{jdbcProperties.password}") String password;
@Bean
public FooService fooService() {
return new FooServiceImpl(fooRepository());
}
@Bean
public FooRepository fooRepository() {
return new HibernateFooRepository(sessionFactory());
}
50
@Bean
public SessionFactory sessionFactory() {
// wire up a session factory
AnnotationSessionFactoryBean asFactoryBean =
new AnnotationSessionFactoryBean();
asFactoryBean.setDataSource(dataSource());
// additional config
return asFactoryBean.getObject();
}
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(jdbcUrl, username, password);
}
}
To get this to work you need to add the following component scanning entry in
your minimal application context XML file.
<context:component-scan base-package="org.example.config"/>
<util:properties id="jdbcProperties"
location="classpath:org/example/config/jdbc.properties"/>
51
2.5.4 General purpose type conversion system and field formatting system
In addition, a formatter SPI has been introduced for formatting field values. This
SPI provides a simpler and more robust alternative to JavaBean PropertyEditors
for use in client environments such as Spring MVC.
Object to XML mapping functionality (OXM) from the Spring Web Services project
has been moved to the core Spring Framework now. The functionality is found in
the org.springframework.oxm package. More information on the use of
the OXM module can be found in the Marshalling XML using O/X Mappers chapter.
The most exciting new feature for the Web Tier is the support for building RESTful
web services and web applications. There are also some new annotations that can
be used in any web application.
2.5.6.2 @MVC additions
52
Additional annotations such as @CookieValue and @RequestHeaders have been added.
See Mapping cookie values with the @CookieValue annotation and Mapping
request header attributes with the @RequestHeader annotation for more
information.
We provide support for asynchronous method invocations through the use of the
new @Async annotation (or EJB 3.1's @Asynchronous annotation).
Part III. Core Technologies
This part of the reference documentation covers all of those technologies that are
absolutely integral to the Spring Framework.
53
make both unit and integration testing easier (in that the presence of setter
methods and appropriate constructors on classes makes them easier to wire
together in a test without having to set up service locator registries and suchlike)...
the chapter dedicated solely to testing will hopefully convince you of this as well.
Chapter 9, Testing
54
In short, the BeanFactory provides the configuration framework and basic
functionality, and the ApplicationContext adds more enterprise-specific
functionality. The ApplicationContext is a complete superset of the BeanFactory, and
is used exclusively in this chapter in descriptions of Spring's IoC container. For
more information on using the BeanFactory instead of the ApplicationContext, refer
to Section 3.14, “The BeanFactory”.
In Spring, the objects that form the backbone of your application and that are
managed by the Spring IoC container are called beans. A bean is an object that is
instantiated, assembled, and otherwise managed by a Spring IoC container.
Otherwise, a bean is simply one of many objects in your application. Beans, and
the dependencies among them, are reflected in the configuration metadata used
by a container.
3.2 Container overview
In most application scenarios, explicit user code is not required to instantiate one
or more instances of a Spring IoC container. For example, in a web application
scenario, a simple eight (or so) lines of boilerplate J2EE web descriptor XML in
the web.xml file of the application will typically suffice (see Section 3.13.4,
“Convenient ApplicationContext instantiation for web applications”). If you are
using the SpringSource Tool SuiteEclipse-powered development environment
or Spring Roo this boilerplate configuration can be easily created with few mouse
clicks or keystrokes.
55
The following diagram is a high-level view of how Spring works. Your application
classes are combined with configuration metadata so that after
theApplicationContext is created and initialized, you have a fully configured and
executable system or application.
3.2.1 Configuration metadata
As the preceding diagram shows, the Spring IoC container consumes a form
of configuration metadata; this configuration metadata represents how you as an
application developer tell the Spring container to instantiate, configure, and
assemble the objects in your application.
Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what
most of this chapter uses to convey key concepts and features of the Spring IoC container.
Note
XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC
container itself is totallydecoupled from the format in which this configuration metadata is
actually written.
For information about using other forms of metadata with the Spring container,
see:
56
Annotation-based configuration: Spring 2.5 introduced support for
annotation-based configuration metadata.
Java-based configuration: Starting with Spring 3.0, many features provided
by the Spring JavaConfig project became part of the core Spring
Framework. Thus you can define beans external to your application classes
by using Java rather than XML files. To use these new features, see
the @Configuration, @Bean, @Import and @DependsOn annotations.
Spring configuration consists of at least one and typically more than one bean
definition that the container must manage. XML-based configuration metadata
shows these beans configured as <bean/> elements inside a top-
level <beans/> element.
These bean definitions correspond to the actual objects that make up your
application. Typically you define service layer objects, data access objects
(DAOs), presentation objects such as Struts Action instances, infrastructure
objects such as Hibernate SessionFactories, JMS Queues, and so forth. Typically one
does not configure fine-grained domain objects in the container, because it is
usually the responsibility of DAOs and business logic to create and load domain
objects. However, you can use Spring's integration with AspectJ to configure
objects that have been created outside the control of an IoC container. See Using
AspectJ to dependency-inject domain objects with Spring.
</beans>
57
The id attribute is a string that you use to identify the individual bean definition.
The class attribute defines the type of the bean and uses the fully qualified
classname. The value of the id attribute refers to collaborating objects. The XML
for referring to collaborating objects is not shown in this example;
see Dependencies for more information.
3.2.2 Instantiating a container
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
Note
After you learn about Spring's IoC container, you may want to know more about
Spring's Resource abstraction, as described inChapter 4, Resources, which provides a convenient
mechanism for reading an InputSream from locations defined in a URI syntax. In
particular, Resource paths are used to construct applications contexts as described in Section 4.7,
“Application contexts and Resource paths”.
<bean id="petStore"
class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
</beans>
58
The following example shows the data access objects daos.xml file:
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<bean id="itemDao"
class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
It can be useful to have bean definitions span multiple XML files. Often each
individual XML configuration file represents a logical layer or module in your
architecture.
You can use the application context constructor to load bean definitions from all
these XML fragments. This constructor takes multiple Resourcelocations, as was
shown in the previous section. Alternatively, use one or more occurrences of
the <import/> element to load bean definitions from another file or files. For
example:
<beans>
<import resource="services.xml"/>
59
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
</beans>
In the preceding example, external bean definitions are loaded from three
files, services.xml, messageSource.xml, and themeSource.xml. All location paths
are relative to the definition file doing the importing, so services.xml must be in the
same directory or classpath location as the file doing the importing,
while messageSource.xml and themeSource.xml must be in a resources location
below the location of the importing file. As you can see, a leading slash is ignored,
but given that these paths are relative, it is better form not to use the slash at all.
The contents of the files being imported, including the top level <beans/> element,
must be valid XML bean definitions according to the Spring Schema or DTD.
Note
It is possible, but not recommended, to reference files in parent directories using a relative "../"
path. Doing so creates a dependency on a file that is outside the current application. In particular,
this reference is not recommended for "classpath:" URLs (for example,
"classpath:../services.xml"), where the runtime resolution process chooses the "nearest" classpath
root and then looks into its parent directory. Classpath configuration changes may lead to the
choice of a different, incorrect directory.
You can always use fully qualified resource locations instead of relative paths: for example,
"file:C:/config/services.xml" or "classpath:/config/services.xml". However, be aware that you are
coupling your application's configuration to specific absolute locations. It is generally preferable
to keep an indirection for such absolute locations, for example, through "${...}" placeholders that
are resolved against JVM system properties at runtime.
60
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
3.3 Bean overview
A Spring IoC container manages one or more beans. These beans are created
with the configuration metadata that you supply to the container, for example, in
the form of XML <bean/> definitions.
References to other beans that are needed for the bean to do its work; these
references are also called collaborators or dependencies.
Other configuration settings to set in the newly created object, for example,
the number of connections to use in a bean that manages a connection pool,
or the size limit of the pool.
This metadata translates to a set of properties that make up each bean definition.
61
Property Explained in...
3.3.1 Naming beans
Every bean has one or more identifiers. These identifiers must be unique within
the container that hosts the bean. A bean usually has only one identifier, but if it
requires more than one, the extra ones can be considered aliases.
62
characters that are legal in XML ids. This is usually not a constraint, but if you
need to use one of these special XML characters, or want to introduce other
aliases to the bean, you can also specify them in the name attribute, separated by a
comma (,), semicolon (;), or white space.
You are not required to supply a name or id for a bean. If no name or id is supplied
explicitly, the container generates a unique name for that bean. However, if you
want to refer to that bean by name, through the use of the ref element or Service
Location style lookup, you must provide a name. Motivations for not supplying a
name are related to using inner beans and autowiring collaborators.
Bean naming conventions
The convention is to use the standard Java convention for instance field names when naming beans. That
is, bean names start with a lowercase letter, and are camel-cased from then on. Examples of such names
would be (without quotes)'accountManager', 'accountService','userDao', 'loginController',
and so forth.
Naming beans consistently makes your configuration easier to read and understand, and if you are using
Spring AOP it helps a lot when applying advice to a set of beans related by name.
In a bean definition itself, you can supply more than one name for the bean, by
using a combination of up to one name specified by the id attribute, and any
number of other names in the name attribute. These names can be equivalent
aliases to the same bean, and are useful for some situations, such as allowing
each component in an application to refer to a common dependency by using a
bean name that is specific to that component itself.
Specifying all aliases where the bean is actually defined is not always adequate,
however. It is sometimes desirable to introduce an alias for a bean that is defined
elsewhere. This is commonly the case in large systems where configuration is split
amongst each subsystem, each subsystem having its own set of object definitions.
In XML-based configuration metadata, you can use the <alias/> element to
accomplish this.
In this case, a bean in the same container which is named fromName, may also after
the use of this alias definition, be referred to as toName.
63
For example, the configuration metadata for subsystem A may refer to a
DataSource via the name 'subsystemA-dataSource. The configuration metadata
for subsystem B may refer to a DataSource via the name 'subsystemB-
dataSource'. When composing the main application that uses both these
subsystems the main application refers to the DataSource via the name 'myApp-
dataSource'. To have all three names refer to the same object you add to the
MyApp configuration metadata the following aliases definitions:
Now each component and the main application can refer to the dataSource
through a name that is unique and guaranteed not to clash with any other
definition (effectively creating a namespace), yet they refer to the same bean.
3.3.2 Instantiating beans
A bean definition essentially is a recipe for creating one or more objects. The
container looks at the recipe for a named bean when asked, and uses the
configuration metadata encapsulated by that bean definition to create (or acquire)
an actual object.
If you use XML-based configuration metadata, you specify the type (or class) of
object that is to be instantiated in the class attribute of the <bean/>element.
This class attribute, which internally is a Class property on
a BeanDefinition instance, is usually mandatory. (For exceptions,
seeSection 3.3.2.3, “Instantiation using an instance factory
method” and Section 3.7, “Bean definition inheritance”.) You use the Class property
in one of two ways:
Typically, to specify the bean class to be constructed in the case where the
container itself directly creates the bean by calling its constructor reflectively,
somewhat equivalent to Java code using the new operator.
64
If you want to configure a bean definition for a static nested class, you have to use the binaryname of the
inner class.
For example, if you have a class called Foo in thecom.example package, and this Foo class has
astatic inner class called Bar, the value of the'class' attribute on a bean definition would be...
com.example.Foo$Bar
Notice the use of the $ character in the name to separate the inner class name from the outer class name.
When you create a bean by the constructor approach, all normal classes are
usable by and compatible with Spring. That is, the class being developed does not
need to implement any specific interfaces or to be coded in a specific fashion.
Simply specifying the bean class should suffice. However, depending on what type
of IoC you use for that specific bean, you may need a default (empty) constructor.
The Spring IoC container can manage virtually any class you want it to manage; it
is not limited to managing true JavaBeans. Most Spring users prefer actual
JavaBeans with only a default (no-argument) constructor and appropriate setters
and getters modeled after the properties in the container. You can also have more
exotic non-bean-style classes in your container. If, for example, you need to use a
legacy connection pool that absolutely does not adhere to the JavaBean
specification, Spring can manage it as well.
With XML-based configuration metadata you can specify your bean class as
follows:
For details about the mechanism for supplying arguments to the constructor (if
required) and setting object instance properties after the object is constructed,
see Injecting Dependencies.
When defining a bean that you create with a static factory method, you use
the class attribute to specify the class containing the static factory method and an
attribute named factory-method to specify the name of the factory method itself.
You should be able to call this method (with optional arguments as described later)
65
and return a live object, which subsequently is treated as if it had been created
through a constructor. One use for such a bean definition is to call static factories
in legacy code.
The following bean definition specifies that the bean will be created by calling a
factory-method. The definition does not specify the type (class) of the returned
object, only the class containing the factory method. In this example,
the createInstance() method must be a static method.
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
For details about the mechanism for supplying (optional) arguments to the factory
method and setting object instance properties after the object is returned from the
factory, see Dependencies and configuration in detail.
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
66
private DefaultServiceLocator() {}
One factory class can also hold more than one factory method as shown here:
<bean id="accountService"
factory-bean="serviceLocator"
factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
private DefaultServiceLocator() {}
This approach shows that the factory bean itself can be managed and configured
through dependency injection (DI). See Dependencies and configuration in detail.
Note
3.4 Dependencies
A typical enterprise application does not consist of a single object (or bean in the
Spring parlance). Even the simplest application has a few objects that work
together to present what the end-user sees as a coherent application. This next
67
section explains how you go from defining a number of bean definitions that stand
alone to a fully realized application where objects collaborate to achieve a goal.
3.4.1 Dependency injection
Code is cleaner with the DI principle and decoupling is more effective when
objects are provided with their dependencies. The object does not look up its
dependencies, and does not know the location or class of the dependencies. As
such, your classes become easier to test, in particular when the dependencies are
on interfaces or abstract base classes, which allow for stub or mock
implementations to be used in unit tests.
68
}
package x.y;
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
</beans>
When another bean is referenced, the type is known, and matching can occur (as
was the case with the preceding example). When a simple type is used, such
as <value>true<value>, Spring cannot determine the type of the value, and so
cannot match by type without help. Consider the following class:
package examples;
69
public class ExampleBean {
In the preceding scenario, the container can use type matching with simple types if
you explicitly specify the type of the constructor argument using the type attribute.
For example:
As of Spring 3.0 you can also use the constructor parameter name for value
disambiguation:
70
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>
Keep in mind that to make this work out of the box your code must be compiled
with the debug flag enabled so that Spring can look up the parameter name from
the constructor. If you can't compile your code with debug flag (or don't want to)
you can use @ConstructorProperties JDK annotation to explicitly name your
constructor arguments. The sample class would then have to look as follows:
package examples;
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
The following example shows a class that can only be dependency-injected using
pure setter injection. This class is conventional Java. It is a POJO that has no
dependencies on container specific interfaces, base classes or annotations.
71
The ApplicationContext supports constructor- and setter-based DI for the beans it
manages. It also supports setter-based DI after some dependencies are already
injected through the constructor approach. You configure the dependencies in the
form of a BeanDefinition, which you use with PropertyEditor instances to convert
properties from one format to another. However, most Spring users do not work
with these classes directly (programmatically), but rather with an XML definition file
that is then converted internally into instances of these classes, and used to load
an entire Spring IoC container instance.
Constructor-based or setter-based DI?
Since you can mix both, Constructor- and Setter-based DI, it is a good rule of thumb to use constructor
arguments for mandatory dependencies and setters for optional dependencies. Note that the use of
a @Required annotation on a setter can be used to make setters required dependencies.
The Spring team generally advocates setter injection, because large numbers of constructor arguments
can get unwieldy, especially when properties are optional. Setter methods also make objects of that class
amenable to reconfiguration or re-injection later. Management through JMX MBeansis a compelling use
case.
Some purists favor constructor-based injection. Supplying all object dependencies means that the object is
always returned to client (calling) code in a totally initialized state. The disadvantage is that the object
becomes less amenable to reconfiguration and re-injection.
Use the DI that makes the most sense for a particular class. Sometimes, when dealing with third-party
classes to which you do not have the source, the choice is made for you. A legacy class may not expose
any setter methods, and so constructor injection is the only available DI.
72
By default Spring can convert a value supplied in string format to all built-in
types, such as int, long, String, boolean, etc.
The Spring container validates the configuration of each bean as the container is
created, including the validation of whether bean reference properties refer to valid
beans. However, the bean properties themselves are not set until the bean is
actually created. Beans that are singleton-scoped and set to be pre-instantiated
(the default) are created when the container is created. Scopes are defined
in Section 3.5, “Bean scopes” Otherwise, the bean is created only when it is
requested. Creation of a bean potentially causes a graph of beans to be created,
as the bean's dependencies and its dependencies' dependencies (and so on) are
created and assigned.
Circular dependencies
If you use predominantly constructor injection, it is possible to create an unresolvable circular dependency
scenario.
For example: Class A requires an instance of class B through constructor injection, and class B requires
an instance of class A through constructor injection. If you configure beans for classes A and B to be
injected into each other, the Spring IoC container detects this circular reference at runtime, and throws
a BeanCurrentlyInCreationException.
One possible solution is to edit the source code of some classes to be configured by setters rather than
constructors. Alternatively, avoid constructor injection and use setter injection only. In other words,
although it is not recommended, you can configure circular dependencies with setter injection.
Unlike the typical case (with no circular dependencies), a circular dependency between bean A and bean
B forces one of the beans to be injected into the other prior to being fully initialized itself (a classic
chicken/egg scenario).
You can generally trust Spring to do the right thing. It detects configuration
problems, such as references to non-existent beans and circular dependencies, at
container load-time. Spring sets properties and resolves dependencies as late as
possible, when the bean is actually created. This means that a Spring container
which has loaded correctly can later generate an exception when you request an
object if there is a problem creating that object or one of its dependencies. For
example, the bean throws an exception as a result of a missing or invalid property.
This potentially delayed visibility of some configuration issues is
why ApplicationContext implementations by default pre-instantiate singleton beans.
At the cost of some upfront time and memory to create these beans before they
are actually needed, you discover configuration issues when
the ApplicationContext is created, not later. You can still override this default
behavior so that singleton beans will lazy-initialize, rather than be pre-instantiated.
73
If no circular dependencies exist, when one or more collaborating beans are being
injected into a dependent bean, each collaborating bean is totally configured prior
to being injected into the dependent bean. This means that if bean A has a
dependency on bean B, the Spring IoC container completely configures bean B
prior to invoking the setter method on bean A. In other words, the bean is
instantiated (if not a pre-instantiated singleton), its dependencies are set, and the
relevant lifecycle methods (such as a configured init method or the IntializingBean
callback method) are invoked.
In the preceding example, setters are declared to match against the properties
specified in the XML file. The following example uses constructor-based DI:
74
<bean id="exampleBean" class="examples.ExampleBean">
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
// a private constructor
private ExampleBean(...) {
...
}
75
// considered the dependencies of the bean that is returned,
// regardless of how those arguments are actually used.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
As mentioned in the previous section, you can define bean properties and
constructor arguments as references to other managed beans (collaborators), or
as values defined inline. Spring's XML-based configuration metadata supports
sub-element types within its <property/> and<constructor-arg/> elements for this
purpose.
76
The following example uses the p-namespace for even more succinct XML
configuration.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
The preceding XML is more succinct; however, typos are discovered at runtime
rather than design time, unless you use an IDE such as IntelliJ IDEAor
the SpringSource Tool Suite (STS) that support automatic property completion
when you create bean definitions. Such IDE assistance is highly recommended.
<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
The idref element
77
The idref element is simply an error-proof way to pass the id (string value - not a
reference) of another bean in the container to a <constructor-
arg/> or <property/> element.
The above bean definition snippet is exactly equivalent (at runtime) to the following
snippet:
The first form is preferable to the second, because using the idref tag allows the
container to validate at deployment time that the referenced, named bean actually
exists. In the second variation, no validation is performed on the value that is
passed to the targetName property of the clientbean. Typos are only discovered
(with most likely fatal results) when the client bean is actually instantiated. If
the client bean is a prototype bean, this typo and the resulting exception may only
be discovered long after the container is deployed.
Additionally, if the referenced bean is in the same XML unit, and the bean name is
the bean id, you can use the local attribute, which allows the XML parser itself to
validate the bean id earlier, at XML document parse time.
<property name="targetName">
<!-- a bean with id 'theTargetBean' must exist; otherwise an exception will be
thrown -->
<idref local="theTargetBean"/>
</property>
A common place (at least in versions earlier than Spring 2.0) where the <idref/>
element brings value is in the configuration of AOP interceptors in
aProxyFactoryBean bean definition. Using <idref/> elements when you specify the
interceptor names prevents you from misspelling an interceptor id.
78
3.4.2.2 References to other beans (collaborators)
<ref bean="someBean"/>
Specifying the target bean through the local attribute leverages the ability of the
XML parser to validate XML id references within the same file. The value of
the local attribute must be the same as the id attribute of the target bean. The
XML parser issues an error if no matching element is found in the same file. As
such, using the local variant is the best choice (in order to know about errors as
early as possible) if the target bean is in the same XML file.
<ref local="someBean"/>
79
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent
bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>
3.4.2.3 Inner beans
An inner bean definition does not require a defined id or name; the container
ignores these values. It also ignores the scope flag. Inner beans
arealways anonymous and they are always scoped as prototypes. It is not possible
to inject inner beans into collaborating beans other than into the enclosing bean.
3.4.2.4 Collections
80
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
The value of a map key or value, or a set value, can also again be any of the
following elements:
Collection merging
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
81
<props>
<prop key="administrator">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the *child* collection definition -->
<props merge="true">
<prop key="sales">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
<beans>
[email protected]
[email protected]
[email protected]
The child Properties collection's value set inherits all property elements from the
parent <props/>, and the child's value for the support value overrides the value in
the parent collection.
You cannot merge different collection types (such as a Map and a List), and if you
do attempt to do so an appropriate Exception is thrown. Themerge attribute must be
specified on the lower, inherited, child definition; specifying the merge attribute on a
82
parent collection definition is redundant and will not result in the desired merging.
The merging feature is available only in Spring 2.0 and later.
In Java 5 and later, you can use strongly typed collections (using generic types).
That is, it is possible to declare a Collection type such that it can only
contain String elements (for example). If you are using Spring to dependency-
inject a strongly-typed Collection into a bean, you can take advantage of Spring's
type-conversion support such that the elements of your strongly-
typed Collection instances are converted to the appropriate type prior to being
added to the Collection.
Spring treats empty arguments for properties and the like as empty Strings. The
following XML-based configuration metadata snippet sets the email property to the
empty String value ("")
83
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
The following example shows two XML snippets that resolve to the same result:
The first uses standard XML format and the second uses the p-namespace.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
84
The example shows an attribute in the p-namespace called email in the bean
definition. This tells Spring to include a property declaration. As previously
mentioned, the p-namespace does not have a schema definition, so you can set
the name of the attribute to the property name.
This next example includes two more bean definitions that both have a reference
to another bean:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
As you can see, this example includes not only a property value using the p-
namespace, but also uses a special format to declare property references.
Whereas the first bean definition uses <property name="spouse" ref="jane"/> to
create a reference from bean john to bean jane, the second bean definition
uses p:spouse-ref="jane" as an attribute to do the exact same thing. In this
case spouse is the property name, whereas the -ref part indicates that this is not a straight
value but rather a reference to another bean.
Note
The p-namespace is not as flexible as the standard XML format. For example, the format for
declaring property references clashes with properties that end in Ref, whereas the standard XML
format does not. We recommend that you choose your approach carefully and communicate this
to your team members, to avoid producing XML documents that use all three approaches at the
same time.
85
3.4.2.7 Compound property names
You can use compound or nested property names when you set bean properties,
as long as all components of the path except the final property name are not null.
Consider the following bean definition.
3.4.3 Using depends-on
If a bean is a dependency of another that usually means that one bean is set as a
property of another. Typically you accomplish this with the <ref/>element in XML-
based configuration metadata. However, sometimes dependencies between beans
are less direct; for example, a static initializer in a class needs to be triggered,
such as database driver registration. The depends-on attribute can explicitly force
one or more beans to be initialized before the bean using this element is initialized.
The following example uses the depends-on attribute to express a dependency on a
single bean:
Note
86
The depends-on attribute in the bean definition can specify both an initialization time
dependency and, in the case of singletonbeans only, a corresponding destroy time dependency.
Dependent beans that define a depends-on relationship with a given bean are destroyed first,
prior to the given bean itself being destroyed. Thus depends-on can also control shutdown order.
3.4.4 Lazy-initialized beans
You can also control lazy-initialization at the container level by using the default-
lazy-init attribute on the <beans/> element; for example:
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
87
3.4.5 Autowiring collaborators
When using XML-based configuration metadata[2], you specify autowire mode for a
bean definition with the autowire attribute of the <bean/>element. The autowiring
functionality has five modes. You specify autowiring per bean and thus can choose
which ones to autowire.
Table 3.2. Autowiring modes
Explanation
fault) No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended
er deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents
cture of a system.
owiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example,
n definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..)method), Spring looks f
n definition named master, and uses it to set the property.
ws a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a
eption is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, not
pens; the property is not set.
logous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in
ainer, a fatal error is raised.
88
With byType or constructor autowiring mode, you can wire arrays and typed-
collections. In such cases all autowire candidates within the container that match
the expected type are provided to satisfy the dependency. You can autowire
strongly-typed Maps if the expected key type is String. An autowired Maps values
will consist of all bean instances that match the expected type, and the Maps keys
will contain the corresponding bean names.
Autowiring is less exact than explicit wiring. Although, as noted in the above
table, Spring is careful to avoid guessing in case of ambiguity that might
have unexpected results, the relationships between your Spring-managed
objects are no longer documented explicitly.
Multiple bean definitions within the container may match the type specified
by the setter method or constructor argument to be autowired. For arrays,
collections, or Maps, this is not necessarily a problem. However for
dependencies that expect a single value, this ambiguity is not arbitrarily
resolved. If no unique bean definition is available, an exception is thrown.
89
Designate a single bean definition as the primary candidate by setting
the primary attribute of its <bean/> element to true.
If you are using Java 5 or later, implement the more fine-grained control
available with annotation-based configuration, as described inSection 3.9,
“Annotation-based container configuration”.
On a per-bean basis, you can exclude a bean from autowiring. In Spring's XML
format, set the autowire-candidate attribute of the <bean/> element to false; the
container makes that specific bean definition unavailable to the autowiring
infrastructure (including annotation style configurations such as @Autowired).
You can also limit autowire candidates based on pattern-matching against bean
names. The top-level <beans/> element accepts one or more patterns within
its default-autowire-candidates attribute. For example, to limit autowire candidate
status to any bean whose name ends withRepository, provide a value of
*Repository. To provide multiple patterns, define them in a comma-separated list.
An explicit value of true or falsefor a bean definitions autowire-candidate attribute
always takes precedence, and for such beans, the pattern matching rules do not
apply.
These techniques are useful for beans that you never want to be injected into
other beans by autowiring. It does not mean that an excluded bean cannot itself be
configured using autowiring. Rather, the bean itself is not a candidate for
autowiring other beans.
3.4.6 Method injection
90
a getBean("B") call to the container ask for (a typically new) bean B instance every
time bean A needs it. The following is an example of this approach:
// Spring-API imports
import org.springframework.beans.BeansException;
import org.springframework.context.Applicationcontext;
import org.springframework.context.ApplicationContextAware;
The preceding is not desirable, because the business code is aware of and
coupled to the Spring Framework. Method Injection, a somewhat advanced feature
of the Spring IoC container, allows this use case to be handled in a clean fashion.
You can read more about the motivation for Method Injection in this blog entry.
Note
91
For this dynamic subclassing to work, you must have the CGLIB jar(s) in your classpath. The
class that the Spring container will subclass cannot be final, and the method to be overridden
cannot be final either. Also, testing a class that has an abstractmethod requires you to subclass
the class yourself and to supply a stub implementation of the abstract method. Finally, objects
that have been the target of method injection cannot be serialized.
Looking at the CommandManager class in the previous code snippet, you see that the
Spring container will dynamically override the implementation of
the createCommand() method. Your CommandManager class will not have any Spring
dependencies, as can be seen in the reworked example:
package fiona.apple;
92
<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>
Tip
A less useful form of method injection than lookup method Injection is the ability to
replace arbitrary methods in a managed bean with another method
implementation. Users may safely skip the rest of this section until the functionality
is actually needed.
93
A class implementing
the org.springframework.beans.factory.support.MethodReplacer interface provides the
new method definition.
The bean definition to deploy the original class and specify the method override
would look like this:
java.lang.String
String
Str
94
3.5 Bean scopes
When you create a bean definition, you create a recipe for creating actual
instances of the class defined by that bean definition. The idea that a bean
definition is a recipe is important, because it means that, as with a class, you can
create many object instances from a single recipe.
You can control not only the various dependencies and configuration values that
are to be plugged into an object that is created from a particular bean definition,
but also the scope of the objects created from a particular bean definition. This
approach is powerful and flexible in that you canchoose the scope of the objects
you create through configuration instead of having to bake in the scope of an
object at the Java class level. Beans can be defined to be deployed in one of a
number of scopes: out of the box, the Spring Framework supports five scopes,
three of which are available only if you use a web-aware ApplicationContext.
The following scopes are supported out of the box. You can also create a custom
scope.
Table 3.3. Bean scopes
Description
fault) Scopes a single bean definition to a single object instance per Spring IoC container.
pes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a b
ated off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
pes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aw
ingApplicationContext.
pes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. O
d in the context of a web-aware Spring ApplicationContext.
Thread-scoped beans
As of Spring 3.0, a thread scope is available, but is not registered by default. For more
information, see the documentation forSimpleThreadScope. For instructions on how to register
this or any other custom scope, see Section 3.5.5.2, “Using a custom scope”.
95
3.5.1 The singleton scope
To put it another way, when you define a bean definition and it is scoped as a
singleton, the Spring IoC container creates exactly one instance of the object
defined by that bean definition. This single instance is stored in a cache of such
singleton beans, and all subsequent requests and references for that named bean
return the cached object.
Spring's concept of a singleton bean differs from the Singleton pattern as defined
in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the
scope of an object such that one and only one instance of a particular class is
created per ClassLoader. The scope of the Spring singleton is best described
as per container and per bean. This means that if you define one bean for a
particular class in a single Spring container, then the Spring container creates
one and only one instance of the class defined by that bean definition. The
singleton scope is the default scope in Spring. To define a bean as a singleton in
XML, you would write, for example:
96
<bean id="accountService" class="com.foo.DefaultAccountService"/>
The following diagram illustrates the Spring prototype scope. A data access object
(DAO) is not typically configured as a prototype, because a typical DAO does not
hold any conversational state; it was just easier for this author to reuse the core of
the singleton diagram.
97
<!-- using spring-beans-2.0.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService"
scope="prototype"/>
In contrast to the other scopes, Spring does not manage the complete lifecycle of
a prototype bean: the container instantiates, configures, and otherwise assembles
a prototype object, and hands it to the client, with no further record of that
prototype instance. Thus, although initializationlifecycle callback methods are
called on all objects regardless of scope, in the case of prototypes,
configured destruction lifecycle callbacks are notcalled. The client code must clean
up prototype-scoped objects and release expensive resources that the prototype
bean(s) are holding. To get the Spring container to release resources held by
prototype-scoped beans, try using a custom bean post-processor, which holds a
reference to beans that need to be cleaned up.
However, suppose you want the singleton-scoped bean to acquire a new instance
of the prototype-scoped bean repeatedly at runtime. You cannot dependency-
inject a prototype-scoped bean into your singleton bean, because that injection
occurs only once, when the Spring container is instantiating the singleton bean
and resolving and injecting its dependencies. If you need a new instance of a
prototype bean at runtime more than once, see Section 3.4.6, “Method injection”
98
containers such as the ClassPathXmlApplicationContext, you get
an IllegalStateException complaining about an unknown bean scope.
How you accomplish this initial setup depends on your particular Servlet
environment..
If you access scoped beans within Spring Web MVC, in effect, within a request
that is processed by the Spring DispatcherServlet, orDispatcherPortlet, then no
special setup is necessary: DispatcherServlet and DispatcherPortlet already expose
all relevant state.
If you use a Servlet 2.4+ web container, with requests processed outside of
Spring's DispatcherServlet (for example, when using JSF or Struts), you need to
add the following javax.servlet.ServletRequestListener to the declarations in your
web applications web.xml file:
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
<web-app>
..
<filter>
99
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
DispatcherServlet, RequestContextListener and RequestContextFilter alldo exactly
the same thing, namely bind the HTTP request object to the Thread that is servicing
that request. This makes beans that are request- and session-scoped available
further down the call chain.
3.5.4.2 Request scope
3.5.4.3 Session scope
100
they are particular to an individual HTTP Session. When the HTTP Session is
eventually discarded, the bean that is scoped to that particular HTTP Session is
also discarded.
If you write a standard Servlet-based web application and you define one or more
beans as having global session scope, the standard HTTPSession scope is used,
and no error is raised.
The Spring IoC container manages not only the instantiation of your objects (beans), but also the
wiring up of collaborators (or dependencies). If you want to inject (for example) an HTTP request
scoped bean into another bean, you must inject an AOP proxy in place of the scoped bean. That is, you
need to inject a proxy object that exposes the same public interface as the scoped object but that can
also retrieve the real, target object from the relevant scope (for example, an HTTP request) and
delegate method calls onto the real object.
Note
You do not need to use the <aop:scoped-proxy/> in conjunction with beans that are scoped
as singletons or prototypes. If you try to create a scoped proxy for a singleton bean,
the BeanCreationException is raised.
The configuration in the following example is only one line, but it is important to
understand the “why” as well as the “how” behind it.
101
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
</bean>
</beans>
This is not the behavior you want when injecting a shorter-lived scoped bean into a
longer-lived scoped bean, for example injecting an HTTP Session-scoped
collaborating bean as a dependency into singleton bean. Rather, you need a
102
single userManager object, and for the lifetime of an HTTP Session, you need
a userPreferences object that is specific to said HTTP Session. Thus the container
creates an object that exposes the exact same public interface as
the UserPreferences class (ideally an object that is a UserPreferences instance) which
can fetch the realUserPreferences object from the scoping mechanism (HTTP
request, Session, etc.). The container injects this proxy object into
the userManagerbean, which is unaware that this UserPreferences reference is a
proxy. In this example, when a UserManager instance invokes a method on the
dependency-injected UserPreferences object, it actually is invoking a method on the
proxy. The proxy then fetches the real UserPreferences object from (in this case) the
HTTP Session, and delegates the method invocation onto the retrieved
real UserPreferences object.
Thus you need the following, correct and complete, configuration when
injecting request-, session-, and globalSession-scoped beans into collaborating
objects:
By default, when the Spring container creates a proxy for a bean that is marked up
with the <aop:scoped-proxy/> element, a CGLIB-based class proxy is created. This
means that you need to have the CGLIB library in the classpath of your
application.
Note: CGLIB proxies only intercept public method calls! Do not call non-public
methods on such a proxy; they will not be delegated to the scoped target object.
Alternatively, you can configure the Spring container to create standard JDK
interface-based proxies for such scoped beans, by specifying falsefor the value of
the proxy-target-class attribute of the <aop:scoped-proxy/> element. Using JDK
interface-based proxies means that you do not need additional libraries in your
application classpath to effect such proxying. However, it also means that the
class of the scoped bean must implement at least one interface, and that
all collaborators into which the scoped bean is injected must reference the bean
through one of its interfaces.
103
<!-- DefaultUserPreferences implements the UserPreferences interface -->
<bean id="userPreferences" class="com.foo.DefaultUserPreferences" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
3.5.5 Custom scopes
As of Spring 2.0, the bean scoping mechanism is extensible. You can define your
own scopes, or even redefine existing scopes, although the latter is considered
bad practice and you cannot override the built-in singleton and prototype scopes.
To integrate your custom scope(s) into the Spring container, you need to
implement the org.springframework.beans.factory.config.Scopeinterface, which is
described in this section. For an idea of how to implement your own scopes, see
the Scope implementations that are supplied with the Spring Framework itself and
the Scope Javadoc, which explains the methods you need to implement in more
detail.
The Scope interface has four methods to get objects from the scope, remove them
from the scope, and allow them to be destroyed.
The following method returns the object from the underlying scope. The session
scope implementation, for example, returns the session-scoped bean (and if it
does not exist, the method returns a new instance of the bean, after having bound
it to the session for future reference).
The following method removes the object from the underlying scope. The session
scope implementation for example, removes the session-scoped bean from the
underlying session. The object should be returned, but you can return null if the
object with the specified name is not found.
104
Object remove(String name)
The following method registers the callbacks the scope should execute when it is
destroyed or when the specified object in the scope is destroyed. Refer to the
Javadoc or a Spring scope implementation for more information on destruction
callbacks.
The following method obtains the conversation identifier for the underlying scope.
This identifier is different for each scope. For a session scoped implementation,
this identifier can be the session identifier.
String getConversationId()
After you write and test one or more custom Scope implementations, you need to
make the Spring container aware of your new scope(s). The following method is
the central method to register a new Scope with the Spring container:
Suppose that you write your custom Scope implementation, and then register it as below.
Note
The example below uses SimpleThreadScope which is included with Spring, but not registered
by default. The instructions would be the same for your own custom Scope implementations.
105
Scope threadScope = new SimpleThreadScope();
beanFactory.registerScope("thread", threadScope);
You then create bean definitions that adhere to the scoping rules of your
custom Scope:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread">
<bean
class="org.springframework.context.support.SimpleThreadScope"/>
</entry>
</map>
</property>
</bean>
</beans>
Note
106
3.6 Customizing the nature of a bean
3.6.1 Lifecycle callbacks
To interact with the container's management of the bean lifecycle, you can
implement the Spring InitializingBean and DisposableBean interfaces. The container
calls afterPropertiesSet() for the former and destroy() for the latter to allow the
bean to perform certain actions upon initialization and destruction of your beans.
You can also achieve the same integration with the container without coupling your
classes to Spring interfaces through the use of init-method and destroy method
object definition metadata.
3.6.1.1 Initialization callbacks
107
public void init() {
// do some initialization work
}
}
3.6.1.2 Destruction callbacks
108
public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
When you write initialization and destroy method callbacks that do not use the
Spring-specific InitializingBean and DisposableBean callback interfaces, you
typically write methods with names such as init(), initialize(), dispose(), and so
on. Ideally, the names of such lifecycle callback methods are standardized across
a project so that all developers use the same method names and ensure
consistency.
You can configure the Spring container to look for named initialization and destroy
callback method names on every bean. This means that you, as an application
developer, can write your application classes and use an initialization callback
called init(), without having to configure an init-method="init" attribute with each
bean definition. The Spring IoC container calls that method when the bean is
created (and in accordance with the standard lifecycle callback contract described
previously). This feature also enforces a consistent naming convention for
initialization and destroy method callbacks.
109
<beans default-init-method="init">
</beans>
You configure destroy method callbacks similarly (in XML, that is) by using
the default-destroy-method attribute on the top-level <beans/> element.
Where existing bean classes already have callback methods that are named at
variance with the convention, you can override the default by specifying (in XML,
that is) the method name using the init-method and destroy-method attributes of the
<bean/> itself.
As of Spring 2.5, you have three options for controlling bean lifecycle behavior:
the InitializingBean and DisposableBean callback interfaces;
custom init() and destroy() methods; and
the @PostConstruct and @PreDestroy annotations. You can combine these mechanisms
to control a given bean.
Note
110
If multiple lifecycle mechanisms are configured for a bean, and each mechanism is configured
with a different method name, then each configured method is executed in the order listed below.
However, if the same method name is configured - for example, init() for an initialization
method - for more than one of these lifecycle mechanisms, that method is executed once, as
explained in the preceding section.
Multiple lifecycle mechanisms configured for the same bean, with different
initialization methods, are called as follows:
A custom configured init() method
A custom configured destroy() method
The Lifecycle interface defines the essential methods for any object that has its
own lifecycle requirements (e.g. starts and stops some background process):
void start();
void stop();
boolean isRunning();
Any Spring-managed object may implement that interface. Then, when the
ApplicationContext itself starts and stops, it will cascade those calls to all Lifecycle
implementations defined within that context. It does this by delegating to
a LifecycleProcessor:
111
public interface LifecycleProcessor extends Lifecycle {
void onRefresh();
void onClose();
int getPhase();
boolean isAutoStartup();
When starting, the objects with the lowest phase start first, and when stopping, the
reverse order is followed. Therefore, an object that implements SmartLifecycle and
whose getPhase() method returns Integer.MIN_VALUE would be among the first to
start and the last to stop. At the other end of the spectrum, a phase value
of Integer.MAX_VALUE would indicate that the object should be started last and
stopped first (likely because it depends on other processes to be running). When
considering the phase value, it's also important to know that the default phase for
any "normal" Lifecycleobject that does not implement SmartLifecycle would be 0.
Therefore, any negative phase value would indicate that an object should start
112
before those standard components (and stop after them), and vice versa for any
positive phase value.
As you can see the stop method defined by SmartLifecycle accepts a callback. Any
implementation must invoke that callback's run() method after that
implementation's shutdown process is complete. That enables asynchronous
shutdown where necessary since the default implementation of
the LifecycleProcessor interface, DefaultLifecycleProcessor, will wait up to its
timeout value for the group of objects within each phase to invoke that callback.
The default per-phase timeout is 30 seconds. You can override the default lifecycle
processor instance by defining a bean named "lifecycleProcessor" within the
context. If you only want to modify the timeout, then defining the following would
be sufficient:
<bean id="lifecycleProcessor"
class="org.springframework.context.support.DefaultLifecycleProcessor">
<!-- timeout value in milliseconds -->
<property name="timeoutPerShutdownPhase" value="10000"/>
</bean>
113
If you are using Spring's IoC container in a non-web application environment; for
example, in a rich client desktop environment; you register a shutdown hook with
the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy
methods on your singleton beans so that all resources are released. Of course,
you must still configure and implement these destroy callbacks correctly.
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// main method exits, hook is called prior to the app shutting down...
}
}
3.6.2 ApplicationContextAware and BeanNameAware
114
methods of the ApplicationContext provide access to file resources, publishing
application events, and accessing a MessageSource. These additional features
are described in Section 3.13, “Additional Capabilities of the ApplicationContext”
The callback is invoked after population of normal bean properties but before an
initialization callback such as InitializingBeansafterPropertiesSet or a custom init-
method.
3.6.3 Other Aware interfaces
Table 3.4. Aware interfaces
115
Name Injected Dependency Explained in...
and BeanNameAware”
Section 3.13, “Additional
ApplicationEventPublisherAwar Event publisher of the
e Capabilities of the
enclosing ApplicationContext
ApplicationContext”
BeanClassLoaderAware
Class loader used to load the bean Section 3.3.2, “Instantiating
classes. beans”
Section 3.6.2,
BeanFactoryAware Declaring BeanFactory “ApplicationContextAware
and BeanNameAware”
Section 3.6.2,
BeanNameAware Name of the declaring bean “ApplicationContextAware
and BeanNameAware”
Resource
adapter BootstrapContext the
BootstrapContextAware container runs in. Typically Chapter 23, JCA CCI
available only in JCA
aware ApplicationContexts
Section 7.8.4, “Load-time
Defined weaver for processing
LoadTimeWeaverAware weaving with AspectJ in the
class definition at load time
Spring Framework”
Current PortletConfig the
PortletConfigAware
container runs in. Valid only in a Chapter 18, Portlet MVC
web-aware Framework
Spring ApplicationContext
116
Name Injected Dependency Explained in...
web-aware
Spring ApplicationContext
Current ServletConfig the
ServletConfigAware
container runs in. Valid only in a Chapter 15, Web MVC
web-aware framework
Spring ApplicationContext
Current ServletContext the
ServletContextAware
container runs in. Valid only in a Chapter 15, Web MVC
web-aware framework
Spring ApplicationContext
Note again that usage of these interfaces ties your code to the Spring API and
does not follow the Inversion of Control style. As such, they are recommended for
infrastructure beans that require programmatic access to the container.
117
</bean>
<bean id="inheritsWithDifferentClass"
class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
</bean>
A child bean definition uses the bean class from the parent definition if none is
specified, but can also override it. In the latter case, the child bean class must be
compatible with the parent, that is, it must accept the parent's property values.
A child bean definition inherits constructor argument values, property values, and
method overrides from the parent, with the option to add new values. Any
initialization method, destroy method, and/or static factory method settings that
you specify will override the corresponding parent settings.
The preceding example explicitly marks the parent bean definition as abstract by
using the abstract attribute. If the parent definition does not specify a class,
explicitly marking the parent bean definition as abstract is required, as follows:
The parent bean cannot be instantiated on its own because it is incomplete, and it
is also explicitly marked as abstract. When a definition isabstract like this, it is
usable only as a pure template bean definition that serves as a parent definition for
child definitions. Trying to use such anabstract parent bean on its own, by referring
to it as a ref property of another bean or doing an explicit getBean() call with the
parent bean id, returns an error. Similarly, the container's
internal preInstantiateSingletons() method ignores bean definitions that are defined as abstract.
118
Note
Note
BeanPostProcessors operate on bean (or object) instances; that is to say, the Spring IoC
container instantiates a bean instance and then BeanPostProcessor interfaces do their work.
To change the actual bean definition (that is, the recipe that defines the bean), you instead need to
use aBeanFactoryPostProcessor, described below in Section 3.8.2, “Customizing
119
configuration metadata with BeanFactoryPostProcessor interface”.
The org.springframework.beans.factory.config.BeanPostProcessor interface consists
of exactly two callback methods. When such a class is registered as a post-
processor with the container, for each bean instance that is created by the
container, the post-processor gets a callback from the container
both before container initialization methods (such as afterPropertiesSet and any
declared init method) are called, and also afterwards. The post-processor can take
any action with the bean instance, including ignoring the callback completely. A
bean post-processor typically checks for callback interfaces, or may wrap a bean
with a proxy. Some Spring AOP infrastructure classes are implemented as bean
post-processors and they do this proxy-wrapping logic.
For any such bean, you should see an info log message: “Bean foo is not eligible for getting
processed by all BeanPostProcessor interfaces (for example: not eligible for auto-proxying)”.
120
package scripting;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
<lang:groovy id="messenger"
script-
source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
<lang:property name="message" value="Fiona Apple Is Just So Dreamy."/>
</lang:groovy>
<!--
when the above bean (messenger) is instantiated, this custom
BeanPostProcessor implementation will output the fact to the system console
-->
<bean class="scripting.InstantiationTracingBeanPostProcessor"/>
</beans>
The following small driver script executes the preceding code and configuration:
121
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;
3.8.1.2 Example: The RequiredAnnotationBeanPostProcessor
122
the Ordered interface too; consult the Javadoc for
theBeanFactoryPostProcessor and Ordered interfaces for more details.
Note
If you want to change the actual bean instances (the objects that are created from the
configuration metadata), then you instead need to use a BeanPostProcessor (described above
in Section 3.8.1, “Customizing beans using the BeanPostProcessor Interface ” . While it is
technically possible to work with bean instances within a BeanFactoryPostProcessor (e.g.
usingBeanFactory.getBean()), doing so causes premature bean instantiation, violating the
usual containter lifecycle. This may cause negative side effects such as bypassing bean post
processing.
Note
3.8.2.1 Example: the PropertyPlaceholderConfigurer
123
specific properties such as database URLs and passwords, without the complexity
or risk of modifying the main XML definition file or files for the container.
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
The actual values come from another file in the standard Java Properties format:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
Therefore, the string ${jdbc.username} is replaced at runtime with the value 'sa'
and similarly for other placeholder values that match to keys in the property file.
The PropertyPlaceholderConfigurer checks for placeholders in most locations of a
bean definition and the placeholder prefix and suffix can be customized.
<context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
124
if it cannot find a property you are trying to use. You can customize this behavior
by setting the systemPropertiesMode property of the configurer. It has three values
that specify configurer behavior: always override, never override, and override only
if the property is not found in the properties file specified. Consult the Javadoc for
the PropertyPlaceholderConfigurer for more information.
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/foo/strategy.properties</value>
</property>
<property name="properties">
<value>custom.strategy.class=com.foo.DefaultStrategy</value>
</property>
</bean>
If the class cannot be resolved at runtime to a valid class, resolution of the bean fails when it is
about to be created, which is during the preInstantiateSingletons() phase of
an ApplicationContext for a non-lazy-init bean.
3.8.2.2 Example: the PropertyOverrideConfigurer
beanName.property=value
125
For example:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
This example file is usable against a container definition that contains a bean
called dataSource, which has driver and url properties.
Compound property names are also supported, as long as every component of the
path except the final property being overridden is already non-null (presumably
initialized by the constructors). In this example...
foo.fred.bob.sammy=123
Note
Specified override values are always literal values; they are not translated into bean references.
This convention also applies when the original value in the XML bean definition specifies a bean
reference.
<context:property-override location="classpath:override.properties"/>
126
Object getObject(): returns an instance of the object this factory creates. The
instance can possibly be shared, depending on whether this factory returns
singletons or prototypes.
boolean isSingleton(): returns true if this FactoryBean returns
singletons, false otherwise.
When you need to ask a container for an actual FactoryBean instance itself, not the
bean it produces, you preface the bean id with the ampersand symbol & (without
quotes) when calling the getBean() method of the ApplicationContext. So for a
given FactoryBean with an id of myBean, invokinggetBean("myBean") on the container
returns the product of the FactoryBean, and invoking getBean("&myBean") returns
the FactoryBean instance itself.
The introduction of annotation-based configurations raised the question of whether this approach is 'better'
than XML. The short answer is it depends. The long answer is that each approach has its pros and cons,
and usually it is up to the developer to decide which strategy suits her better. Due to the way they are
defined, annotations provide a lot of context in their declaration, leading to shorter and more concise
configuration. However, XML excels at wiring up components without touching their source code or
recompiling them. Some developers prefer having the wiring close to the source while others argue that
annotated classes are no longer POJOs and, furthermore, that the configuration becomes decentralized
and harder to control.
No matter the choice, Spring can accommodate both styles and even mix them together. It's worth
pointing out that through its JavaConfig option, Spring allows annotations to be used in a non-invasive
way, without touching the target components source code and that in terms of tooling, all configuration
styles are supported by the SpringSource Tool Suite.
127
extending the Spring IoC container. For example, Spring 2.0 introduced the
possibility of enforcing required properties with the@Required annotation. As of
Spring 2.5, it is now possible to follow that same general approach to drive
Spring's dependency injection. Essentially, the @Autowired annotation provides
the same capabilities as described in Section 3.4.5, “Autowiring collaborators”but
with more fine-grained control and wider applicability. Spring 2.5 also adds support
for JSR-250 annotations such as @Resource, @PostConstruct,
and @PreDestroy. Spring 3.0 adds support for JSR-330 (Dependency Injection for
Java) annotations contained in the javax.inject package such
as @Inject, @Qualifier, @Named, and @Provider if the JSR330 jar is present on
the classpath. Use of these annotations also requires that
certain BeanPostProcessors be registered within the Spring container.
Note
Annotation injection is performed before XML injection, thus the latter configuration will
override the former for properties wired through both approaches.
As always, you can register them as individual bean definitions, but they can also
be implicitly registered by including the following tag in an XML-based Spring
configuration (notice the inclusion of the context namespace):
<context:annotation-config/>
</beans>
Note
128
your controllers, and not your services. See Section 15.2, “The DispatcherServlet” for more
information.
3.9.1 @Required
@Required
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
This annotation simply indicates that the affected bean property must be populated
at configuration time, through an explicit property value in a bean definition or
through autowiring. The container throws an exception if the affected bean
property has not been populated; this allows for eager and explicit failure,
avoiding NullPointerExceptions or the like later on. It is still recommended that you
put assertions into the bean class itself, for example, into an init method. Doing so
enforces those required references and values even when you use the class
outside of a container.
Note
JSR 330's @Inject annotation can be used in place of Spring's @Autowired in the examples
below. @Inject does not have a required property unlike Spring's @Autowired annotation which
has a required property to indicate if the value being injected is optional. This behavior is
enabled automatically if you have the JSR 330 JAR on the classpath.
public class SimpleMovieLister {
@Autowired
129
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
You can also apply the annotation to methods with arbitrary names and/or multiple
arguments:
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
130
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
Even typed Maps can be autowired as long as the expected key type is String.
The Map values will contain all beans of the expected type, and the keys will
contain the corresponding bean names:
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
131
@Autowired(required=false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Note
Only one annotated constructor per-class can be marked as required, but multiple non-required
constructors can be annotated. In that case, each is considered among the candidates and Spring
uses the greediest constructor whose dependencies can be satisfied, that is the constructor that has
the largest number of arguments.
@Autowired
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}
Note
132
JSR 330's @Qualifier annotation can only be applied as a meta-annotation unlike Spring's
@Qualifier which takes a string property to discriminate among multiple injection candidates and
can be placed on annotations as well as types, fields, methods, constructors, and parameters.
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
@Autowired
public void prepare(@Qualifier("main") MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
The corresponding bean definitions appear as follows. The bean with qualifier
value "main" is wired with the constructor argument that is qualified with the same
value.
<context:annotation-config/>
133
<bean class="example.SimpleMovieCatalog">
<qualifier value="main"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
<!-- inject any dependencies required by this bean -->
</bean>
</beans>
For a fallback match, the bean name is considered a default qualifier value. Thus
you can define the bean with an id "main" instead of the nested qualifier element,
leading to the same matching result. However, although you can use this
convention to refer to specific beans by name,@Autowired is fundamentally about
type-driven injection with optional semantic qualifiers. This means that qualifier
values, even with the bean name fallback, always have narrowing semantics within
the set of type matches; they do not semantically express a reference to a unique
bean id. Good qualifier values are "main" or "EMEA" or "persistent", expressing
characteristics of a specific component that are independent from the bean id,
which may be auto-generated in case of an anonymous bean definition like the
one in the preceding example.
Tip
If you intend to express annotation-driven injection by name, do not primarily use @Autowired,
even if is technically capable of referring to a bean name through @Qualifier values. Instead, use
the JSR-250 @Resource annotation, which is semantically defined to identify a specific target
component by its unique name, with the declared type being irrelevant for the matching process.
As a specific consequence of this semantic difference, beans that are themselves defined as a
collection or map type cannot be injected through @Autowired, because type matching is not
properly applicable to them. Use @Resource for such beans, referring to the specific collection or
map bean by unique name.
134
@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing
through qualifier annotations at the parameter level. By contrast, @Resource is supported only for
fields and bean property setter methods with a single argument. As a consequence, stick with
qualifiers if your injection target is a constructor or a multi-argument method.
You can create your own custom qualifier annotations. Simply define an
annotation and provide the @Qualifier annotation within your definition:
Note
You can use JSR 330's @Qualifier annotation in the manner described below in place of
Spring's @Qualifier annotation. This behavior is enabled automatically if you have the JSR 330
jar on the classpath.
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
String value();
}
Then you can provide the custom qualifier on autowired fields and parameters:
@Autowired
@Genre("Action")
private MovieCatalog actionCatalog;
@Autowired
public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
this.comedyCatalog = comedyCatalog;
}
// ...
}
Next, provide the information for the candidate bean definitions. You can
add <qualifier/> tags as sub-elements of the <bean/> tag and then specify
the type and value to match your custom qualifier annotations. The type is matched
against the fully-qualified class name of the annotation. Or, as a convenience if no
risk of conflicting names exists, you can use the short class name. Both
approaches are demonstrated in the following example.
135
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog">
<qualifier type="Genre" value="Action"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier type="example.Genre" value="Comedy"/>
<!-- inject any dependencies required by this bean -->
</bean>
</beans>
In some cases, it may be sufficient to use an annotation without a value. This may
be useful when the annotation serves a more generic purpose and can be applied
across several different types of dependencies. For example, you may provide
an offline catalog that would be searched when no Internet connection is available.
First define the simple annotation:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Offline {
@Autowired
@Offline
136
private MovieCatalog offlineCatalog;
// ...
}
<bean class="example.SimpleMovieCatalog">
<qualifier type="Offline"/>
<!-- inject any dependencies required by this bean -->
</bean>
You can also define custom qualifier annotations that accept named attributes in
addition to or instead of the simple value attribute. If multiple attribute values are
then specified on a field or parameter to be autowired, a bean definition must
match all such attribute values to be considered an autowire candidate. As an
example, consider the following annotation definition:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MovieQualifier {
String genre();
Format format();
}
The fields to be autowired are annotated with the custom qualifier and include
values for both attributes: genre and format.
@Autowired
@MovieQualifier(format=Format.VHS, genre="Action")
private MovieCatalog actionVhsCatalog;
137
@Autowired
@MovieQualifier(format=Format.VHS, genre="Comedy")
private MovieCatalog comedyVhsCatalog;
@Autowired
@MovieQualifier(format=Format.DVD, genre="Action")
private MovieCatalog actionDvdCatalog;
@Autowired
@MovieQualifier(format=Format.BLURAY, genre="Comedy")
private MovieCatalog comedyBluRayCatalog;
// ...
}
Finally, the bean definitions should contain matching qualifier values. This example
also demonstrates that bean meta attributes may be used instead of
the <qualifier/> sub-elements. If available, the <qualifier/> and its attributes take
precedence, but the autowiring mechanism falls back on the values provided
within the <meta/> tags if no such qualifier is present, as in the last two bean
definitions in the following example.
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog">
<qualifier type="MovieQualifier">
<attribute key="format" value="VHS"/>
<attribute key="genre" value="Action"/>
</qualifier>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier type="MovieQualifier">
<attribute key="format" value="VHS"/>
<attribute key="genre" value="Comedy"/>
</qualifier>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<meta key="format" value="DVD"/>
138
<meta key="genre" value="Action"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<meta key="format" value="BLURAY"/>
<meta key="genre" value="Comedy"/>
<!-- inject any dependencies required by this bean -->
</bean>
</beans>
3.9.4 CustomAutowireConfigurer
<bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.CustomQualifier</value>
</set>
</property>
</bean>
3.9.5 @Resource
139
example in JSF 1.2 managed beans or JAX-WS 2.0 endpoints. Spring supports
this pattern for Spring-managed objects as well.
@Resource takesa name attribute, and by default Spring interprets that value as the
bean name to be injected. In other words, it follows by-namesemantics, as
demonstrated in this example:
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
If no name is specified explicitly, the default name is derived from the field name or
setter method. In case of a field, it takes the field name; in case of a setter method,
it takes the bean property name. So the following example is going to have the
bean with name "movieFinder" injected into its setter method:
@Resource
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
Note
140
Thus in the following example, the customerPreferenceDao field first looks for a bean
named customerPreferenceDao, then falls back to a primary type match for the
type CustomerPreferenceDao. The "context" field is injected based on the known
resolvable dependency typeApplicationContext.
@Resource
private CustomerPreferenceDao customerPreferenceDao;
@Resource
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}
3.9.6 @PostConstruct and @PreDestroy
@PostConstruct
public void populateMovieCache() {
// populates the movie cache upon initialization...
}
@PreDestroy
public void clearMovieCache() {
// clears the movie cache upon destruction...
}
}
Note
For details about the effects of combining various lifecycle mechanisms, see Section 3.6.1.4,
“Combining lifecycle mechanisms”.
141
3.10 Classpath scanning and managed components
Most examples foo bar in this chapter use XML to specify the configuration
metadata that produces each BeanDefinition within the Spring container. The
previous section (Section 3.9, “Annotation-based container configuration”)
demonstrates how to provide a lot of the configuration metadata through source-
level annotations. Even in those examples, however, the "base" bean definitions
are explicitly defined in the XML file, while the annotations only drive the
dependency injection. This section describes an option for implicitly detecting
the candidate components by scanning the classpath. Candidate components are classes that
match against a filter criteria and have a corresponding bean definition registered with the container.
This removes the need to use XML to perform bean registration, instead you can use annotations (for
example @Component), AspectJ type expressions, or your own custom filter criteria to select which
classes will have bean definitions registered with the container.
Note
Starting with Spring 3.0, many features provided by the Spring JavaConfig project are part of the
core Spring Framework. This allows you to define beans using Java rather than using the
traditional XML files. Take a look at the @Configuration, @Bean,@Import,
and @DependsOn annotations for examples of how to use these new features.
In Spring 2.0 and later, the @Repository annotation is a marker for any class that
fulfills the role or stereotype (also known as Data Access Object or DAO) of a
repository. Among the uses of this marker is the automatic translation of
exceptions as described in Section 13.2.2, “Exception translation”.
142
3.10.2 Automatically detecting classes and registering bean definitions
@Service
public class SimpleMovieLister {
@Autowired
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@Repository
public class JpaMovieFinder implements MovieFinder {
// implementation elided for clarity
}
To autodetect these classes and register the corresponding beans, you need to
include the following element in XML, where the base-package element is a
common parent package for the two classes. (Alternatively, you can specify a
comma-separated list that includes the parent package of each class.)
<context:component-scan base-package="org.example"/>
</beans>
Note
The scanning of classpath packages requires the presence of corresponding directory entries in
the classpath. When you build JARs with Ant, make sure that you do not activate the files-only
switch of the JAR task.
Furthermore,
the AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostPr
ocessor are both included implicitly when you use the component-scan element.
143
That means that the two components are autodetected and wired together - all without
any bean configuration metadata provided in XML.
Note
Table 3.5. Filter Types
<beans>
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
Note
144
the <component-scan/> element. This will in effect disable automatic detection of classes
annotated with @Component, @Repository, @Service, or@Controller.
Spring components can also contribute bean definition metadata to the container.
You do this with the same @Bean annotation used to define bean metadata
within @Configuration annotated classes. Here is a simple example:
@Component
public class FactoryMethodComponent {
@Bean @Qualifier("public")
public TestBean publicInstance() {
return new TestBean("publicInstance");
}
@Component
public class FactoryMethodComponent {
@Bean @Qualifier("public")
public TestBean publicInstance() {
return new TestBean("publicInstance");
}
@Bean @BeanAge(1)
protected TestBean protectedInstance(@Qualifier("public") TestBean spouse,
145
@Value("#{privateInstance.age}") String
country) {
TestBean tb = new TestBean("protectedInstance", 1);
tb.setSpouse(tb);
tb.setCountry(country);
return tb;
}
@Bean @Scope(BeanDefinition.SCOPE_SINGLETON)
private TestBean privateInstance() {
return new TestBean("privateInstance", i++);
}
Note
JSR 330's @Named annotation can be used as a means to both detect components and to provide
146
them with a name. This behavior is enabled automatically if you have the JSR 330 JAR on the
classpath.
@Service("myMovieLister")
public class SimpleMovieLister {
// ...
}
@Repository
public class MovieFinderImpl implements MovieFinder {
// ...
}
Note
If you do not want to rely on the default bean-naming strategy, you can provide a custom bean-
naming strategy. First, implement the BeanNameGenerator interface, and be sure to include a
default no-arg constructor. Then, provide the fully-qualified class name when configuring the
scanner:
<beans>
<context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerator" />
</beans>
As a general rule, consider specifying the name with the annotation whenever
other components may be making explicit references to it. On the other hand, the
auto-generated names are adequate whenever the container is responsible for
wiring.
147
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
// ...
}
Note
To provide a custom strategy for scope resolution rather than relying on the annotation-based
approach, implement theScopeMetadataResolver interface, and be sure to include a default no-
arg constructor. Then, provide the fully-qualified class name when configuring the scanner:
<beans>
<context:component-scan base-package="org.example"
scope-resolver="org.example.MyScopeResolver" />
</beans>
<beans>
<context:component-scan base-package="org.example"
scoped-proxy="interfaces" />
</beans>
148
@Component
@Qualifier("Action")
public class ActionMovieCatalog implements MovieCatalog {
// ...
}
@Component
@Genre("Action")
public class ActionMovieCatalog implements MovieCatalog {
// ...
}
@Component
@Offline
public class CachingMovieCatalog implements MovieCatalog {
// ...
}
Note
As with most annotation-based alternatives, keep in mind that the annotation metadata is bound
to the class definition itself, while the use of XML allows for multiple beans of the same type to
provide variations in their qualifier metadata, because that metadata is provided per-instance
rather than per-class.
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
149
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
3.11.2.1 Simple construction
In much the same way that Spring XML files are used as input when instantiating
a ClassPathXmlApplicationContext, @Configuration classes may be used as input
when instantiating an AnnotationConfigApplicationContext. This allows for
completely XML-free usage of the Spring container:
150
public static void main(String[] args) {
ApplicationContext ctx = new
AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class,
Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
Experienced Spring users will be familiar with the following commonly-used XML
declaration from Spring's context: namespace
<beans>
<context:component-scan base-package="com.acme"/>
</beans>
151
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
}
Note
<web-app>
<!-- Configure ContextLoaderListener to use
AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
152
<!-- Bootstrap the root application context as usual using ContextLoaderListener
-->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-
delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /main/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
</web-app>
3.11.3.1 Using the @Import annotation
@Configuration
public class ConfigA {
public @Bean A a() { return new A(); }
}
153
@Configuration
@Import(ConfigA.class)
public class ConfigB {
public @Bean B b() { return new B(); }
}
The example above works, but is simplistic. In most practical scenarios, beans will
have dependencies on one another across configuration classes. When using
XML, this is not an issue, per se, because there is no compiler involved, and one
can simply declare ref="someBean" and trust that Spring will work it out during
container initialization. Of course, when using @Configuration classes, the Java
compiler places constraints on the configuration model, in that references to other
beans must be valid Java syntax.
@Configuration
public class ServiceConfig {
private @Autowired AccountRepository accountRepository;
154
}
}
@Configuration
public class RepositoryConfig {
private @Autowired DataSource dataSource;
@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class SystemTestConfig {
public @Bean DataSource dataSource() { /* return new DataSource */ }
}
In cases where this ambiguity is not acceptable and you wish to have direct
navigation from within your IDE from one @Configuration class to another, consider
autowiring the configuration classes themselves:
@Configuration
public class ServiceConfig {
private @Autowired RepositoryConfig repositoryConfig;
155
// navigate 'through' the config class to the @Bean method!
return new TransferServiceImpl(repositoryConfig.accountRepository());
}
}
@Configuration
public class ServiceConfig {
private @Autowired RepositoryConfig repositoryConfig;
@Configuration
public interface RepositoryConfig {
@Bean AccountRepository accountRepository();
}
@Configuration
public class DefaultRepositoryConfig implements RepositoryConfig {
public @Bean AccountRepository accountRepository() {
return new JdbcAccountRepository(...);
}
}
@Configuration
@Import({ServiceConfig.class, DefaultRepositoryConfig.class}) // import the
concrete config!
public class SystemTestConfig {
public @Bean DataSource dataSource() { /* return DataSource */ }
}
156
3.11.3.2 Combining Java and XML configuration
@Configuration
public class AppConfig {
private @Autowired DataSource dataSource;
157
<bean class="com.acme.AppConfig"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
jdbc.properties
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa
jdbc.password=
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("classpath:/com/acme/system-test-config.xml");
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}
Note
system-test-config.xml
<beans>
<!-- picks up and registers AppConfig as a bean definition -->
<context:component-scan base-package="com.acme"/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
158
In applications where @Configuration classes are the primary mechanism for
configuring the container, it will still likely be necessary to use at least some XML.
In these scenarios, simply use @ImportResource and define only as much XML as is
needed. Doing so achieves a "Java-centric" approach to configuring the container
and keeps XML to a bare minimum.
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
private @Value("${jdbc.url}") String url;
private @Value("${jdbc.username}") String username;
private @Value("${jdbc.password}") String password;
3.11.4 Using the @Bean annotation
3.11.4.1 Declaring a bean
159
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
3.11.4.2 Injecting dependencies
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
}
@Bean
public Bar bar() {
return new Bar();
}
160
3.11.4.3 Receiving lifecycle callbacks
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}
161
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
Foo foo = new Foo();
foo.init();
return foo;
}
// ...
}
Tip
When you work directly in Java, you can do anything you like with your objects and do not
always need to rely on the container lifecycle!
You can specify that your beans defined with the @Bean annotation should have a
specific scope. You can use any of the standard scopes specified in the Bean
Scopes section.
The default scope is singleton, but you can override this with the @Scope annotation:
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}
If you port the scoped proxy example from the XML reference documentation (see
preceding link) to our @Bean using Java, it would look like the following:
162
// an HTTP Session-scoped bean exposed as a proxy
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserPreferences userPreferences() {
return new UserPreferences();
}
@Bean
public Service userService() {
UserService service = new SimpleUserService();
// a reference to the proxied userPreferences bean
service.setUserPreferences(userPreferences());
return service;
}
@Bean
@Scope("prototype")
public AsyncCommand asyncCommand() {
AsyncCommand command = new AsyncCommand();
// inject dependencies here as required
return command;
}
163
@Bean
public CommandManager commandManager() {
// return new anonymous implementation of CommandManager with command()
overridden
// to return a new prototype Command object
return new CommandManager() {
protected Command createCommand() {
return asyncCommand();
}
}
}
@Configuration
public class AppConfig {
@Bean(name = "myFoo")
public Foo foo() {
return new Foo();
}
3.11.4.6 Bean aliasing
@Configuration
public class AppConfig {
164
@Configuration
public class AppConfig {
@Bean
public ClientService clientService1() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientService clientService2() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientDao clientDao() {
return new ClientDaoImpl();
}
}
Note
The behavior could be different according to the scope of your bean. We are talking about
singletons here.
Note
Beware that, in order for JavaConfig to work, you must include the CGLIB jar in your list of
dependencies.
Note
There are a few restrictions due to the fact that CGLIB dynamically adds features at startup-time:
165
3.12 Registering a LoadTimeWeaver
<beans>
<context:load-time-weaver/>
</beans>
166
Event publication to beans implementing the ApplicationListener interface,
through the use of the ApplicationEventPublisher interface.
3.13.1 Internationalization using MessageSource
Spring provides
two MessageSource implementations, ResourceBundleMessageSource and StaticMessageSo
urce. Both implementHierarchicalMessageSource in order to do nested messaging.
167
The StaticMessageSource is rarely used but provides programmatic ways to add
messages to the source. The ResourceBundleMessageSource is shown in the following
example:
<beans>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>
In the example it is assumed you have three resource bundles defined in your
classpath called format, exceptions and windows. Any request to resolve a message
will be handled in the JDK standard way of resolving messages through
ResourceBundles. For the purposes of the example, assume the contents of two
of the above resource bundle files are...
# in format.properties
message=Alligators rock!
# in exceptions.properties
argument.required=The '{0}' argument is required.
Alligators rock!
168
So to summarize, the MessageSource is defined in a file called beans.xml, which exists
at the root of your classpath. The messageSource bean definition refers to a number
of resource bundles through its basenames property. The three files that are passed
in the list to the basenames property exist as files at the root of your classpath and
are called format.properties, exceptions.properties,
and windows.properties respectively.
The next example shows arguments passed to the message lookup; these
arguments will be converted into Strings and inserted into placeholders in the
lookup message.
<beans>
<!-- lets inject the above MessageSource into this POJO -->
<bean id="example" class="com.foo.Example">
<property name="messages" ref="messageSource"/>
</bean>
</beans>
public class Example {
169
example messageSource defined previously, if you want to resolve messages against
the British (en-GB) locale, you would create files
called format_en_GB.properties, exceptions_en_GB.properties,
andwindows_en_GB.properties respectively.
# in exceptions_en_GB.properties
argument.required=Ebagum lad, the '{0}' argument is required, I say, required.
public static void main(final String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("argument.required",
new Object [] {"userDao"}, "Required", Locale.UK);
System.out.println(message);
}
The resulting output from the running of the above program will be...
Note
170
time an ApplicationEvent gets published to theApplicationContext, that bean is
notified. Essentially, this is the standard Observer design pattern. Spring provides
the following standard events:
Table 3.6. Built-in Events
Explanation
Published when the ApplicationContext is initialized or refreshed, for example, using the refresh() method on
the ConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor
hedEvent
beans are detected and activated, singletons are pre-instantiated, and the ApplicationContextobject is ready for use
long as the context has not been closed, a refresh can be triggered multiple times, provided that the
chosen ApplicationContext actually supports such "hot" refreshes. For
example,XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.
Published when the ApplicationContext is started, using the start() method on
dEvent
theConfigurableApplicationContext interface. "Started" here means that all Lifecycle beans receive an explicit
start signal. Typically this signal is used to restart beans after an explicit stop, but it may also be used to start compon
that have not been configured for autostart , for example, components that have not already started on initialization.
Published when the ApplicationContext is stopped, using the stop() method on
dEvent theConfigurableApplicationContext interface. "Stopped" here means that all Lifecycle beans receive an explici
stop signal. A stopped context may be restarted through a start() call.
Published when the ApplicationContext is closed, using the close() method on
Event theConfigurableApplicationContext interface. "Closed" here means that all singleton beans are destroyed. A clos
context reaches its end of life; it cannot be refreshed or restarted.
dEvent
A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the reque
complete. This event is only applicable to web applications using Spring's DispatcherServlet.
You can also create and publish your own custom events. This example
demonstrates a simple class that extends Spring's ApplicationEvent base class:
171
implements ApplicationEventPublisherAware and registering it as a Spring bean. The
following example demonstrates such a class:
172
Notice that ApplicationListener is generically parameterized with the type of your
custom event, BlackListEvent. This means that theonApplicationEvent() method can
remain type-safe, avoiding any need for downcasting. You may register as many
event listeners as you wish, but note that by default event listeners receive events
synchronously. This means the publishEvent() method blocks until all listeners
have finished processing the event. One advantage of this synchronous and
single-threaded approach is that when a listener receives an event, it operates
inside the transaction context of the publisher if a transaction context is available.
If another strategy for event publication becomes necessary, refer to the JavaDoc
for Spring's ApplicationEventMulticaster interface.
The following example demonstrates the bean definitions used to register and
configure each of the classes above:
Note
Spring's eventing mechanism is designed for simple communication between Spring beans within
the same application context. However, for more sophisticated enterprise integration needs, the
separately-maintained Spring Integration project provides complete support for building
lightweight, pattern-oriented, event-driven architectures that build upon the well-known Spring
programming model.
173
3.13.3 Convenient access to low-level resources
You can configure a bean deployed into the application context to implement the
special callback interface, ResourceLoaderAware, to be automatically called back at
initialization time with the application context itself passed in as the ResourceLoader.
You can also expose properties of type Resource, to be used to access static
resources; they will be injected into it like any other properties. You can specify
those Resourceproperties as simple String paths, and rely on a special
JavaBean PropertyEditor that is automatically registered by the context, to convert
those text strings to actual Resource objects when the bean is deployed.
174
context for the web application is created and is available to service the first
request (and also when the Servlet context is about to be shut down). As such a
Servlet context listener is an ideal place to initialize the Spring ApplicationContext.
All things being equal, you should probably prefer ContextLoaderListener; for more
information on compatibility, have a look at the Javadoc for
theContextLoaderServlet.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
175
access the J2EE servers facilities. RAR deployment is a more natural alternative
to scenario of deploying a headless WAR file, in effect, a WAR file without any
HTTP entry points that is used only for bootstrapping a Spring ApplicationContext
in a J2EE environment.
RAR deployment is ideal for application contexts that do not need HTTP entry
points but rather consist only of message endpoints and scheduled jobs. Beans in
such a context can use application server resources such as the JTA transaction
manager and JNDI-bound JDBC DataSources and JMS ConnectionFactory
instances, and may also register with the platform's JMX server - all through
Spring's standard transaction management and JNDI and JMX support facilities.
Application components can also interact with the application server's JCA
WorkManager through Spring'sTaskExecutor abstraction.
Note
Such RAR deployment units are usually self-contained; they do not expose components to the
outside world, not even to other modules of the same application. Interaction with a RAR-based
ApplicationContext usually occurs through JMS destinations that it shares with other modules. A
RAR-based ApplicationContext may also, for example, schedule some jobs, reacting to new files
in the file system (or the like). If it needs to allow synchronous access from the outside, it could
for example export RMI endpoints, which of course may be used by other application modules on
the same machine.
3.14 The BeanFactory
176
that can not use more modern equivalents such as @PostConstruct or@PreDestroy in
order to remain compatible with JDK 1.4 or to avoid a dependency on JSR-250.
3.14.1 BeanFactory or ApplicationContext?
Use an ApplicationContext unless you have a good reason for not doing so.
Table 3.7. Feature Matrix
Automatic BeanPostProcessor registration No Yes
Automatic BeanFactoryPostProcessor registration No Yes
ApplicationEvent publication No Yes
177
ConfigurableBeanFactory factory = new XmlBeanFactory(...);
In both cases, the explicit registration step is inconvenient, which is one reason
why the various ApplicationContext implementations are preferred above
plain BeanFactory implementations in the vast majority of Spring-backed
applications, especially when
using BeanFactoryPostProcessorsand BeanPostProcessors. These mechanisms
implement important functionality such as property placeholder replacement and
AOP.
178
retrieved from a Spring IoC container. While the Spring IoC container itself ideally
does not have to be a singleton, it may be unrealistic in terms of memory usage or
initialization times (when using beans in the Spring IoC container such as a
Hibernate SessionFactory) for each bean to use its own, non-singleton Spring IoC
container.
Looking up the application context in a service locator style is sometimes the only
option for accessing shared Spring-managed components, such as in an EJB 2.1
environment, or when you want to share a single ApplicationContext as a parent to
WebApplicationContexts across WAR files. In this case you should look into using
the utility class ContextSingletonBeanFactoryLocator locator that is described in
this SpringSource team blog entry.
[1]
See Background
[2]
See Section 3.4.1, “Dependency injection”
4. Resources
4.1 Introduction
4.2 The Resource interface
boolean exists();
179
boolean isOpen();
String getFilename();
String getDescription();
}
public interface InputStreamSource {
getDescription():
returns a description for this resource, to be used for error
output when working with the resource. This is often the fully qualified file
name or the actual URL of the resource.
180
simple form is used to create a Resource appropriate to that context
implementation, or via special prefixes on the String path, allow the caller to
specify that a specific Resource implementation must be created and used.
While the Resource interface is used a lot with Spring and by Spring, it's actually
very useful to use as a general utility class by itself in your own code, for access to
resources, even when your code doesn't know or care about any other parts of
Spring. While this couples your code to Spring, it really only couples it to this small
set of utility classes, which are serving as a more capable replacement for URL, and
can be considered equivalent to any other library you would use for this purpose.
4.3 Built-in Resource implementations
4.3.1 UrlResource
181
4.3.2 ClassPathResource
This class represents a resource which should be obtained from the classpath.
This uses either the thread context class loader, a given class loader, or a given
class for loading resources.
4.3.3 FileSystemResource
4.3.4 ServletContextResource
This always supports stream access and URL access, but only
allows java.io.File access when the web application archive is expanded and the
resource is physically on the filesystem. Whether or not it's expanded and on the
filesystem like this, or accessed directly from the JAR or somewhere else like a DB
(it's conceivable) is actually dependent on the Servlet container.
4.3.5 InputStreamResource
182
In contrast to other Resource implementations, this is a descriptor for
an already opened resource - therefore returning true from isOpen(). Do not use it
if you need to keep the resource descriptor somewhere, or if you need to read a
stream multiple times.
4.3.6 ByteArrayResource
It's useful for loading content from any given byte array, without having to resort to
a single-use InputStreamResource.
4.4 The ResourceLoader
When you call getResource() on a specific application context, and the location path
specified doesn't have a specific prefix, you will get back a Resource type that is
appropriate to that particular application context. For example, assume the
following snippet of code was executed against
aClassPathXmlApplicationContext instance:
183
On the other hand, you may also force ClassPathResource to be used, regardless of
the application context type, by specifying the specialclasspath: prefix:
Resource template =
ctx.getResource("classpath:some/resource/path/myTemplate.txt");
Table 4.1. Resource strings
classpath classpath:com/myapp/config.xml
Loaded from the classpath.
:
[1]
But see also Section 4.7.3, “FileSystemResource caveats”.
4.5 The ResourceLoaderAware interface
184
When a class implements ResourceLoaderAware and is deployed into an application
context (as a Spring-managed bean), it is recognized asResourceLoaderAware by the
application context. The application context will then invoke
the setResourceLoader(ResourceLoader), supplying itself as the argument (remember,
all application contexts in Spring implement the ResourceLoader interface).
If the bean itself is going to determine and supply the resource path through some
sort of dynamic process, it probably makes sense for the bean to use
the ResourceLoader interface to load resources. Consider as an example the loading
of a template of some sort, where the specific resource that is needed depends on
the role of the user. If the resources are static, it makes sense to eliminate the use
of the ResourceLoader interface completely, and just have the bean expose
the Resource properties it needs, and expect that they will be injected into it.
What makes it trivial to then inject these properties, is that all application contexts
register and use a special JavaBeans PropertyEditor which can
convert String paths to Resource objects. So if myBean has a template property of
type Resource, it can be configured with a simple string for that resource, as follows:
185
<bean id="myBean" class="...">
<property name="template" value="some/resource/path/myTemplate.txt"/>
</bean>
Note that the resource path has no prefix, so because the application context itself
is going to be used as the ResourceLoader, the resource itself will be loaded via
a ClassPathResource, FileSystemResource, or ServletContextResource (as appropriate)
depending on the exact type of the context.
When such a location path doesn't have a prefix, the specific Resource type built
from that path and used to load the bean definitions, depends on and is
appropriate to the specific application context. For example, if you create
a ClassPathXmlApplicationContext as follows:
ApplicationContext ctx =
new FileSystemXmlApplicationContext("conf/appContext.xml");
The bean definition will be loaded from a filesystem location, in this case relative to
the current working directory.
186
Note that the use of the special classpath prefix or a standard URL prefix on the
location path will override the default type of Resource created to load the definition.
So this FileSystemXmlApplicationContext...
ApplicationContext ctx =
new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");
... will actually load its bean definitions from the classpath. However, it is still
a FileSystemXmlApplicationContext. If it is subsequently used as a ResourceLoader,
any unprefixed paths will still be treated as filesystem paths.
4.7.1.1 Constructing ClassPathXmlApplicationContext instances - shortcuts
An example will hopefully make this clear. Consider a directory layout that looks
like this:
com/
foo/
services.xml
daos.xml
MessengerService.class
The resource paths in application context constructor values may be a simple path
(as shown above) which has a one-to-one mapping to a target Resource, or
alternately may contain the special "classpath*:" prefix and/or internal Ant-style
187
regular expressions (matched using Spring'sPathMatcher utility). Both of the latter
are effectively wildcards
One use for this mechanism is when doing component-style application assembly.
All components can 'publish' context definition fragments to a well-known location
path, and when the final application context is created using the same path
prefixed via classpath*:, all component fragments will be picked up automatically.
Note that this wildcarding is specific to use of resource paths in application context
constructors (or when using the PathMatcher utility class hierarchy directly), and is
resolved at construction time. It has nothing to do with the Resource type itself. It's
not possible to use the classpath*:prefix to construct an actual Resource, as a
resource points to just one resource at a time.
4.7.2.1 Ant-style Patterns
/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml
... the resolver follows a more complex but defined procedure to try to resolve the
wildcard. It produces a Resource for the path up to the last non-wildcard segment
and obtains a URL from it. If this URL is not a "jar:" URL or container-specific
variant (e.g. "zip:" in WebLogic, "wsjar" in WebSphere, etc.), then a java.io.File is
obtained from it and used to resolve the wildcard by traversing the filesystem. In
the case of a jar URL, the resolver either gets a java.net.JarURLConnection from it or
manually parses the jar URL and then traverses the contents of the jar file to
resolve the wildcards.
Implications on portability
If the specified path is already a file URL (either explicitly, or implicitly because the
base ResourceLoader is a filesystem one, then wildcarding is guaranteed to work in
a completely portable fashion.
If the specified path is a classpath location, then the resolver must obtain the last
non-wildcard path segment URL via aClassloader.getResource() call. Since this is
just a node of the path (not the file at the end) it is actually undefined (in
the ClassLoader Javadocs) exactly what sort of a URL is returned in this case. In
188
practice, it is always a java.io.File representing the directory, where the classpath
resource resolves to a filesystem location, or a jar URL of some sort, where the
classpath resource resolves to a jar location. Still, there is a portability concern on
this operation.
If a jar URL is obtained for the last non-wildcard segment, the resolver must be
able to get a java.net.JarURLConnection from it, or manually parse the jar URL, to be
able to walk the contents of the jar, and resolve the wildcard. This will work in most
environments, but will fail in others, and it is strongly recommended that the
wildcard resolution of resources coming from jars be thoroughly tested in your
specific environment before you rely on it.
4.7.2.2 The classpath*: prefix
ApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");
This special prefix specifies that all classpath resources that match the given name
must be obtained (internally, this essentially happens via
aClassLoader.getResources(...) call), and then merged to form the final application context
definition.
Classpath*: portability
The "classpath*:" prefix can also be combined with a PathMatcher pattern in the rest
of the location path, for example "classpath*:META-INF/*-beans.xml". In this case, the
resolution strategy is fairly simple: a ClassLoader.getResources() call is used on
the last non-wildcard path segment to get all the matching resources in the class
loader hierarchy, and then off each resource the same PathMatcher resoltion
strategy described above is used for the wildcard subpath.
189
4.7.2.3 Other notes relating to wildcards
Please note that "classpath*:" when combined with Ant-style patterns will only
work reliably with at least one root directory before the pattern starts, unless the
actual target files reside in the file system. This means that a pattern like
"classpath*:*.xml" will not retrieve files from the root of jar files but rather only from
the root of expanded directories. This originates from a limitation in the
JDK's ClassLoader.getResources() method which only returns file system locations
for a passed-in empty string (indicating potential roots to search).
Ant-style patterns with "classpath:" resources are not guaranteed to find matching
resources if the root package to search is available in multiple class path locations.
This is because a resource such as
com/mycompany/package1/service-context.xml
classpath:com/mycompany/**/service-context.xml
is used to try to resolve it, the resolver will work off the (first) URL returned
by getResource("com/mycompany");. If this base package node exists in multiple
classloader locations, the actual end resource may not be underneath. Therefore,
preferably, use "classpath*:" with the same Ant-style pattern in such a case, which
will search all class path locations that contain the root package.
4.7.3 FileSystemResource caveats
190
ApplicationContext ctx =
new FileSystemXmlApplicationContext("conf/context.xml");
ApplicationContext ctx =
new FileSystemXmlApplicationContext("/conf/context.xml");
As are the following: (Even though it would make sense for them to be different, as
one case is relative and the other absolute.)
In practice, if true absolute filesystem paths are needed, it is better to forgo the use
of absolute paths with FileSystemResource /FileSystemXmlApplicationContext, and just
force the use of a UrlResource, by using the file: URL prefix.
// actual context type doesn't matter, the Resource will always be UrlResource
ctx.getResource("file:/some/resource/path/myTemplate.txt");
// force this FileSystemXmlApplicationContext to load its definition via a
UrlResource
ApplicationContext ctx =
new FileSystemXmlApplicationContext("file:/conf/context.xml");
5.1 Introduction
There are pros and cons for considering validation as business logic, and Spring
offers a design for validation (and data binding) that does not exclude either one of
them. Specifically validation should not be tied to the web tier, should be easy to
localize and it should be possible to plug in any validator available. Considering
the above, Spring has come up with a Validator interface that is both basic ands
eminently usable in every layer of an application.
Data binding is useful for allowing user input to be dynamically bound to the
domain model of an application (or whatever objects you use to process user
input). Spring provides the so-called DataBinder to do exactly that.
The Validator and the DataBinder make up the validationpackage, which is
primarily used in but not limited to the MVC framework.
191
the BeanWrapper directly. Because this is reference documentation however, we felt
that some explanation might be in order. We will explain theBeanWrapper in this
chapter since, if you were going to use it at all, you would most likely do so when
trying to bind data to objects.
/**
192
* This Validator validates just Person instances
*/
public boolean supports(Class clazz) {
return Person.class.equals(clazz);
}
193
}
this.addressValidator = addressValidator;
}
/**
* This Validator validates Customer instances, and any subclasses of Customer
too
*/
public boolean supports(Class clazz) {
return Customer.class.isAssignableFrom(clazz);
}
194
usingrejectValue("age", "too.darn.old"), apart from the too.darn.old code, Spring
will also register too.darn.old.age and too.darn.old.age.int(so the first will include
the field name and the second will include the type of the field); this is done as a
convenience to aid developers in targeting error messages and suchlike.
One quite important class in the beans package is the BeanWrapper interface and its
corresponding implementation (BeanWrapperImpl). As quoted from the Javadoc,
the BeanWrapper offers functionality to set and get property values (individually or in
bulk), get property descriptors, and to query properties to determine if they are
readable or writable. Also, the BeanWrapper offers support for nested properties,
enabling the setting of properties on sub-properties to an unlimited depth. Then,
the BeanWrapper supports the ability to add standard
JavaBeansPropertyChangeListeners and VetoableChangeListeners, without the need
for supporting code in the target class. Last but not least, the BeanWrapper provides
support for the setting of indexed properties. The BeanWrapper usually isn't used by
application code directly, but by theDataBinder and the BeanFactory.
Table 5.1. Examples of properties
195
on Explanation
Indicates the property name corresponding to the methods getName() or isName() and setName(..)
Indicates the nested property name of the property account corresponding e.g. to the
methodsgetAccount().setName() or getAccount().getName()
Indicates the third element of the indexed property account. Indexed properties can be of type array, list or
other naturally ordered collection
NYNAME] Indicates the value of the map entry indexed by the key COMPANYNAME of the Map property account
Below you'll find some examples of working with the BeanWrapper to get and set
properties.
(This next section is not vitally important to you if you're not planning to work with
the BeanWrapper directly. If you're just using the DataBinderand the BeanFactory and
their out-of-the-box implementation, you should skip ahead to the section
about PropertyEditors.)
196
}
public void setSalary(float salary) {
this.salary = salary;
}
}
The following code snippets show some examples of how to retrieve and
manipulate some of the properties of instantiated Companies andEmployees:
5.4.2 Built-in PropertyEditor implementations
197
parsing HTTP request parameters in Spring's MVC framework is done using
all kinds of PropertyEditors that you can manually bind in all subclasses of
the CommandController.
Table 5.2. Built-in PropertyEditors
s Explanation
ertyEditor
Editor for byte arrays. Strings will simply be converted to their corresponding byte representations. Registered by
default by BeanWrapperImpl.
Parses Strings representing classes to actual classes and the other way around. When a class is not found,
an IllegalArgumentException is thrown. Registered by default by BeanWrapperImpl.
Editor
Customizable property editor for Boolean properties. Registered by default by BeanWrapperImpl, but, can be
overridden by registering custom instance of it as custom editor.
ionEditor Property editor for Collections, converting any source Collection to a given target Collection type.
tor
Customizable property editor for java.util.Date, supporting a custom DateFormat. NOT registered by default. Must
user registered as needed with appropriate format.
ditor
Customizable property editor for any Number subclass like Integer, Long, Float, Double. Registered by default
by BeanWrapperImpl, but can be overridden by registering custom instance of it as a custom editor.
Capable of resolving Strings to java.io.File objects. Registered by default by BeanWrapperImpl.
One-way property editor, capable of taking a text string and producing (via an
itor
intermediate ResourceEditorand Resource) an InputStream, so InputStream properties may be directly set as
Strings. Note that the default usage will not close the InputStream for you! Registered by default
by BeanWrapperImpl.
Capable of resolving Strings to Locale objects and vice versa (the String format is [language]_[country]_[variant],
which is the same thing the toString() method of Locale provides). Registered by default by BeanWrapperImpl.
Capable of resolving Strings to JDK 1.5 Pattern objects and vice versa.
tor
Capable of converting Strings (formatted using the format as defined in the Javadoc for the java.lang.Properties cla
to Properties objects. Registered by default by BeanWrapperImpl.
Editor
Property editor that trims Strings. Optionally allows transforming an empty string into a null value. NOT registere
by default; must be user registered as needed.
Capable of resolving a String representation of a URL to an actual URL object. Registered by default
byBeanWrapperImpl.
198
Spring uses the java.beans.PropertyEditorManager to set the search path for
property editors that might be needed. The search path also
includes sun.bean.editors, which includes PropertyEditor implementations for types
such as Font, Color, and most of the primitive types. Note also that the standard
JavaBeans infrastructure will automatically discover PropertyEditor classes
(without you having to register them explicitly) if they are in the same package as
the class they handle, and have the same name as that class,
with 'Editor' appended; for example, one could have the following class and
package structure, which would be sufficient for the FooEditor class to be
recognized and used as the PropertyEditorfor Foo-typed properties.
com
chank
pop
Foo
FooEditor // the PropertyEditor for the Foo class
Note that you can also use the standard BeanInfo JavaBeans mechanism here as
well (described in not-amazing-detail here). Find below an example of using
the BeanInfo mechanism for explicitly registering one or
more PropertyEditor instances with the properties of an associated class.
com
chank
pop
Foo
FooBeanInfo // the BeanInfo for the Foo class
Here is the Java source code for the referenced FooBeanInfo class. This would
associate a CustomNumberEditor with the age property of the Fooclass.
199
catch (IntrospectionException ex) {
throw new Error(ex.toString());
}
}
}
When setting bean properties as a string value, a Spring IoC container ultimately
uses standard JavaBeans PropertyEditors to convert these Strings to the complex
type of the property. Spring pre-registers a number of custom PropertyEditors (for
example, to convert a classname expressed as a string into a real Class object).
Additionally, Java's standard JavaBeans PropertyEditor lookup mechanism allows
aPropertyEditor for a class simply to be named appropriately and placed in the
same package as the class it provides support for, to be found automatically.
Note that all bean factories and application contexts automatically use a number of
built-in property editors, through their use of something called aBeanWrapper to
handle property conversions. The standard property editors that
the BeanWrapper registers are listed in the previous section.
Additionally, ApplicationContexts also override or add an additional number of
editors to handle resource lookups in a manner appropriate to the specific
application context type.
200
Consider a user class ExoticType, and another class DependsOnExoticType which
needs ExoticType set as a property:
package example;
When things are properly set up, we want to be able to assign the type property as
a string, which a PropertyEditor will behind the scenes convert into an
actual ExoticType instance:
201
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="example.ExoticType" value="example.ExoticTypeEditor"/>
</map>
</property>
</bean>
Using PropertyEditorRegistrars
Another mechanism for registering property editors with the Spring container is to
create and use a PropertyEditorRegistrar. This interface is particularly useful when
you need to use the same set of property editors in several different situations:
write a corresponding registrar and reuse that in each
case. PropertyEditorRegistrars work in conjunction with an interface
called PropertyEditorRegistry, an interface that is implemented by the
Spring BeanWrapper (and DataBinder). PropertyEditorRegistrars are particularly
convenient when used in conjunction with
the CustomEditorConfigurer (introduced here), which exposes a property
called setPropertyEditorRegistrars(..): PropertyEditorRegistrarsadded to
a CustomEditorConfigurer in this fashion can easily be shared with DataBinder and
Spring MVC Controllers. Furthermore, it avoids the need for synchronization on
custom editors: a PropertyEditorRegistrar is expected to create
fresh PropertyEditor instances for each bean creation attempt.
package com.foo.editors.spring;
202
of the registerCustomEditors(..) method it creates new instances of each property
editor.
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="customPropertyEditorRegistrar"/>
</list>
</property>
</bean>
<bean id="customPropertyEditorRegistrar"
class="com.foo.editors.spring.CustomPropertyEditorRegistrar"/>
Finally, and in a bit of a departure from the focus of this chapter, for those of you
using Spring's MVC web framework, usingPropertyEditorRegistrars in conjunction
with data-binding Controllers (such as SimpleFormController) can be very
convenient. Find below an example of using a PropertyEditorRegistrar in the
implementation of an initBinder(..) method:
203
5.5 Spring 3 Type Conversion
5.5.1 Converter SPI
The SPI to implement type conversion logic is simple and strongly typed:
package org.springframework.core.convert.converter;
T convert(S source);
package org.springframework.core.convert.support;
204
5.5.2 ConverterFactory
When you need to centralize the conversion logic for an entire class hierarchy, for
example, when converting from String to java.lang.Enum objects,
implement ConverterFactory:
package org.springframework.core.convert.converter;
Parameterize S to be the type you are converting from and R to be the base type
defining the range of classes you can convert to. Then implement
getConverter(Class<T>), where T is a subclass of R.
package org.springframework.core.convert.support;
205
5.5.3 GenericConverter
package org.springframework.core.convert.converter;
A good example of a GenericConverter is a converter that converts between a Java Array and a
Collection. Such an ArrayToCollectionConverter introspects the field that declares the target
Collection type to resolve the Collection's element type. This allows each element in the source array
to be converted to the Collection element type before the Collection is set on the target field.
Note
Because GenericConverter is a more complex SPI interface, only use it when you need it. Favor
Converter or ConverterFactory for basic type conversion needs.
5.5.3.1 ConditionalGenericConverter
Sometimes you only want a Converter to execute if a specific condition holds true.
For example, you might only want to execute a Converter if a specific annotation is
present on the target field. Or you might only want to execute a Converter if a
specific method, such as static valueOf method, is defined on the target class.
206
ConditionalGenericConverter is an subinterface of GenericConverter that allows
you to define such custom matching criteria:
5.5.4 ConversionService API
The ConversionService defines a unified API for executing type conversion logic at
runtime. Converters are often executed behind this facade interface:
package org.springframework.core.convert;
207
5.5.5 Configuring a ConversionService
Note
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean"/>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="example.MyCustomConverter"/>
</list>
</property>
</bean>
208
5.5.6 Using a ConversionService programatically
@Service
public class MyService {
@Autowired
public MyService(ConversionService conversionService) {
this.conversionService = conversionService;
}
In general, use the Converter SPI when you need to implement general-purpose
type conversion logic; for example, for converting between a java.util.Date and and
java.lang.Long. Use the Formatter SPI when you're working in a client
environment, such as a web application, and need to parse and print localized field
values. The ConversionService provides a unified type conversion API for both
SPIs.
209
5.6.1 Formatter SPI
The Formatter SPI to implement field formatting logic is simple and strongly typed:
package org.springframework.format;
Where Formatter extends from the Printer and Parser building-block interfaces:
To create your own Formatter, simply implement the Formatter interface above.
Parameterize T to be the type of object you wish to format, for
example, java.util.Date. Implement the print() operation to print an instance of T
for display in the client locale. Implement the parse()operation to parse an instance
of T from the formatted representation returned from the client locale. Your
Formatter should throw a ParseException or IllegalArgumentException if a parse
attempt fails. Take care to ensure your Formatter implementation is thread-safe.
Consider DateFormatter as an example Formatter implementation:
package org.springframework.format.datetime;
210
public DateFormatter(String pattern) {
this.pattern = pattern;
}
5.6.2 Annotation-driven Formatting
As you will see, field formatting can be configured by field type or annotation. To
bind an Annotation to a formatter, implement AnnotationFormatterFactory:
package org.springframework.format;
Set<Class<?>> getFieldTypes();
211
HavegetPrinter() return a Printer to print the value of an annotated field.
Have getParser() return a Parser to parse a clientValue for an annotated field.
@NumberFormat(style=Style.CURRENCY)
private BigDecimal decimal;
212
5.6.2.1 Format Annotation API
@DateTimeFormat(iso=ISO.DATE)
private Date date;
5.6.3 FormatterRegistry SPI
package org.springframework.format;
213
configured programatically, or declaratively as a Spring bean
using FormattingConversionServiceFactoryBean. Because this implemementation also
implements ConversionService, it can be directly configured for use with Spring's
DataBinder and the Spring Expression Language (SpEL).
<mvc:annotation-driven/>
</beans>
With this one-line of configuation, default formatters for Numbers and Date types
will be installed, including support for the @NumberFormat and
@DateTimeFormat annotations. Full support for the Joda Time formatting library is
also installed if Joda Time is present on the classpath.
214
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/
>
</beans>
5.7 Spring 3 Validation
Spring 3 introduces several enhancements to its validation support. First, the JSR-
303 Bean Validation API is now fully supported. Second, when used
programatically, Spring's DataBinder can now validate objects as well as bind to
them. Third, Spring MVC now has support for declaratively validating @Controller
inputs.
JSR-303 standardizes validation constraint declaration and metadata for the Java
platform. Using this API, you annotate domain model properties with declarative
validation constraints and the runtime enforces them. There are a number of built-
in constraints you can take advantage of. You may also define your own custom
constraints.
215
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Spring provides full support for the JSR-303 Bean Validation API. This includes
convenient support for bootstrapping a JSR-303 implementation as a Spring bean.
This allows for a javax.validation.ValidatorFactory or javax.validation.Validator to
be injected wherever validation is needed in your application.
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
The basic configuration above will trigger JSR-303 to initialize using its default
bootstrap mechanism. A JSR-303 provider, such as Hibernate Validator, is
expected to be present in the classpath and will be detected automatically.
5.7.2.1 Injecting a Validator
LocalValidatorFactoryBean implements
both javax.validation.ValidatorFactory and javax.validation.Validator, as well as
Spring'sorg.springframework.validation.Validator. You may inject a reference to
either of these interfaces into beans that need to invoke validation logic.
216
Inject a reference to javax.validation.Validator if you prefer to work with the JSR-
303 API directly:
import javax.validation.Validator;
@Service
public class MyService {
@Autowired
private Validator validator;
import org.springframework.validation.Validator;
@Service
public class MyService {
@Autowired
private Validator validator;
By default, the LocalValidatorFactoryBean configures
a SpringConstraintValidatorFactory that uses Spring to create ConstraintValidator
instances. This allows your custom ConstraintValidators to benefit from
dependency injection like any other Spring bean.
217
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=MyConstraintValidator.class)
public @interface MyConstraint {
}
import javax.validation.ConstraintValidator;
@Autowired;
private Foo aDependency;
...
}
5.7.3 Configuring a DataBinder
When working with the DataBinder programatically, this can be used to invoke
validation logic after binding to a target object:
218
5.7.4 Spring MVC 3 Validation
Beginning with Spring 3, Spring MVC has the ability to automatically validate
@Controller inputs. In previous versions it was up to the developer to manually
invoke validation logic.
@Controller
public class MyController {
@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo) { /* ... */ }
Spring MVC will validate a @Valid object after binding so-long as an appropriate Validator has been
configured.
Note
The @Valid annotation is part of the standard JSR-303 Bean Validation API, and is not a Spring-
specific construct.
@Controller
public class MyController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo) { ... }
219
Second, you may call setValidator(Validator) on the global WebBindingInitializer.
This allows you to configure a Validator instance across all @Controllers. This can
be achieved easily by using the Spring MVC namespace:
<mvc:annotation-driven validator="globalValidator"/>
</beans>
</beans>
220
enforce any constraints declared against the input. Any ConstraintViolations will
automatically be exposed as errors in the BindingResult renderable by standard
Spring MVC form tags.
6.1 Introduction
While there are several other Java expression languages available, OGNL, MVEL,
and JBoss EL, to name a few, the Spring Expression Language was created to
provide the Spring community with a single well supported expression language
that can be used across all the products in the Spring portfolio. Its language
features are driven by the requirements of the projects in the Spring portfolio,
including tooling requirements for code completion support within the eclipse
based SpringSource Tool Suite. That said, SpEL is based on a technology
agnostic API allowing other expression language implementations to be integrated
should the need arise.
While SpEL serves as the foundation for expression evaluation within the Spring
portfolio, it is not directly tied to Spring and can be used independently. In order to
be self contained, many of the examples in this chapter use SpEL as if it were an
independent expression language. This requires creating a few bootstrapping
infrastructure classes such as the parser. Most Spring users will not need to deal
with this infrastructure and will instead only author expression strings for
evaluation. An example of this typical use is the integration of SpEL into creating
XML or annotated based bean definitions as shown in the section Expression
support for defining bean definitions.
This chapter covers the features of the expression language, its API, and its
language syntax. In several places an Inventor and Inventor's Society class are
used as the target objects for expression evaluation. These class declarations and
the data used to populate them are listed at the end of the chapter.
6.2 Feature Overview
221
Literal expressions
Boolean and relational operators
Regular expressions
Class expressions
Method invocation
Relational operators
Assignment
Calling constructors
Bean references
Array construction
Inline lists
Ternary operator
Variables
Collection projection
Collection selection
Templated expressions
This section introduces the simple use of SpEL interfaces and its expression
language. The complete language reference can be found in the section Language
Reference.
The following code introduces the SpEL API to evaluate the literal string
expression 'Hello World'.
222
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();
The SpEL classes and interfaces you are most likely to use are located in the
packages org.springframework.expression and its sub packages andspel.support.
// invokes 'getBytes()'
Expression exp = parser.parseExpression("'Hello World'.bytes");
SpEL also supports nested properties using standard 'dot' notation, i.e.
prop1.prop2.prop3 and the setting of property values
223
ExpressionParser parser = new SpelExpressionParser();
// invokes 'getBytes().length'
Expression exp = parser.parseExpression("'Hello World'.bytes.length");
In the last line, the value of the string variable 'name' will be set to "Nikola Tesla".
The class StandardEvaluationContext is where you can specify which object the
"name" property will be evaluated against. This is the mechanism to use if the root
object is unlikely to change, it can simply be set once in the evaluation context. If
the root object is likely to change repeatedly, it can be supplied on each call
to getValue, as this next example shows:
224
/ Create and set a calendar
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
In some cases it can be desirable to use a configured evaluation context and yet
still supply a different root object on each call to getValue.getValue allows both to be
specified on the same call. In these situations the root object passed on the call is considered to
override any (which maybe null) specified on the evaluation context.
Note
In standalone usage of SpEL there is a need to create the parser, parse expressions and perhaps
provide evaluation contexts and a root context object. However, more common usage is to
provide only the SpEL expression string as part of a configuration file, for example for Spring
bean or Spring Web Flow definitions. In this case, the parser, evaluation context, root object and
any predefined variables are all set up implicitly, requiring the user to specify nothing other than
the expressions.
As a final introductory example, the use of a boolean operator is shown using the
Inventor object in the previous example.
225
6.3.1 The EvaluationContext interface
6.3.1.1 Type Conversion
class Simple {
public List<Boolean> booleanList = new ArrayList<Boolean>();
}
simple.booleanList.add(true);
// false is passed in here as a string. SpEL and the conversion service will
226
// correctly recognize that it needs to be a Boolean and convert it
parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");
// b will be false
Boolean b = simple.booleanList.get(0);
You can also refer to other bean properties by name, for example.
227
<property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>
6.4.2 Annotation-based configuration
228
Autowired methods and constructors can also use the @Value annotation.
@Autowired
public void configure(MovieFinder movieFinder,
@Value("#{ systemProperties['user.region'] }"} String
defaultLocale) {
this.movieFinder = movieFinder;
this.defaultLocale = defaultLocale;
}
// ...
}
public class MovieRecommender {
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
@Value("#{systemProperties['user.country']}"} String
defaultLocale) {
this.customerPreferenceDao = customerPreferenceDao;
this.defaultLocale = defaultLocale;
}
// ...
}
6.5 Language Reference
6.5.1 Literal expressions
The types of literal expressions supported are strings, dates, numeric values (int,
real, and hex), boolean and null. Strings are delimited by single quotes. To put a
single quote itself in a string use two single quote characters. The following listing
shows simple usage of literals. Typically they would not be used in isolation like
this, but as part of a more complex expression, for example using a literal on one
side of a logical comparison operator.
229
double avogadrosNumber = (Double)
parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
Numbers support the use of the negative sign, exponential notation, and decimal
points. By default real numbers are parsed using Double.parseDouble().
Navigating with property references is easy, just use a period to indicate a nested
property value. The instances of Inventor class, pupin and tesla, were populated
with data listed in the section Classes used in the examples. To navigate "down"
and get Tesla's year of birth and Pupin's city of birth the following expressions are
used.
// evals to 1856
int year = (Integer) parser.parseExpression("Birthdate.Year +
1900").getValue(context);
Case insensitivity is allowed for the first letter of property names. The contents of
arrays and lists are obtained using square bracket notation.
// Inventions Array
StandardEvaluationContext teslaContext = new StandardEvaluationContext(tesla);
// Members List
StandardEvaluationContext societyContext = new StandardEvaluationContext(ieee);
230
String name = parser.parseExpression("Members[0].Name").getValue(societyContext,
String.class);
String.class);
The contents of maps are obtained by specifying the literal key value within the
brackets. In this case, because keys for the Officers map are strings, we can
specify string literals.
// Officer's Dictionary
Inventor pupin =
parser.parseExpression("Officers['president']").getValue(societyContext,
Inventor.class);
// evaluates to "Idvor"
String city =
parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(society
Context,
String.class);
// setting values
parser.parseExpression("Officers['advisors']
[0].PlaceOfBirth.Country").setValue(societyContext,
"Croatia");
6.5.3 Inline lists
231
{} by itself means an empty list. For performance reasons, if the list is itself entirely
composed of fixed literals then a constant list is created to represent the
expression, rather than building a new list on each evaluation.
6.5.4 Array construction
Arrays can be built using the familiar Java syntax, optionally supplying an initializer
to have the array populated at construction time.
6.5.5 Methods
Methods are invoked using typical Java programming syntax. You may also invoke
methods on literals. Varargs are also supported.
// evaluates to true
boolean isMember = parser.parseExpression("isMember('Mihajlo
Pupin')").getValue(societyContext,
Boolean.class);
6.5.6 Operators
6.5.6.1 Relational operators
The relational operators; equal, not equal, less than, less than or equal, greater
than, and greater than or equal are supported using standard operator notation.
// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
232
// evaluates to false
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
// evaluates to true
boolean trueValue = parser.parseExpression("'black' <
'block'").getValue(Boolean.class);
// evaluates to false
boolean falseValue = parser.parseExpression("'xyz' instanceof
T(int)").getValue(Boolean.class);
// evaluates to true
boolean trueValue =
parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?
$'").getValue(Boolean.class);
//evaluates to false
boolean falseValue =
parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?
$'").getValue(Boolean.class);
6.5.6.2 Logical operators
The logical operators that are supported are and, or, and not. Their use is
demonstrated below.
// -- AND --
// evaluates to false
boolean falseValue = parser.parseExpression("true and
false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext,
Boolean.class);
233
// -- OR --
// evaluates to true
boolean trueValue = parser.parseExpression("true or
false").getValue(Boolean.class);
// evaluates to true
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext,
Boolean.class);
// -- NOT --
// evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);
6.5.6.3 Mathematical operators
The addition operator can be used on numbers, strings and dates. Subtraction can
be used on numbers and dates. Multiplication and division can be used only on
numbers. Other mathematical operators supported are modulus (%) and
exponential power (^). Standard operator precedence is enforced. These operators
are demonstrated below.
// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
String testString =
parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class); //
'test string'
// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
// Multiplication
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
// Division
234
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
// Modulus
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
// Operator precedence
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); //
-21
6.5.7 Assignment
// alternatively
String.class);
6.5.8 Types
The special 'T' operator can be used to specify an instance of java.lang.Class (the
'type'). Static methods are invoked using this operator as well.
The StandardEvaluationContext uses a TypeLocator to find types and
the StandardTypeLocator (which can be replaced) is built with an understanding of
the java.lang package. This means T() references to types within java.lang do not
need to be fully qualified, but all other type references must be.
Class dateClass =
parser.parseExpression("T(java.util.Date)").getValue(Class.class);
235
boolean trueValue =
parser.parseExpression("T(java.math.RoundingMode).CEILING <
T(java.math.RoundingMode).FLOOR")
.getValue(Boolean.class);
6.5.9 Constructors
Constructors can be invoked using the new operator. The fully qualified class
name should be used for all but the primitive type and String (where int, float, etc,
can be used).
Inventor einstein =
p.parseExpression("new org.spring.samples.spel.inventor.Inventor('Albert
Einstein',
'German')")
.getValue(Inventor.class);
.getValue(societyContext);
6.5.10 Variables
parser.parseExpression("Name = #newName").getValue(context);
The variable #this is always defined and refers to the current evaluation object
(against which unqualified references are resolved). The variable #root is always
defined and refers to the root context object. Although #this may vary as
components of an expression are evaluated, #root always refers to the root.
236
// create an array of integers
List<Integer> primes = new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
// all prime numbers > 10 from the list (using selection ?{...})
// evaluates to [11, 13, 17]
List<Integer> primesGreaterThanTen =
(List<Integer>) parser.parseExpression("#primes.?
[#this>10]").getValue(context);
6.5.11 Functions
You can extend SpEL by registering user defined functions that can be called
within the expression string. The function is registered with
theStandardEvaluationContext using the method.
This method is then registered with the evaluation context and can be used within
an expression string.
context.registerFunction("reverseString",
StringUtils.class.getDeclaredMethod("reverseString",
237
new Class[]
{ String.class }));
String helloWorldReversed =
parser.parseExpression("#reverseString('hello')").getValue(context,
String.class);
6.5.12 Bean references
If the evaluation context has been configured with a bean resolver it is possible to
lookup beans from an expression using the (@) symbol.
You can use the ternary operator for performing if-then-else conditional logic inside
the expression. A minimal example is:
String falseString =
parser.parseExpression("false ? 'trueExp' :
'falseExp'").getValue(String.class);
In this case, the boolean false results in returning the string value 'falseExp'. A
more realistic example is shown below.
parser.parseExpression("Name").setValue(societyContext, "IEEE");
societyContext.setVariable("queryName", "Nikola Tesla");
String queryResultString =
parser.parseExpression(expression).getValue(societyContext,
String.class);
// queryResultString = "Nikola Tesla is a member of the IEEE Society"
Also see the next section on the Elvis operator for an even shorter syntax for the
ternary operator.
238
6.5.14 The Elvis Operator
The Elvis operator is a shortening of the ternary operator syntax and is used in
the Groovy language. With the ternary operator syntax you usually have to repeat
a variable twice, for example:
Instead you can use the Elvis operator, named for the resemblance to Elvis' hair
style.
System.out.println(name); // 'Unknown'
tesla.setName(null);
239
ExpressionParser parser = new SpelExpressionParser();
tesla.setPlaceOfBirth(null);
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context,
String.class);
6.5.16 Collection Selection
Selection uses the syntax ?[selectionExpression]. This will filter the collection and
return a new collection containing a subset of the original elements. For example,
selection would allow us to easily get a list of Serbian inventors:
Selection is possible upon both lists and maps. In the former case the selection
criteria is evaluated against each individual list element whilst against a map the
selection criteria is evaluated against each map entry (objects of the Java
type Map.Entry). Map entries have their key and value accessible as properties for
use in the selection.
This expression will return a new map consisting of those elements of the original
map where the entry value is less than 27.
In addition to returning all the selected elements, it is possible to retrieve just the
first or the last value. To obtain the first entry matching the selection the syntax
is ^[...] whilst to obtain the last matching selection the syntax is $[...].
240
6.5.17 Collection Projection
A map can also be used to drive projection and in this case the projection
expression is evaluated against each entry in the map (represented as a
Java Map.Entry). The result of a projection across a map is a list consisting of the
evaluation of the projection expression against each map entry.
6.5.18 Expression templating
Expression templates allow a mixing of literal text with one or more evaluation
blocks. Each evaluation block is delimited with prefix and suffix characters that you
can define, a common choice is to use #{ } as the delimiters. For example,
String randomPhrase =
parser.parseExpression("random number is #{T(java.lang.Math).random()}",
new TemplateParserContext()).getValue(String.class);
The string is evaluated by concatenating the literal text 'random number is ' with
the result of evaluating the expression inside the #{ } delimiter, in this case the
result of calling that random() method. The second argument to the
method parseExpression() is of the type ParserContext. TheParserContext interface is
used to influence how the expression is parsed in order to support the expression
templating functionality. The definition of TemplateParserContext is shown below.
241
}
Inventor.java
package org.spring.samples.spel.inventor;
import java.util.Date;
import java.util.GregorianCalendar;
public Inventor() {
}
242
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public PlaceOfBirth getPlaceOfBirth() {
return placeOfBirth;
}
public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) {
this.placeOfBirth = placeOfBirth;
}
public void setInventions(String[] inventions) {
this.inventions = inventions;
}
public String[] getInventions() {
return inventions;
}
}
PlaceOfBirth.java
package org.spring.samples.spel.inventor;
243
}
Society.java
package org.spring.samples.spel.inventor;
import java.util.*;
244
7. Aspect Oriented Programming with Spring
7.1 Introduction
One of the key components of Spring is the AOP framework. While the Spring IoC
container does not depend on AOP, meaning you do not need to use AOP if you
don't want to, AOP complements Spring IoC to provide a very capable middleware
solution.
Spring 2.0 AOP
Spring 2.0 introduces a simpler and more powerful way of writing custom aspects using either a schema-
based approach or the @AspectJ annotation style. Both of these styles offer fully typed advice and use of
the AspectJ pointcut language, while still using Spring AOP for weaving.
The Spring 2.0 schema- and @AspectJ-based AOP support is discussed in this chapter. Spring 2.0 AOP
remains fully backwards compatible with Spring 1.2 AOP, and the lower-level AOP support offered by the
Spring 1.2 APIs is discussed in the following chapter.
7.1.1 AOP concepts
Let us begin by defining some central AOP concepts and terminology. These
terms are not Spring-specific... unfortunately, AOP terminology is not particularly
245
intuitive; however, it would be even more confusing if Spring used its own
terminology.
Target object: object being advised by one or more aspects. Also referred to
as the advised object. Since Spring AOP is implemented using runtime
proxies, this object will always be a proxied object.
246
compiler, for example), load time, or at runtime. Spring AOP, like other pure
Java AOP frameworks, performs weaving at runtime.
Types of advice:
Before advice: Advice that executes before a join point, but which does not
have the ability to prevent execution flow proceeding to the join point (unless
it throws an exception).
After returning advice: Advice to be executed after a join point completes
normally: for example, if a method returns without throwing an exception.
Around advice is the most general kind of advice. Since Spring AOP, like AspectJ,
provides a full range of advice types, we recommend that you use the least
powerful advice type that can implement the required behavior. For example, if you
need only to update a cache with the return value of a method, you are better off
implementing an after returning advice than an around advice, although an around
advice can accomplish the same thing. Using the most specific advice type
provides a simpler programming model with less potential for errors. For example,
you do not need to invoke the proceed() method on the JoinPoint used for around
advice, and hence cannot fail to invoke it.
In Spring 2.0, all advice parameters are statically typed, so that you work with
advice parameters of the appropriate type (the type of the return value from a
method execution for example) rather than Object arrays.
The concept of join points, matched by pointcuts, is the key to AOP which
distinguishes it from older technologies offering only interception. Pointcuts enable
advice to be targeted independently of the Object-Oriented hierarchy. For
example, an around advice providing declarative transaction management can be
247
applied to a set of methods spanning multiple objects (such as all business
operations in the service layer).
Spring AOP currently supports only method execution join points (advising the
execution of methods on Spring beans). Field interception is not implemented,
although support for field interception could be added without breaking the core
Spring AOP APIs. If you need to advise field access and update join points,
consider a language such as AspectJ.
Spring AOP's approach to AOP differs from that of most other AOP frameworks.
The aim is not to provide the most complete AOP implementation (although Spring
AOP is quite capable); it is rather to provide a close integration between AOP
implementation and Spring IoC to help solve common problems in enterprise
applications.
Thus, for example, the Spring Framework's AOP functionality is normally used in
conjunction with the Spring IoC container. Aspects are configured using normal
bean definition syntax (although this allows powerful "autoproxying" capabilities):
this is a crucial difference from other AOP implementations. There are some things
you cannot do easily or efficiently with Spring AOP, such as advise very fine-
grained objects (such as domain objects typically): AspectJ is the best choice in
such cases. However, our experience is that Spring AOP provides an excellent
solution to most problems in enterprise Java applications that are amenable to
AOP.
Spring AOP will never strive to compete with AspectJ to provide a comprehensive
AOP solution. We believe that both proxy-based frameworks like Spring AOP and
full-blown frameworks such as AspectJ are valuable, and that they are
complementary, rather than in competition. Spring 2.0 seamlessly integrates
Spring AOP and IoC with AspectJ, to enable all uses of AOP to be catered for
within a consistent Spring-based application architecture. This integration does not
affect the Spring AOP API or the AOP Alliance API: Spring AOP remains
backward-compatible. See the following chapter for a discussion of the Spring AOP APIs.
Note
One of the central tenets of the Spring Framework is that of non-invasiveness; this is the idea that
248
you should not be forced to introduce framework-specific classes and interfaces into your
business/domain model. However, in some places the Spring Framework does give you the
option to introduce Spring Framework-specific dependencies into your codebase: the rationale in
giving you such options is because in certain scenarios it might be just plain easier to read or
code some specific piece of functionality in such a way. The Spring Framework (almost) always
offers you the choice though: you have the freedom to make an informed decision as to which
option best suits your particular use case or scenario.
One such choice that is relevant to this chapter is that of which AOP framework (and which AOP
style) to choose. You have the choice of AspectJ and/or Spring AOP, and you also have the
choice of either the @AspectJ annotation-style approach or the Spring XML configuration-style
approach. The fact that this chapter chooses to introduce the @AspectJ-style approach first
should not be taken as an indication that the Spring team favors the @AspectJ annotation-style
approach over the Spring XML configuration-style.
See Section 7.4, “Choosing which AOP declaration style to use” for a more complete discussion
of the whys and wherefores of each style.
7.1.3 AOP Proxies
Spring AOP can also use CGLIB proxies. This is necessary to proxy classes,
rather than interfaces. CGLIB is used by default if a business object does not
implement an interface. As it is good practice to program to interfaces rather than
classes, business classes normally will implement one or more business
interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases
where you need to advise a method that is not declared on an interface, or where
you need to pass a proxied object to a method as a concrete type.
7.2 @AspectJ support
249
Using the AspectJ compiler and weaver enables use of the full AspectJ language,
and is discussed in Section 7.8, “Using AspectJ with Spring applications”.
The @AspectJ support is enabled by including the following element inside your
spring configuration:
<aop:aspectj-autoproxy/>
If you are using the DTD, it is still possible to enable @AspectJ support by adding
the following definition to your application context:
<bean
class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyC
reator" />
You will also need two AspectJ libraries on the classpath of your
application: aspectjweaver.jar and aspectjrt.jar. These libraries are available in
the 'lib' directory of an AspectJ installation (version 1.5.1 or later required), or in
the 'lib/aspectj' directory of the Spring-with-dependencies distribution.
7.2.2 Declaring an aspect
With the @AspectJ support enabled, any bean defined in your application context
with a class that is an @AspectJ aspect (has the @Aspectannotation) will be
automatically detected by Spring and used to configure Spring AOP. The following
example shows the minimal definition required for a not-very-useful aspect:
250
A regular bean definition in the application context, pointing to a bean class that
has the @Aspect annotation:
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class NotVeryUsefulAspect {
Aspects (classes annotated with @Aspect) may have methods and fields just like any other
class. They may also contain pointcut, advice, and introduction (inter-type) declarations.
You may register aspect classes as regular beans in your Spring XML configuration, or
autodetect them throuch classpath scanning - just like any other Spring-managed bean. However,
note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that
purpose, you need to add a separate @Component annotation (or alternatively a custom
stereotype annotation that qualifies, as per the rules of Spring's component scanner).
Advising aspects with other aspects?
In Spring AOP, it is not possible to have aspects themselves be the target of advice from other
aspects. The @Aspectannotation on a class marks it as an aspect, and hence excludes it from
auto-proxying.
7.2.3 Declaring a pointcut
Recall that pointcuts determine join points of interest, and thus enable us to control
when advice executes. Spring AOP only supports method execution join points for
Spring beans, so you can think of a pointcut as matching the execution of methods
on Spring beans. A pointcut declaration has two parts: a signature comprising a
name and any parameters, and a pointcut expression that
determines exactly which method executions we are interested in. In the
@AspectJ annotation-style of AOP, a pointcut signature is provided by a regular
method definition, and the pointcut expression is indicated using
251
the @Pointcut annotation (the method serving as the pointcut signature must have
a void return type).
An example will help make this distinction between a pointcut signature and a
pointcut expression clear. The following example defines a pointcut
named 'anyOldTransfer' that will match the execution of any method
named 'transfer':
Spring AOP supports the following AspectJ pointcut designators (PCD) for use in
pointcut expressions:
Other pointcut types
The full AspectJ pointcut language supports additional pointcut designators that are not supported in
Spring. These are: call, get, set, preinitialization, staticinitialization,
initialization, handler, adviceexecution, withincode, cflow, cflowbelow, if, @this,
and @withincode. Use of these pointcut designators in pointcut expressions interpreted by Spring AOP
will result in anIllegalArgumentException being thrown.
The set of pointcut designators supported by Spring AOP may be extended in future releases to support
more of the AspectJ pointcut designators.
execution - for matching method execution join points, this is the primary
pointcut designator you will use when working with Spring AOP
within - limits matching to join points within certain types (simply the
execution of a method declared within a matching type when using Spring
AOP)
this - limits matching to join points (the execution of methods when using
Spring AOP) where the bean reference (Spring AOP proxy) is an instance of
the given type
252
target - limits matching to join points (the execution of methods when using
Spring AOP) where the target object (application object being proxied) is an
instance of the given type
args - limits matching to join points (the execution of methods when using
Spring AOP) where the arguments are instances of the given types
@within -limits matching to join points within types that have the given
annotation (the execution of methods declared in types with the given
annotation when using Spring AOP)
@annotation - limits matching to join points where the subject of the join
point (method being executed in Spring AOP) has the given annotation
Because Spring AOP limits matching to only method execution join points, the
discussion of the pointcut designators above gives a narrower definition than you
will find in the AspectJ programming guide. In addition, AspectJ itself has type-
based semantics and at an execution join point both 'this' and 'target' refer to the
same object - the object executing the method. Spring AOP is a proxy-based
system and differentiates between the proxy object itself (bound to 'this') and the
target object behind the proxy (bound to 'target').
Note
Due to the proxy-based nature of Spring's AOP framework, protected methods are by
definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB
proxies (where this is technically possible but not recommendable for AOP purposes). As a
consequence, any given pointcut will be matched against public methods only!
If your interception needs include protected/private methods or even constructors, consider the
use of Spring-driven native AspectJ weaving instead of Spring's proxy-based AOP framework.
This constitutes a different mode of AOP usage with different characteristics, so be sure to make
yourself familiar with weaving first before making a decision.
Spring AOP also supports an additional PCD named 'bean'. This PCD allows you to
limit the matching of join points to a particular named Spring bean, or to a set of
253
named Spring beans (when using wildcards). The 'bean' PCD has the following
form:
bean(idOrNameOfBean)
The 'idOrNameOfBean' token can be the name of any Spring bean: limited
wildcard support using the '*' character is provided, so if you establish some
naming conventions for your Spring beans you can quite easily write a 'bean' PCD
expression to pick them out. As is the case with other pointcut designators, the
'bean' PCD can be &&'ed, ||'ed, and ! (negated) too.
Note
Please note that the 'bean' PCD is only supported in Spring AOP - and not in native AspectJ
weaving. It is a Spring-specific extension to the standard PCDs that AspectJ defines.
The 'bean' PCD operates at the instance level (building on the Spring bean name concept) rather
than at the type level only (which is what weaving-based AOP is limited to). Instance-based
pointcut designators are a special capability of Spring's proxy-based AOP framework and its
close integration with the Spring bean factory, where it is natural and straightforward to identify
specific beans by name.
Pointcut expressions can be combined using '&&', '||' and '!'. It is also possible to
refer to pointcut expressions by name. The following example shows three pointcut
expressions: anyPublicOperation (which matches if a method execution join point
represents the execution of any public method); inTrading (which matches if a
method execution is in the trading module), and tradingOperation (which matches if
a method execution represents any public method in the trading module).
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
@Pointcut("within(com.xyz.someapp.trading..*)")
private void inTrading() {}
254
protected pointcuts in the hierarchy, public pointcuts anywhere and so on).
Visibility does not affect pointcut matching.
When working with enterprise applications, you often want to refer to modules of
the application and particular sets of operations from within several aspects. We
recommend defining a "SystemArchitecture" aspect that captures common
pointcut expressions for this purpose. A typical such aspect would look as follows:
package com.xyz.someapp;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SystemArchitecture {
/**
* A join point is in the web layer if the method is defined
* in a type in the com.xyz.someapp.web package or any sub-package
* under that.
*/
@Pointcut("within(com.xyz.someapp.web..*)")
public void inWebLayer() {}
/**
* A join point is in the service layer if the method is defined
* in a type in the com.xyz.someapp.service package or any sub-package
* under that.
*/
@Pointcut("within(com.xyz.someapp.service..*)")
public void inServiceLayer() {}
/**
* A join point is in the data access layer if the method is defined
* in a type in the com.xyz.someapp.dao package or any sub-package
* under that.
*/
@Pointcut("within(com.xyz.someapp.dao..*)")
public void inDataAccessLayer() {}
/**
* A business service is the execution of any method defined on a service
* interface. This definition assumes that interfaces are placed in the
* "service" package, and that implementation types are in sub-packages.
*
* If you group service interfaces by functional area (for example,
* in packages com.xyz.someapp.abc.service and com.xyz.def.service) then
* the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))"
* could be used instead.
*
255
* Alternatively, you can write the expression using the 'bean'
* PCD, like so "bean(*Service)". (This assumes that you have
* named your Spring service beans in a consistent fashion.)
*/
@Pointcut("execution(* com.xyz.someapp.service.*.*(..))")
public void businessService() {}
/**
* A data access operation is the execution of any method defined on a
* dao interface. This definition assumes that interfaces are placed in the
* "dao" package, and that implementation types are in sub-packages.
*/
@Pointcut("execution(* com.xyz.someapp.dao.*.*(..))")
public void dataAccessOperation() {}
The pointcuts defined in such an aspect can be referred to anywhere that you
need a pointcut expression. For example, to make the service layer transactional,
you could write:
<aop:config>
<aop:advisor
pointcut="com.xyz.someapp.SystemArchitecture.businessService()"
advice-ref="tx-advice"/>
</aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
7.2.3.4 Examples
Spring AOP users are likely to use the execution pointcut designator the most
often. The format of an execution expression is:
256
All parts except the returning type pattern (ret-type-pattern in the snippet above),
name pattern, and parameters pattern are optional. The returning type pattern
determines what the return type of the method must be in order for a join point to
be matched. Most frequently you will use * as the returning type pattern, which
matches any return type. A fully-qualified type name