posts - 93,  comments - 2,  trackbacks - 0

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

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

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

          分開(kāi)解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。
          1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個(gè)注解就可以創(chuàng)建一個(gè)簡(jiǎn)單的spring配置類,可以用來(lái)替代相應(yīng)的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> 

           相當(dāng)于:

          @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的注解類標(biāo)識(shí)這個(gè)類可以使用Spring IoC容器作為bean定義的來(lái)源。@Bean注解告訴Spring,一個(gè)帶有@Bean的注解方法將返回一個(gè)對(duì)象,該對(duì)象應(yīng)該被注冊(cè)為在Spring應(yīng)用程序上下文中的bean。

          2、@EnableAutoConfiguration:能夠自動(dòng)配置spring的上下文,試圖猜測(cè)和配置你想要的bean類,通常會(huì)自動(dòng)根據(jù)你的類路徑和你的bean定義自動(dòng)配置。

          3、@ComponentScan:會(huì)自動(dòng)掃描指定包下的全部標(biāo)有@Component的類,并注冊(cè)成bean,當(dāng)然包括@Component下的子注解@Service,@Repository,@Controller。


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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2020年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 西乌珠穆沁旗| 阜平县| 海林市| 安宁市| 灌南县| 镇巴县| 丹凤县| 北辰区| 固阳县| 绥芬河市| 太康县| 镇巴县| 疏附县| 南充市| 新干县| 南通市| 乐安县| 张家川| 景泰县| 庄浪县| 碌曲县| 长丰县| 聂拉木县| 临江市| 通城县| 疏勒县| 昔阳县| 正安县| 太保市| 黑龙江省| 上蔡县| 白朗县| 宜黄县| 石嘴山市| 沧州市| 玉屏| 柯坪县| 军事| 辽阳市| 高邮市| 通江县|