spring 基礎(2.5) 註解說明

1註解與xml的相應說明

  @EnableConfigurationProperties: 配置@ConfigurationProperties使用,在@EnableConfigurationProperties寫入class名稱,然後class中寫入@ConfigurationProperties

  @SpringBootApplication:springboot的核心註解,目的是開啓自動配置
  @PropertySource:指定加載配置文件
  @Configuration: 等價於<Beans></Beans>。用於定義配置類,可替換xml配置文件,被註解的類內部包含有一個或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。
  @Bean:等價於<Bean></Bean>
  @ComponentScan:指定範圍 等價於<context:component-scan base-package=”com.item.demo”/>
  @ConfigurationProperties: 主要用來把properties配置文件轉化爲bean(類)來使用的

   1.1@Configuration  @Bean beans 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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">
  <bean id="student" class="domain.Student">
         <property name="id" value="11"/>
         <property name="age" value="22"/>
         <property name="name" value="jack"/>
     </bean>
     //掃描 com.item.demo包下所有的 帶註解的文件
     <context:component-scan base-package=”com.item.demo”/>
</beans>


1.2需要導入的依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>     


2四類主鍵類型註解

  @Component:組件註解,通用註解,被該註解描述的類將被loC容器管理並實例化,以下三個都是主鍵註解的細化,當不確當用哪一個時候,就可以用這個。
  @Controller:語義註解,說明當前類是MVC應用中的控制器類。
  @Service:語義註解,說明當前類是Service業務服務類。
  @Repository:語義註解,說明當前類用於業務持久層通常描述對應Dao類。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章