日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

@SpringBootApplication的使用

 jasonbetter 2019-02-13

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

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

1
2
3
4
5
6
@SpringBootApplication 
public class ApplicationMain { 
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    
}

 

分開解釋@Configuration,@EnableAutoConfiguration,@ComponentScan。

1、@Configuration:提到@Configuration就要提到他的搭檔@Bean。使用這兩個(gè)注解就可以創(chuàng)建一個(gè)簡單的spring配置類,可以用來替代相應(yīng)的xml配置文件。

1
2
3
4
5
6
<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)于:

1
2
3
4
5
6
7
8
9
10
11
12
13
@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)識這個(gè)類可以使用Spring IoC容器作為bean定義的來源。@Bean注解告訴Spring,一個(gè)帶有@Bean的注解方法將返回一個(gè)對象,該對象應(yīng)該被注冊為在Spring應(yīng)用程序上下文中的bean。

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

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

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多