@注解说明

注解说明

注解 作用
@Autowired 自动注入,通过类型。【1】
@Qualifier 可指定beanId【2】
@Resource 是java层面的注解,也可实现自动装配。并且是组合注解【3】
@Nullable 段标记了这个注解,说明这个字段可以为null;
@Component @Repository @Service @Controller 组件放在类上,说明这个类被Spring管理了,就是bean【4】
@Value 赋值【5】
@Scope() 作用域:singleton,prototype
@Aspect 标注这个类是个切面

【1】
使用注解前提:
1.导入约束
applicationContext.xml

xmlns:context="http://www.springframework.org/schema/context"  
======================================
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd

2.配置注解的支持【重要】

<context:annotation-config/>

3.注意:最终还是通过byName实现,要注意实体类属性名和beanId一致。
【2】
在这里插入图片描述
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
【3】
@Resource(name=“beanId”)指定beanId
自动注入通过名字,名字没找到,通过类型。相当于@Autowired+@Qualifier。

【4】

//等价于  <bean id="user" class="com.my.pojo.User"/>
@Component
public class User {
    
    
    public String name="aaa";
}
@Conponent有几个衍生注解,我们在web开发中 ,会按照mvc三层框架分层!
 #pojo 【@Conponent】
 #dao   【@Repository】
 #service 【@Service】
 #controller 【@Controller】
 它们四个是等价的,都会被装配到Spring中。

【5】
相当于<property name="name" value="张三"/>

@Component
public class User {
    
    
    @Value("张三")
    public String name;
}

Guess you like

Origin blog.csdn.net/fhuqw/article/details/121291771
Recommended