Spring 自动注入

bean 头文件:

<?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-coontext.xsd">
  <context:annotation-config/>
  <bean id="..." class="...">     <!-- collaboretors and configuration for this bean go here -->   </bean>      </beans>

 Controll讲解:https://www.cnblogs.com/yaqee/p/11256047.html

依赖注入装配Bean 基于注解

    • 注解:就是一个类,使用@注解名称
    • 开发中:使用注解 取代 xml配置文件。
      1.@Component取代<bean class="">
      @Component("id") 取代 <bean id="" class="">
      2.web开发,提供3个@Component注解衍生注解(功能一样)取代
      @Repository :dao层
      @Service:service层
      @Controller:web层
      3.依赖注入,给私有字段设值,也可以给setter方法设值

      • 普通值:@Value(" ")
      • 引用值:
        方式1:按照【类型】注入
        @Autowired
        方式2:按照【名称】注入1
        @Autowired
        @Qualifier("名称")
        方式3:按照【名称】注入2
        @Resource("名称")

        4.生命周期
        初始化:@PostConstruct
        销毁:@PreDestroy

        5.作用域
        @Scope("prototype") 多例
原文地址:https://www.cnblogs.com/xuzhongyin/p/13647126.html