posts - 93,  comments - 2,  trackbacks - 0

          之前用戶使用的是3個注解注解他們的main類。分別是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于這些注解一般都是一起使用,spring boot提供了一個統一的注解@SpringBootApplication。

          @SpringBootApplication = (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。

          @SpringBootApplication 
          public class ApplicationMain { 
              public static void main(String[] args) { 
                  SpringApplication.run(Application.class, args); 
              } 
          }

          分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
          1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個注解就可以創建一個簡單的spring配置類,可以用來替代相應的xml配置文件。

          <beans> 
              <bean id = "car" class="com.test.Car"> 
                  <property name="wheel" ref = "wheel"></property> 
              </bean> 
              <bean id = "wheel" class="com.test.Wheel"></bean> 
          </beans> 

           相當于:

          @Configuration 
          public class Conf { 
              @Bean 
              public Car car() { 
                  Car car = new Car(); 
                  car.setWheel(wheel()); 
                  return car; 
              } 
              @Bean  
              public Wheel wheel() { 
                  return new Wheel(); 
              } 
          }

          @Configuration的注解類標識這個類可以使用Spring IoC容器作為bean定義的來源。@Bean注解告訴Spring,一個帶有@Bean的注解方法將返回一個對象,該對象應該被注冊為在Spring應用程序上下文中的bean。

          2、@EnableAutoConfiguration:能夠自動配置spring的上下文,試圖猜測和配置你想要的bean類,通常會自動根據你的類路徑和你的bean定義自動配置。

          3、@ComponentScan:會自動掃描指定包下的全部標有@Component的類,并注冊成bean,當然包括@Component下的子注解@Service,@Repository,@Controller。


          posted on 2020-04-09 09:10 Terry Zou 閱讀(133) 評論(0)  編輯  收藏 所屬分類: SpringBoot

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2020年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 崇仁县| 阿鲁科尔沁旗| 乌拉特后旗| 锡林郭勒盟| 孝感市| 沭阳县| 东源县| 澎湖县| 衡山县| 正定县| 镇江市| 彭泽县| 临武县| 改则县| 南郑县| 宝坻区| 旬阳县| 察隅县| 东源县| 章丘市| 中卫市| 新和县| 泽库县| 茂名市| 彭山县| 勃利县| 桦南县| 武山县| 淅川县| 荆州市| 古浪县| 三明市| 奈曼旗| 页游| 宝兴县| 汉中市| 镶黄旗| 康定县| 松潘县| 贵德县| 陆良县|