04_Spring基於註解的IoC配置

Spring 基於註解的IoC配置

1. 常用的IoC註解按作用分類

<!-- 非註解的bml配置 -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
      scope=""  init-method="" destroy-method="">
    <property name=""  value="" | ref=""></property>
</bean>

1.1 用於創建對象的

作用就和在XML配置文件中編寫一個<bean>標籤實現的功能是一樣的

  • @Component:

    • 作用:用於把當前類對象存入spring容器中
    • 屬性value:用於指定bean的id。當我們不寫時,它的默認值是當前類名,且首字母改小寫。

以下三個註解的作用和屬性與Component是一模一樣。spring框架提供明確的三層使用的註解,使用三層對象更加清晰

  • @Controller:一般用在表現層
  • @Service:一般用在業務層
  • @Repository:一般用在持久層

1.2 用於注入數據的

作用就和在xml配置文件中的bean標籤中寫一個<property>標籤的作用是一樣的

位置:可以是變量上,也可以是方法上
細節:在使用註解注入時,set方法就不是必須的了。

  • @Autowired:

    • 作用:自動按照類型注入。只要容器中有唯一的一個bean對象類型和要注入的變量類型匹配,就可以注入成功; 如果ioc容器中沒有任何bean的類型和要注入的變量類型匹配,則報錯。
  • @Qualifier:

    • 如果Ioc容器中有多個類型匹配時,需要使用@Qualifier註解:
    • 作用:在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用,必須和@Autowired註解一起使用。但是在給方法參數注入時可以。
    • 屬性:
      • value:用於指定注入bean的id。
  • @Resource

    • 作用:直接按照bean的id注入。它可以獨立使用
    • 屬性:
      • name:用於指定bean的id。

注意:以上三個注入都只能注入其他bean類型的數據,而基本類型和String類型無法使用上述註解實現。另外,集合類型的注入只能通過XML來實現。

  • @Value

    • 作用:用於注入基本類型和String類型的數據
    • value:用於指定數據的值。它可以使用spring中SpEL(也就是spring的el表達式); SpEL的寫法:${表達式}

1.3 用於改變作用範圍的

作用就和在bean標籤中使用scope屬性實現的功能是一樣的

  • @Scope:
    • 作用:用於指定bean的作用範圍
    • value:指定範圍的取值。常用取值:singleton prototype

1.4 和生命週期相關

(瞭解)作用就和在bean標籤中使用init-method和destroy-methode的作用是一樣的

  • @PreDestroy 用於指定銷燬方法
  • @PostConstruct 用於指定初始化方法

2. 配置註解

實例代碼

@Configuration
@ComponentScan(basePackages = "com.lwq")
@Import(JdbcConfiguration.class)
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {

}

public class JdbcConfiguration {
    @Bean(value = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }

    @Bean(value = "dataSource")
    public DataSource createDatasource() {
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?useSSL=false");
            ds.setUser("root");
            ds.setPassword("root");
            return ds;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}

示例代碼中的SpringConfiguration類,是一個配置類,它的作用和applicationContext.xml是一樣的,其中的註解:

  1. @Configuration
    • 作用: 指定當前類是一個配置類
    • 注意: 當配置類作爲AnnotationConfigApplicationContext對象創建的參數時,該註解可以不寫。
  2. @CompontScan
    • 作用: 通過註解指定spring在創建容器時要掃描的包
    • 屬性: valuebasePackages的作用是一樣的,都是用於指定創建容器時要掃描的包。
  3. @Bean
    • 作用: 用於把當前方法的返回值作爲bean對象存入spring的ioc容器中
    • 注意: 當我們使用註解配置方法時,如果方法有參數,spring框架會去容器中查找有沒有可用的bean對象。查找的方式和Autowired註解的作用是一樣的
  4. @Import
    • 作用: 用於導入其他的配置類
    • 屬性: value 用於指定其他配置類的字節碼。當使用@Import的註解之後,有Import註解的類就父配置類,而導入的都是子配置類

使用jdbc.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy?useSSL=false
jdbc.user=root
jdbc.password=root
@Configuration
@ComponentScan(basePackages = "com.lwq")
@Import(JdbcConfiguration.class)
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {
}

@Configuration
public class JdbcConfiguration {

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.user}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    @Bean(value = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }

    @Bean(value = "dataSource")
    public DataSource createDatasource() {
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(user);
            ds.setPassword(password);
            return ds;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}
  1. @PropertySource
    • 作用:用戶指定properties文件的位置
    • value:指定文件的名稱和路徑

3. Spring整合junit

  1. 導入Spring整合junit的jar包

    <!-- pom.xml -->
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>4.3.13.RELEASE</version>
     </dependency>
    
  2. 使用Junit提供的@Runwith註解

  3. 使用@ContextConfiguration註解,告知spring的運行器,spring和ioc創建是基於xml還是註解的,並且說明位置

  4. 當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class AccountServiceTest {
    
        @Autowired
        private AccountService accountService;
    
        @Test
        public void testFindAll() {
    
            List<Account> accounts = accountService.findAllAccount();
    
            for (Account account : accounts) {
                System.out.println(account);
            }
        }
    
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章