Spring MVC 简述

300 阅读5分钟

Spring MVC

Spring 2.5 开始就可以使用注解,需要在配置文件中启用。 你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身。

启用

<context:annotation-config/>

常用注解

@Required 注解应用于 bean 属性的 setter 方法
@Autowired 以应用到 bean 属性的 setter 方法,非 setter 方法,构造函数和属性
@Qualifier 通过指定确切的将被连线的 bean,@Autowired 和 @Qualifier 注解可以用来删除混乱
JSR-250 Annotations Spring 支持 JSR-250 的基础的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解
  1. @Required表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。
  2. @Autowired 注释自动注入。可以用在属性或者构造函数中。**注意:**默认意味着依赖是必须的,然而可用 @Autowired(required=false)关闭默认行为。
  3. @Qualifier 多个相同类型的 bean 只装配一个时,你可以使用 @Autowired @Qualifier("student1")注释,指定哪一个真正的 bean 被装配。
  4. Spring JSR-250注解:包
@PostConstruct 类似 init-method
@PreDestroy 类似destroy-method
@Resource 遵循 by-name 自动连接语义,

注意: @Resource(name= "spellChecker") 没有明确指定一个 ‘name’,在字段的情况下,默认使用字段名;在一个 setter 方法情况下,它默认使用 bean 属性名称。

java 注解

  • @Configuration 代表容器和 @Bean 代表注入对象
  • @import 注解允许从另一个配置类中加载 @Bean 定义。
  • @Bean 注解支持指定任意的初始化和销毁的回调方法,@Bean(initMethod = "init", destroyMethod = "cleanup" )
  • @Scope 注解的bean的范围。

Spring 中的事件分为以下几类

基本事件

ApplicationContext,它负责管理 beans 的完整生命周期当启动时回调。如果一个 bean 实现 ApplicationListener ,那么每次 ApplicationEvent 被发布到 ApplicationContext 上,那个 bean 会被通知。 Spring 提供了以下的标准事件:

ContextRefreshedEvent ApplicationContext 被初始化或刷新时,或者在使用 refresh() 方法来发生触发
ContextStartedEvent start()触发,此时可以调查数据库,或者重启任何停止的应用程序。
ContextStoppedEvent stop()触发,此时你可以在接受到这个事件后做必要的清理的工作。
ContextClosedEvent close()触发,一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。
RequestHandledEvent 这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。

注意:Spring 的事件处理是单线程的,所以如果一个事件被发布,直至并且除非所有的接收者得到的该消息,该进程被阻塞并且流程将不会继续。

监听上下文事件

为了监听上下文事件,一个 bean 应该实现只有一个方法 onApplicationEvent() 的 ApplicationListener 接口。

Spring 中的自定义事件

可按照以下几步实现

  1. 创建一个事件类,extends ApplicationEvent 比如:CustomEvent。这个类必须定义一个默认的构造函数,它应该从 ApplicationEvent 类中继承的构造函数。
  2. 发布事件,implements ApplicationEventPublisherAware并在XML 配置文件中声明这个类作为一个 bean。
      CustomEvent ce = new CustomEvent(this);
      publisher.publishEvent(ce);
   }

接着同系统事件实现监听。

Spring 框架的 AOP

Spring AOP:面向切面的编程。 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你可以在方法执行之前或之后添加额外的功能。

为了在本节的描述中使用 aop 命名空间标签,你需要导入 spring-aop j架构,

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

xml配置实现
  1. 声明一个 aspect:一个 aspect 是使用 元素声明的,支持的 bean 是使用 ref 属性引用的,如下所示
  2. 声明一个切入点: 一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。
  3. 声明建议 你可以使用 <aop:{ADVICE NAME}> 元素在一个 中声明五个建议中的任何一个,如下所示
<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
      <aop:pointcut id="businessService"
         expression="execution(* com.xyz.myapp.service.*.*(..))"/>
      <!-- a before advice definition -->
      <aop:before pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after advice definition -->
      <aop:after pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref="businessService"
         returning="retVal"
         method="doRequiredTask"/>
      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref="businessService"
         throwing="ex"
         method="doRequiredTask"/>
      <!-- an around advice definition -->
      <aop:around pointcut-ref="businessService" 
         method="doRequiredTask"/>
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
注解实现
  1. 引入引入lib
  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

  1. 在xml 中配置支持@AspectJ <aop:aspectj-autoproxy/>
  2. 声明一个 aspect和其他任何正常的 bean 一样,用@Aspect注解,然后在xml配置。
  3. 声明一个切入点,即关注点。

实例: ‘getname’ 切点,与 com.tutorialspoint 包下的 Student 类中的 getName() 相匹配:

@Pointcut("execution(* com.tutorialspoint.Student.getName(..))") 
private void getname() {}
  1. 声明建议:可用@{ADVICE-NAME} 注释声明五个建议中的任意一个例子:
@Before("businessService()")
public void doBeforeTask(){
 ...
}

Spring JDBC 框架

JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。JdbcTemplate 关键类 JdbcTemplate 类执行 SQL 查询、更新语句和存储过程调用,执行迭代结果集和提取返回参数值。捕获并转化异常。

  1. 引入对应的库mysql-connector-java.jar,org.springframework.jdbc.jar 和 org.springframework.transaction.jar。
  2. 配置数据源 )1 数据库 TEST 中创建一个数据库表 Student。
CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

)2在xml中配置数据源到JdbcTemplate中。

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
   <property name="username" value="root"/>
   <property name="password" value="password"/>
</bean>

3.jdbcTemplate 操作数据库执行 CRUD jdbcTemplateObject.queryForInt( SQL );。可通过执行SQL 语句和DDL 语句 execute(..)建议先定义数据库操作接口StudentDAO,然后实现 public class StudentJDBCTemplate implements StudentDAO {}

如有疏漏,请指出,如有问题可以通过如下方式联系我

简书 csdn 掘金 klvens跑码场