1. 程式人生 > 其它 >2022/3/12 【暑期實習】美團筆試

2022/3/12 【暑期實習】美團筆試

3、Bean的自動裝配

  • spring自動尋找上下文,並自動裝配屬性

3.1spring中自動裝配的三種方式

  1. 在xml中顯示的配置
  2. 在java中顯示的配置
  3. 隱示自動裝配

3.2測試

3.2.1在xml中顯示的配置

  • byName 會自動從spring上下文中的去查詢,有沒有和自己屬性相同的class的小寫的id
    <bean id="dog" class="com.mhy.atowired.Dog"/>
    <bean id="cat" class="com.mhy.atowired.Cat"/>

<!--    手動裝配-->
<!--    <bean id="people" class="com.mhy.atowired.People">-->
<!--        <property name="name" value="水三丫"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->

    <bean id="people" class="com.mhy.atowired.People" autowire="byName">
        <property name="name" value="水三丫"/>
    </bean>

注意:如果配置的id的名字不是小寫的不一樣不能夠自動裝配

例如如這裡的id="dog",如果改成dog222就會裝配失敗

  • byType 會根據上下文配置的class型別去尋找,id名可以不是小寫,沒有id也可以的
    <bean id="dog222" class="com.mhy.atowired.Dog" p:dogName="小狗狗旺財"/>
    <bean id="cat222" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/>

<!--    手動裝配-->
<!--    <bean id="people" class="com.mhy.atowired.People">-->
<!--        <property name="name" value="水三丫"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->
    <bean id="people" class="com.mhy.atowired.People" autowire="byType">
        <property name="name" value="水三丫"/>
    </bean>

注意:但是不能夠配置多個一樣的class型別,否則會尋找失敗

例如這樣:

    <bean id="dog222" class="com.mhy.atowired.Dog"/>
    <bean id="cat222" class="com.mhy.atowired.Cat"/>
    <bean id="cat22" class="com.mhy.atowired.Cat"/>
    <bean id="cat2" class="com.mhy.atowired.Cat"/>

3.2.2使用註解的自動裝配

jdk1.5才支援的註解,spring2.5支援的註解

<?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/>

</beans>
  • 註解種類

    • @Autowired :可以在set方法使用!class類的屬性上使用!
        @Autowired
        private Cat cat;
    
        @Autowired
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
    • @Autowired(required = false) :表示該屬性可以為null
    • @Qualifier(value="cat222") :這個註解可以顯示的去定義名字,當有多個相同的class的bean的id不同配置時候可以避免精確的定位
    <bean id="cat222" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/>
    <bean id="cat22233" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/>
    
    @Autowired
    @Qualifier(value="cat222")
    private Cat cat;
    
    • @Resource(name="cat222"):是java自帶的註解不是spring的註解,它也可以完成自動裝配,它也可以加name屬性,可以精確定位到

4、使用註解開發

使用註解開發不是使用xml必須匯入spring-aop的jar包

配置環境

    <!--    掃描包-->
    <context:component-scan base-package="com.mhy.pojo2"/>
    <!--    使用註解開發-->
    <context:annotation-config/>
  • @Component(value = "user"):註解是bean的配置,這裡的value相當於id,相當於

    • Component有很多衍生註解,功能相同
    • @Repository:一般用於Dao層
    • @Service:一般用於Service層
    • @Controller:一般用Web,servlet
    <bean id="user2" class="com.mhy.pojo2.User2"/>
    
  • @Value:用於屬性的值,可以在屬性上,也可以在set方法上

    @Value("shuisanya")
    public void setName(String name) {
        this.name = name;
    }
    @Value("shuisanya")
    private String name;
<property name="name" value="shuisanya"/>

5、完全使用java的方式配置spring

  • @Configuration:配置到自己的定義的類上面,該類就相當於applicationContext.xml的配置檔案,相當於一個容器,獲取context物件的方法也要變AnnotationConfigApplicationContext

  • @Bean:就你需要註冊在spring的實體類物件,加在上面configuration註解的類的中作為方法返回你註冊的實體類

  • @ComponentScan("com.mhy.pojo3"):掃描包

  • @Import(SyyConfig2.class):匯入另一個@Configuration註解的配置容器

    @Configuration
    @ComponentScan("com.mhy.pojo3")//掃描包
    @Import(SyyConfig2.class)
    public class SsyConfig {
      @Bean
      public User3 getUser3(){
          return new User3();
      }
    

}

​```java
@Component
public class User3 {
  private String name;

  public String getName() {
      return name;
  }

  @Value("shuisanya")
  public void setName(String name) {
      this.name = name;
  }

  @Override
  public String toString() {
      return "User3{" +
              "name='" + name + '\'' +
              '}';
  }
}
@Test
public void user3Test(){
   ApplicationContext context = new AnnotationConfigApplicationContext(SsyConfig.class);

   User3 user3 = context.getBean("getUser3", User3.class);

   System.out.println(user3.getName());
}

注意:如果@Component(value = "user3") 加了value,並且加了@ComponentScan("com.mhy.pojo3")這個掃描包的註解,可以用user3來獲取;當然如果在@Configuration註解的類寫Bean註解用getUser3獲取也是可以的

@Component(value = "user3")
public class User3 
    
@Bean
public User3 getUser3(){
    return new User3();
}

User3 user3 = context.getBean("user3", User3.class);