- 浏览: 248704 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
sweed0:
为何每一段代码都重复一次呢?
spring注解实例二 -
Gary_Huangpf:
- - 插件报错啊
Ext前台分页 -
ddvk2007:
版主 我想請問你所說的mapreduce是hadoop的還是g ...
MapReduce中的Shuffle和Sort分析 -
人可木:
好问章,楼主写的相当详细。。。多谢。。。
findbugs插件的安装与应用 -
hautbbs:
按照博主的方法启动调试出现jvm terminated.Ex ...
10分钟学会使用MyEclipse断点调试js
先来构建一个极为简单的web应用,从controller到dao。不考虑具体实现,只是先对整体架构有一个清晰的了解。
我们将用到如下jar包:
aopalliance-1.0.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar
先看web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID"
- version="2.5">
- <display-name>spring</display-name>
- <!-- 应用路径 -->
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.webapp.root</param-value>
- </context-param>
- <!-- Log4J 配置 -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4fRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <!--Spring上下文 配置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <!-- 字符集 过滤器 -->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Spring 监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- Spring 分发器 -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring</display-name> <!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>spring.webapp.root</param-value> </context-param> <!-- Log4J 配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> </context-param> <context-param> <param-name>log4fRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!--Spring上下文 配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 字符集 过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Spring 分发器 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
有不少人问我,这段代码是什么:
- <!-- 应用路径 -->
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.webapp.root</param-value>
- </context-param>
<!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>spring.webapp.root</param-value> </context-param>
这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?
- <!-- Log4J 配置 -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4fRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
<!-- Log4J 配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> </context-param> <context-param> <param-name>log4fRefreshInterval</param-name> <param-value>60000</param-value> </context-param>
这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!并且,log4j的配置文件只能放在classpath根路径。同时,需要通过commons-logging配置将日志控制权转交给log4j。同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
最后,记得配置log4j的监听器:
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。按照分层思想,统领service层和dao层。servlet.xml则单纯控制controller层。
- <?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.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <import
- resource="service.xml" />
- <import
- resource="dao.xml" />
- </beans>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="service.xml" /> <import resource="dao.xml" /> </beans>
applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。
这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。
service.xml
- <?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.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan
- base-package="org.zlex.spring.service" />
- </beans>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.zlex.spring.service" /> </beans>
注意,这里通过<context:component-scan />标签指定了业务层的基础包路径——“org.zlex.spring.service”。也就是说,业务层相关实现均在这一层。这是有必要的分层之一。
dao.xml
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
- <context:component-scan
- base-package="org.zlex.spring.dao" />
- </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="org.zlex.spring.dao" /> </beans>
dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml
- <?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.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan
- base-package="org.zlex.spring.controller" />
- <bean
- id="urlMapping"
- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- </beans>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.zlex.spring.controller" /> <bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> </beans>
包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是默认实现,可以不写,Spring容器默认会默认使用该类。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接关系到多动作控制器配置是否可用!
简单看一下代码结构,如图:
Account类是来存储账户信息,属于域对象,极为简单,代码如下所示:
Account.java
- /**
- * 2010-1-23
- */
- package org.zlex.spring.domain;
- import java.io.Serializable;
- /**
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
- * @version 1.0
- * @since 1.0
- */
- public class Account implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -533698031946372178L;
- private String username;
- private String password;
- /**
- * @param username
- * @param password
- */
- public Account(String username, String password) {
- this.username = username;
- this.password = password;
- }
- /**
- * @return the username
- */
- public String getUsername() {
- return username;
- }
- /**
- * @param username the username to set
- */
- public void setUsername(String username) {
- this.username = username;
- }
- /**
- * @return the password
- */
- public String getPassword() {
- return password;
- }
- /**
- * @param password the password to set
- */
- public void setPassword(String password) {
- this.password = password;
- }
- }
/** * 2010-1-23 */ package org.zlex.spring.domain; import java.io.Serializable; /** * * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @version 1.0 * @since 1.0 */ public class Account implements Serializable { /** * */ private static final long serialVersionUID = -533698031946372178L; private String username; private String password; /** * @param username * @param password */ public Account(String username, String password) { this.username = username; this.password = password; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } }
通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口Serializable
我们再来看看控制器,这就稍微复杂了一点代码如下所示:
AccountController .java
- /**
- * 2010-1-23
- */
- package org.zlex.spring.controller;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.ServletRequestUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.zlex.spring.service.AccountService;
- /**
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
- * @version 1.0
- * @since 1.0
- */
- @Controller
- @RequestMapping("/account.do")
- public class AccountController {
- @Autowired
- private AccountService accountService;
- @RequestMapping(method = RequestMethod.GET)
- public void hello(HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String username = ServletRequestUtils.getRequiredStringParameter(
- request, "username");
- String password = ServletRequestUtils.getRequiredStringParameter(
- request, "password");
- System.out.println(accountService.verify(username, password));
- }
- }
/** * 2010-1-23 */ package org.zlex.spring.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.zlex.spring.service.AccountService; /** * * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @version 1.0 * @since 1.0 */ @Controller @RequestMapping("/account.do") public class AccountController { @Autowired private AccountService accountService; @RequestMapping(method = RequestMethod.GET) public void hello(HttpServletRequest request, HttpServletResponse response) throws Exception { String username = ServletRequestUtils.getRequiredStringParameter( request, "username"); String password = ServletRequestUtils.getRequiredStringParameter( request, "password"); System.out.println(accountService.verify(username, password)); } }
分段详述:
@Controller @RequestMapping("/account.do")
这两行注解,@Controller是告诉Spring容器,这是一个控制器类,@RequestMapping("/account.do")是来定义该控制器对应的请求路径(/account.do)
- <span style=
- spring注解1.rar (2.2 MB)
- 下载次数: 201
发表评论
-
spring AOP
2012-06-13 11:14 928先简单的说说spring aop使用的好处:利用AOP横向添加 ... -
spring IOC的使用
2012-06-13 10:58 969Ioc和他的作用,简单的来讲,就是由容器控制程序之间的关 ... -
spring IOC介绍
2012-06-13 10:55 1366首先想说说IoC(Inversion of ... -
spring概述
2012-06-13 10:52 817随着越来越多的项目 ... -
spring学习总结
2010-02-04 08:49 978先说说简单地使用Struts2做Web时的经历: 1.经常需要 ... -
spring注解实例二
2010-01-26 11:22 2384昨天对Spring注解有了一个整体认识,至少完成了一个简单的w ... -
spring注解配置文件解析
2010-01-26 11:01 35831、applicationContext.xml ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC(二)
2010-01-22 11:13 3439我们在 ② 处添加了一个 ModelMap 属性,其属性名为 ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC(一)
2010-01-22 11:05 1375基于注解的配置有越来越流行的趋势,Spring 2.5 顺应这 ... -
spring注解详解
2010-01-22 09:57 13581.准备工作(1)导入common-annotations.j ... -
spring注解
2010-01-13 09:56 1032Spring JSR-250注解 注释配 ... -
spring相关配置内容解析
2010-01-05 14:27 13351、web.xml配置文件: <? ... -
Spring MVC 配置【转】
2010-01-03 14:51 1073一,配置分发器 Dispatche ... -
spring mvc 学习笔记
2010-01-03 14:41 1168一、与struts的不同 1.Spr ... -
Spring Mvc快速入门
2010-01-03 14:20 1756Spring MVC是结构最清晰的MVC ... -
Spring 可代码简化操作类
2009-12-28 09:31 1069SimpleJdbcTemplate类是Jd ... -
Spring 命名参数操作类NamedParameterJdbcTemplate
2009-12-28 09:28 1813在传统的SQL语句中,参数都是用'?'占 ... -
Spring JdbcTemplate操作类
2009-12-24 10:42 27991.JdbcTemplate ...
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
本实例将深入探讨Spring中的注解使用,特别是如何创建一个最简单的Spring注解实例。 首先,我们需要了解Spring的核心组件——Spring容器,也称为ApplicationContext。这个容器负责管理应用程序中的bean,包括它们的...
本文将深入探讨Spring注解注入的相关知识点,以及如何通过提供的压缩包文件进行实践学习。 **1. Spring注解概述** 在Spring框架中,注解提供了元数据的方式来配置bean,使得我们不再需要XML配置文件。常见的注解...
3. **注解实例**:通过实例,你可以学习如何使用这些注解来简化代码,比如使用`@RequestMapping`处理不同的URL请求,`@Autowired`自动装配bean,以及`@Service`和`@Component`在组件扫描中的作用。 4. **Spring与...
在这个"我的博客spring注解概述的示例代码"资源中,我们可能找到如何使用`@Autowired`来自动装配bean的实例。 首先,让我们了解什么是依赖注入。在面向对象编程中,一个类往往依赖于其他类来完成特定任务。依赖注入...
总的来说,这个入门实例旨在帮助初学者理解如何在没有使用注解的情况下,通过XML配置文件集成SpringMVC、Spring和Hibernate,完成一个简单的Web应用。虽然现在的最佳实践倾向于使用注解和Spring Boot,但理解非注解...
1. `@Controller` 注解:这个注解用于标记一个类作为Spring MVC的控制器。当Spring容器启动时,会扫描带有@Controller的类,并将这些类实例化,用于处理HTTP请求。例如: ```java @Controller public class ...
Spring注解的主要目的是消除XML配置文件,使开发者能够通过在类或方法上直接添加注解来声明对象及其依赖关系。这个小例子将深入探讨Spring框架中的主要注解及其用法。 1. `@Component`、`@Service`、`@Repository` ...
总的来说,这个"spring mvc 注解实例"项目为你提供了一个学习Spring MVC基本功能的起点,包括注解驱动的控制器、拦截器的使用以及数据库操作。通过运行提供的代码,你可以亲自体验和理解这些概念,从而更好地掌握...
### Spring MVC 注解及页面跳转实例解析 #### 一、Spring MVC 页面跳转实例概述 在本实例中,我们将构建一个简单的Spring MVC应用程序,演示如何处理HTTP请求并实现页面跳转。通过这个实例,我们可以了解Spring ...
IT学习者Spring MVC注解实例.pdf
本文将深入探讨Spring注解的基本原理,包括它们如何被解析、处理以及如何影响应用程序的生命周期。 首先,我们需要了解注解在Java语言中的本质。注解是一种元数据,允许程序员在源代码中嵌入信息,这些信息可以被...
spring3整合EhCache注解实例
### Spring注解知识点详解 #### 1. Spring注解基础 在Spring框架中,注解是一种轻量级的依赖注入方式,能够简化配置并提高开发效率。在本节中,我们主要介绍几个Spring中常用的注解,它们分别是@Component、@...
总的来说,Spring MVC 注解实例展示了如何通过注解方式简化 Web 应用的配置和开发。这种方式减少了 XML 配置,提高了代码的可读性和可维护性。通过 `web.xml` 和 `spring-servlet.xml` 文件的配置,以及 Controller ...
### Spring注解详解及实例分析 #### 概述 随着技术的发展与演进,Spring框架在不断迭代过程中引入了大量的注解来简化应用的配置过程。相比于传统的XML配置方式,注解配置提供了更为简洁且直观的方式来定义Bean以及...
Spring注解是Spring框架中的一个重要特性,它极大地简化了配置,提高了代码的可读性和可维护性。在本文中,我们将深入探讨如何使用Spring注解进行属性注入,并重点关注`@Autowired`和`@Qualifier`这两个关键注解。 ...
1. **类型匹配**:Spring会根据注解的属性类型去BeanDefinitionRegistry中查找相同类型的bean。如果找到多个,那么需要进一步判断。 2. **候选bean的确定**:如果有多个候选bean,Spring会基于以下规则进行选择: ...
尽管我们无法直接访问这个链接,但我们可以基于常见的Spring注解配置实践来解释相关概念。 1. `@Component`:这是Spring中的基础注解,用于标记一个类为Spring管理的bean。它的子注解包括`@Service`、`@Repository`...