使用配置類而不使用XML文件(代替bean.xml)對spring進行配置


以下類是一個配置類,它的作用和bean.xml是一樣的
注解:
  @Configuration
    作用:
       用於指定當前類是一個spring配置類,當創建容器時會從該類上加載注解。
      
 獲取容器時需要使用AnnotationApplicationContext(有@Configuration注解的類.class)。
       ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
    
屬性: value:用於指定配置類的字節碼
    細節:當配置類作為AnnotationConfigApplicationContext對象創建的參數時,該注解可以不寫。

  @ComponentScan
    作用: 用於通過注解指定Spring在創建容器時要掃描的包
     代替了:
    <!-- 告知spring在創建容器時要掃描的包 -->
    <context:component-scan base-package="com.itheima"></context:component-scan>
 
        
    屬性: basePackages value  兩者的作用是一樣的,都是用於指定創建容器時要掃描的包
       我們使用此注解就等同於在xml中配置了
       需要掃描多個包時, 使用{}(即數組的賦值方式)
    源碼:
  @Bean

    作用: 該注解只能寫在方法上,表明使用此方法創建一個對象,並且放入spring容器
    屬性: name 用於指定bean的id 默認值是當前方法的名稱
    @Bean(name="runner")
    @Scope("prototype")
      public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){
          return new QueryRunner(dataSource);
      }

      代替了:

    <!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
            <!--注入數據源-->
            <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

 

 
    @Bean(name="ds2")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

      代替了:

  <!-- 配置數據源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--連接數據庫的必備信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>

    細節: 當我們使用注解配置方法時, 如果方法有參數, spring框架回去容器中查找有沒有類型同參數匹配 的bean對象

       查找的方式同AutoWired注解的方式是一樣的

  @Import

    作用: 用於導入其他配置類,在引入其他配置類時,可以不用再寫@Configuration注解。當然,寫上也沒問題。

       屬性: value[]:用於指定其他配置類的字節碼。 

         當我們使用Import的注解之后,有Import注解的類就父配置類,而導入的都是子配置類

      @Configuration 
      @ComponentScan(basePackages = "com.itheima.spring")
      @Import({ JdbcConfig.class})
      public class SpringConfiguration { }
      @Configuration 
      @PropertySource("classpath:jdbc.properties")
      public class JdbcConfig{ }

  @PropertySource

  作用:用於加載.properties文件中的配置。
      例如我們配置數據源時,可以把連接數據庫的信息寫到properties配置文件中,
      就可以使用此注解指定properties配置文件的位置
   屬性: value[]:用於指定properties文件位置。如果是在類路徑下,需要寫上classpath:
  

      

    在JdbcConfig中使用SpEL通過key獲取properties文件中的相應的值     通過@Value注入

    

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM