spring中常用注解

@Autowired:自动通过类型,名字。如果不能唯一自动装配使用@Qualifier(value="""),也可放在setter方法上

@Nullable:允许参数为空

@Resources:自动通过名字然后在类型

@Component:组件,放在类上,说明该类被Spring管理了就是所谓的bean,放在pojo层

@Value:给属性赋值,也可以放在setter方法上。

@Repository:组件,相当于Component,但放在Dao层

@Controller:组件,相当于Component,但放在Controller层

@Component,@Repository,@Controller被扫描入IOC容器中称为组件

@Scope("singleto"):单例模式或原型模式(prototype)

纯java的配置类,相当于xml文件

@Configuration:这个也会让Spring容器托管,注册到容器中,因为他本身也是一个@Component,@Configuration代表这是一个配置类

@ComponentScan(" "):扫描包

@Bean:相当于以前的bean标签。名字相当于方法名,返回值相当于bean标签中的class属性 new AnnotationConfigApplicationContext(配置类.class);

@Import(.class):导入另一个配置类

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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--开启注解支持-->
        <context:annotation-config/>
        <!--指定要扫描的包,则该包下的注解能生效-->
        <context:component-scan base-package=""/>
    </beans>
原文地址:https://www.cnblogs.com/Difcipo/p/13052385.html