Spring注解(零配置)

本文介绍Spring框架中的零配置概念,即使用注解替代XML配置文件。重点讲解了@Controller、@Service等常用注解的用途,以及<context:component-scan>元素如何指定Spring扫描的包。此外,还对比了@Resource与@Autowired的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Spring框架的时候,经常要用到XML文件作为它的配置文件,在xml中配置Bean的依赖、作用域、属性值、自动装配等。但除了xml文件配置方式,Spring还提供了另外一种配置方式——Annotation(注解),来达到“零配置”。

那么,Spring“零配置”存在一个问题,Spring容器如何知道哪些Java类是Spring Bean,进而对其预初始化呢?

这就要用到Annotation(注解)了,Spring通过注解来标注Java类为Spring Bean类。如,在包com.xingnana.controller下创建java类MainController,其中@Controller就是注解,标注一个控制器组件类。然后通过xml配置文件,显示指定Spring搜索指定包下的java类。

package com.xingnana.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
        //return "test";
    }

}

同时,在mvc-dispatcher-sevlet.xml中添加<context:component-scan>元素,指定Spring要搜索的包,如下:

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--指明 controller 所在包,并扫描其中的注解 自动扫描指定包及其子包下的所有Spring bean类-->
    <context:component-scan base-package="com.xingnana.controller"/>

 <!-- 开启注解 -->
    <mvc:annotation-driven/>

<!-- 静态资源(js、image、css等)的访问 --> <mvc:default-servlet-handler/> </beans>

 

此外,还可以在<context:component-scan>元素下,添加<include-filter>元素,或者<exclude-filter>元素来指定Spring Bean。<include-filter>元素用于指定满足规则的java类,会被当做Spring Bean。<exclude-filter>元素用于指定满足规则的java类,不会被当做Spring Bean。这两个元素都有属性type和expression。

 

type:指定过滤器类型,如annotation(注解名),assignable(类名过滤器),regex(正则表达式),aspectj

expression:

Spring常用的注解有如下几个:

1. @Component:标注一个普通的Spring Bean;

2. @Controller:标注一个控制器组件类;

3. @Service:标注一个业务逻辑组件类;

4. @Repository:标注一个DAO组件类,能够启用与Spring数据访问相关联的其他功能;

5. @Scope:指定Bean实例的作用域;如下:

//指定Spring Bean的作用域为prototype
@Scope("prototype")
//指定Student作为Spring Bean
@Controller
public class Student{
String name;
int age;
...
}

6. @Resource:配置依赖(依赖注入),相当于xml配置文件中元素<property>的属性ref,指向一个需要被注入的bean实例。它可以修饰setter方法和类属性,用法:@Resource(name="bean"),name属性可以省略。(name相当于xml配置文件中<bean>元素的id)

跟@Resource类似的还有一个@Autowired(自动装配) ,也可以写在属性上或者setter方法上。

自动装配:

有时没有必要显示地(setter或者构造函数注入)在Bean定义中定义依赖,可以让Spring自动地向Bean中注入依赖。该过程被称为自动装配。

区别是:@Autowired 与@Resource的区别(详细)

(1) @Autowired默认按类型装配(这个注解是属业Spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

@Autowired()@Qualifier("baseDao")
privateBaseDao baseDao;

(2)@Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。注意,如果name属性一旦指定,就只会按照名称进行装配。

@Resource(name="baseDao")
privateBaseDao baseDao;
public class Student{
@Resource(name="person")//Filed注入,若省略name属性,则name属性默认值与Filed同名
private Person person;
@Resource//省略name属性,则name属性默认值为setPerson去掉set,且Perosn首字母P小写
public void setPerson(Person p){
this.person=p;
}

}

7. @PostConstruct和@PreDestory:用于修饰方法,分别与xml配置文件中<bean>元素的init-method和destory-method属性类似。@PostConstruct修饰init()方法,让Spring在该Bean的依赖关系注入完成之后回调该方法;@PreDestory修饰close()方法,让Spring在销毁该Bean之前回调该方法。

public class Student{
...
@PostConstruct
public void init(){
//初始化操作
}

@PreDestory
public void close(){
//释放资源等
}

}

8. @DependsOn:修饰Spring Bean类和方法,用于强制初始化其他Bean。例如,一个执行JVM级别初始化Bean必须在其他Bean创建之前。当在类级别使用@DependsOn,通常在组件扫描期间处理,此时,只有在@DependsOn注解内指定的Bean被创建后,才会创建本类。

以下是在初始化Student Bean之前,先强制初始化bena1和bean2。

@DependsOn({"bean1","bean2"})
@Cotroller
public class Student{
}

9. @Lazy:用于修饰Spring Bean类,用于指定该Bean是否延迟始化,它有一个boolean的属性,@Lazy(true)。

默认情况下,Spring 会自动搜索注解标注的Java类,并把它们当做Spring Bean处理。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值