没有合适的资源?快使用搜索试试~ 我知道了~
首页Spring框架深度解析与应用
Spring框架深度解析与应用
需积分: 7 6 下载量 22 浏览量
更新于2024-07-27
收藏 4.2MB PDF 举报
"Spring框架文档"
本文档详细阐述了Spring框架的核心概念、模块组成、使用场景以及依赖管理等方面,旨在为开发者提供全面的指导。Spring框架是Java开发中的一个关键组件,以其依赖注入(Dependency Injection, DI)和控制反转(Inversion of Control, IoC)为核心特性,极大地简化了企业级应用的构建。
1. Spring框架概述
Spring框架是一个开源的Java平台,它为现代企业应用程序提供了广泛的支持。其核心目标是通过IoC和DI来减少代码间的耦合,提高软件的可测试性和可维护性。
1.1 依赖注入和控制反转
依赖注入是Spring的核心机制,允许对象在运行时通过容器进行依赖关系的配置,而不是在代码中硬编码依赖。控制反转则是这一机制的另一种表述,意味着组件的控制权由容器接管,而不是组件自身决定如何创建和管理其依赖。
1.2 模块
Spring框架由多个模块组成,包括:
- 核心容器:包含Bean工厂和ApplicationContext,是其他模块的基础。
- 数据访问/集成:支持JDBC、ORM(如Hibernate、JPA)、OXM(对象/XML映射)等数据访问技术。
- Web:包括Spring MVC和Web-Servlet模块,用于构建Web应用程序。
- 面向切面编程(AOP)和Instrumentation:提供声明式事务管理、切面编程等功能。
- 测试:支持单元测试和集成测试,如Spring Test和Spring Boot Test。
1.3 使用场景
Spring框架适用于多种应用场景,如:
- 创建单体应用
- 微服务架构
- 数据访问和事务管理
- 集成不同技术栈
- 测试驱动开发
2. 依赖管理与命名规范
Spring框架提供了对依赖管理的支持,无论是在Maven还是Ivy环境下,都有相应的规则来管理库的版本和依赖关系。
- Maven依赖管理:Spring项目通常在Maven的pom.xml文件中定义依赖,遵循一定的版本策略,确保项目中所有组件的一致性。
- Ivy依赖管理:对于使用Apache Ivy作为构建工具的项目,Spring同样提供了依赖管理的解决方案。
3. 日志
Spring框架支持多种日志解决方案,如Log4j、Logback等,允许开发者根据项目需求选择合适的日志系统,并方便地集成到Spring应用中。
Spring框架通过其强大的功能和灵活性,已经成为Java开发的基石,为开发者提供了构建复杂、可扩展且易于维护的应用程序的工具。无论是数据访问、Web开发还是测试,Spring都能提供相应的模块和最佳实践,帮助开发者高效地完成任务。
16.11. Handling exceptions ...................................................................................485
HandlerExceptionResolver ............................................................................485
@ExceptionHandler ......................................................................................486
16.12. Convention over configuration support ........................................................487
The Controller ControllerClassNameHandlerMapping ....................................487
The Model ModelMap (ModelAndView) .......................................................488
The View - RequestToViewNameTranslator ..................................................490
16.13. ETag support ..............................................................................................491
16.14. Configuring Spring MVC ............................................................................492
Enabling MVC Java Config or the MVC XML Namespace .............................492
Customizing the Provided Configuration ........................................................493
Configuring Interceptors ...............................................................................494
Configuring View Controllers .......................................................................495
Configuring Serving of Resources .................................................................495
mvc:default-servlet-handler ...........................................................................497
More Spring Web MVC Resources ................................................................498
Advanced Customizations with MVC Java Config ..........................................499
Advanced Customizations with the MVC Namespace .....................................499
17. View technologies ...................................................................................................501
17.1. Introduction .................................................................................................501
17.2. JSP & JSTL .................................................................................................501
View resolvers .............................................................................................501
'Plain-old' JSPs versus JSTL ..........................................................................502
Additional tags facilitating development ........................................................502
Using Spring's form tag library ......................................................................502
Configuration .......................................................................................502
The form tag ........................................................................................503
The input tag ........................................................................................504
The checkbox tag ..................................................................................504
The checkboxes tag ..............................................................................506
The radiobutton tag ...............................................................................506
The radiobuttons tag .............................................................................507
The password tag ..................................................................................507
The select tag .......................................................................................507
The option tag ......................................................................................508
The options tag .....................................................................................508
The textarea tag ....................................................................................509
The hidden tag ......................................................................................509
The errors tag .......................................................................................510
HTTP Method Conversion ....................................................................512
HTML5 Tags .......................................................................................512
17.3. Tiles ............................................................................................................513
Dependencies ...............................................................................................513
How to integrate Tiles ...................................................................................513
UrlBasedViewResolver .........................................................................514
Spring Framework
3.1 Reference Documentation xvi
ResourceBundleViewResolver ..............................................................514
SimpleSpringPreparerFactory and SpringBeanPreparerFactory ................514
17.4. Velocity & FreeMarker .................................................................................515
Dependencies ...............................................................................................515
Context configuration ...................................................................................515
Creating templates ........................................................................................516
Advanced configuration ................................................................................516
velocity.properties ................................................................................517
FreeMarker ..........................................................................................517
Bind support and form handling .....................................................................517
The bind macros ...................................................................................518
Simple binding .....................................................................................518
Form input generation macros ...............................................................519
HTML escaping and XHTML compliance .............................................523
17.5. XSLT ..........................................................................................................524
My First Words ............................................................................................524
Bean definitions ...................................................................................524
Standard MVC controller code ..............................................................524
Convert the model data to XML ............................................................525
Defining the view properties ..................................................................526
Document transformation ......................................................................526
Summary .....................................................................................................526
17.6. Document views (PDF/Excel) .......................................................................527
Introduction .................................................................................................527
Configuration and setup ................................................................................527
Document view definitions ....................................................................527
Controller code .....................................................................................528
Subclassing for Excel views ..................................................................528
Subclassing for PDF views ....................................................................529
17.7. JasperReports ...............................................................................................530
Dependencies ...............................................................................................530
Configuration ...............................................................................................530
Configuring the ViewResolver ..............................................................531
Configuring the Views ..........................................................................531
About Report Files ...............................................................................531
Using JasperReportsMultiFormatView ...................................................532
Populating the ModelAndView ......................................................................533
Working with Sub-Reports ............................................................................533
Configuring Sub-Report Files ................................................................534
Configuring Sub-Report Data Sources ...................................................534
Configuring Exporter Parameters ...................................................................535
17.8. Feed Views ..................................................................................................535
17.9. XML Marshalling View ................................................................................536
17.10. JSON Mapping View ..................................................................................536
18. Integrating with other web frameworks .....................................................................538
Spring Framework
3.1 Reference Documentation xvii
18.1. Introduction .................................................................................................538
18.2. Common configuration .................................................................................539
18.3. JavaServer Faces 1.1 and 1.2 .........................................................................540
DelegatingVariableResolver (JSF 1.1/1.2) ......................................................540
SpringBeanVariableResolver (JSF 1.1/1.2) .....................................................541
SpringBeanFacesELResolver (JSF 1.2+) ........................................................541
FacesContextUtils ........................................................................................542
18.4. Apache Struts 1.x and 2.x .............................................................................542
ContextLoaderPlugin ....................................................................................543
DelegatingRequestProcessor .................................................................544
DelegatingActionProxy .........................................................................544
ActionSupport Classes ..................................................................................545
18.5. WebWork 2.x ...............................................................................................545
18.6. Tapestry 3.x and 4.x .....................................................................................546
Injecting Spring-managed beans ....................................................................547
Dependency Injecting Spring Beans into Tapestry pages .........................548
Component definition files ....................................................................549
Adding abstract accessors ......................................................................550
Dependency Injecting Spring Beans into Tapestry pages - Tapestry 4.x style
.............................................................................................................552
18.7. Further Resources .........................................................................................553
19. Portlet MVC Framework .........................................................................................554
19.1. Introduction .................................................................................................554
Controllers - The C in MVC ..........................................................................555
Views - The V in MVC .................................................................................555
Web-scoped beans ........................................................................................556
19.2. The DispatcherPortlet ...................................................................................556
19.3. The ViewRendererServlet .............................................................................558
19.4. Controllers ...................................................................................................559
AbstractController and PortletContentGenerator .............................................559
Other simple controllers ................................................................................561
Command Controllers ...................................................................................561
PortletWrappingController ............................................................................562
19.5. Handler mappings ........................................................................................562
PortletModeHandlerMapping ........................................................................563
ParameterHandlerMapping ............................................................................564
PortletModeParameterHandlerMapping .........................................................564
Adding HandlerInterceptors ..........................................................................565
HandlerInterceptorAdapter ............................................................................565
ParameterMappingInterceptor .......................................................................565
19.6. Views and resolving them .............................................................................566
19.7. Multipart (file upload) support ......................................................................566
Using the PortletMultipartResolver ................................................................567
Handling a file upload in a form ....................................................................567
19.8. Handling exceptions .....................................................................................570
Spring Framework
3.1 Reference Documentation xviii
19.9. Annotation-based controller configuration .....................................................571
Setting up the dispatcher for annotation support ..............................................571
Defining a controller with @Controller ..........................................................571
Mapping requests with @RequestMapping .....................................................572
Supported handler method arguments ............................................................573
Binding request parameters to method parameters with @RequestParam ..........575
Providing a link to data from the model with @ModelAttribute .......................576
Specifying attributes to store in a Session with @SessionAttributes .................576
Customizing WebDataBinder initialization .....................................................577
Customizing data binding with @InitBinder ...........................................577
Configuring a custom WebBindingInitializer ..........................................577
19.10. Portlet application deployment ....................................................................578
VI. Integration ......................................................................................................................579
20. Remoting and web services using Spring ..................................................................580
20.1. Introduction .................................................................................................580
20.2. Exposing services using RMI ........................................................................581
Exporting the service using the RmiServiceExporter .......................................581
Linking in the service at the client .................................................................582
20.3. Using Hessian or Burlap to remotely call services via HTTP ...........................583
Wiring up the DispatcherServlet for Hessian and co. .......................................583
Exposing your beans by using the HessianServiceExporter ..............................583
Linking in the service on the client ................................................................584
Using Burlap ................................................................................................584
Applying HTTP basic authentication to a service exposed through Hessian or
Burlap ..........................................................................................................585
20.4. Exposing services using HTTP invokers ........................................................585
Exposing the service object ...........................................................................585
Linking in the service at the client .................................................................586
20.5. Web services ................................................................................................587
Exposing servlet-based web services using JAX-RPC .....................................588
Accessing web services using JAX-RPC ........................................................588
Registering JAX-RPC Bean Mappings ...........................................................590
Registering your own JAX-RPC Handler .......................................................591
Exposing servlet-based web services using JAX-WS ......................................591
Exporting standalone web services using JAX-WS .........................................592
Exporting web services using the JAX-WS RI's Spring support .......................593
Accessing web services using JAX-WS ..........................................................593
20.6. JMS .............................................................................................................594
Server-side configuration ..............................................................................595
Client-side configuration ...............................................................................596
20.7. Auto-detection is not implemented for remote interfaces .................................597
20.8. Considerations when choosing a technology ...................................................597
20.9. Accessing RESTful services on the Client ......................................................598
RestTemplate ...............................................................................................598
Working with the URI ..........................................................................600
Spring Framework
3.1 Reference Documentation xix
Dealing with request and response headers .............................................601
HTTP Message Conversion ...........................................................................601
StringHttpMessageConverter .................................................................602
FormHttpMessageConverter ..................................................................602
ByteArrayHttpMessageConverter ..........................................................602
MarshallingHttpMessageConverter ........................................................602
MappingJacksonHttpMessageConverter .................................................603
SourceHttpMessageConverter ...............................................................603
BufferedImageHttpMessageConverter ...................................................603
21. Enterprise JavaBeans (EJB) integration ....................................................................604
21.1. Introduction .................................................................................................604
21.2. Accessing EJBs ............................................................................................604
Concepts ......................................................................................................604
Accessing local SLSBs .................................................................................605
Accessing remote SLSBs ..............................................................................606
Accessing EJB 2.x SLSBs versus EJB 3 SLSBs ..............................................607
21.3. Using Spring's EJB implementation support classes ........................................607
EJB 2.x base classes .....................................................................................607
EJB 3 injection interceptor ............................................................................609
22. JMS (Java Message Service) ....................................................................................611
22.1. Introduction .................................................................................................611
22.2. Using Spring JMS ........................................................................................611
JmsTemplate ................................................................................................611
Connections .................................................................................................612
Caching Messaging Resources ...............................................................613
SingleConnectionFactory ......................................................................613
CachingConnectionFactory ...................................................................613
Destination Management ...............................................................................613
Message Listener Containers .........................................................................614
SimpleMessageListenerContainer ..........................................................615
DefaultMessageListenerContainer .........................................................615
Transaction management ...............................................................................615
22.3. Sending a Message .......................................................................................616
Using Message Converters ............................................................................617
SessionCallback and ProducerCallback ..........................................................618
22.4. Receiving a message .....................................................................................618
Synchronous Reception .................................................................................618
Asynchronous Reception - Message-Driven POJOs ........................................618
The SessionAwareMessageListener interface .................................................619
The MessageListenerAdapter ........................................................................619
Processing messages within transactions ........................................................621
22.5. Support for JCA Message Endpoints ..............................................................622
22.6. JMS Namespace Support ..............................................................................624
23. JMX .......................................................................................................................629
23.1. Introduction .................................................................................................629
Spring Framework
3.1 Reference Documentation xx
剩余847页未读,继续阅读
2009-06-20 上传
2019-03-13 上传
730 浏览量
2013-09-21 上传
Lemon_zhang
- 粉丝: 3
- 资源: 49
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MATLAB实现小波阈值去噪:Visushrink硬软算法对比
- 易语言实现画板图像缩放功能教程
- 大模型推荐系统: 优化算法与模型压缩技术
- Stancy: 静态文件驱动的简单RESTful API与前端框架集成
- 掌握Java全文搜索:深入Apache Lucene开源系统
- 19计应19田超的Python7-1试题整理
- 易语言实现多线程网络时间同步源码解析
- 人工智能大模型学习与实践指南
- 掌握Markdown:从基础到高级技巧解析
- JS-PizzaStore: JS应用程序模拟披萨递送服务
- CAMV开源XML编辑器:编辑、验证、设计及架构工具集
- 医学免疫学情景化自动生成考题系统
- 易语言实现多语言界面编程教程
- MATLAB实现16种回归算法在数据挖掘中的应用
- ***内容构建指南:深入HTML与LaTeX
- Python实现维基百科“历史上的今天”数据抓取教程
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功