欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學(xué)堂Java培訓(xùn)之Spring創(chuàng)建Bean的6種方式詳解

2019-07-02 14:43:43 1882瀏覽

今天千鋒扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于Spring創(chuàng)建Bean的6種方式詳解,首先在Spring應(yīng)用中創(chuàng)建Bean的多種方式,包括自動(dòng)創(chuàng)建,以及手動(dòng)創(chuàng)建注入方式,實(shí)際開(kāi)發(fā)中可以根據(jù)業(yè)務(wù)場(chǎng)景選擇合適的方案,下面我們一起來(lái)看一下吧。



方式1:

使用SpringXML方式配置,該方式用于在純Spring應(yīng)用中,適用于簡(jiǎn)單的小應(yīng)用,當(dāng)應(yīng)用變得復(fù)雜,將會(huì)導(dǎo)致XMl配置文件膨脹,不利于對(duì)象管理。

<bean id="xxxx" class="xxxx.xxxx"/>

方式2:

使用@Component,@Service,@Controler,@Repository注解

這幾個(gè)注解都是同樣的功能,被注解的類將會(huì)被Spring容器創(chuàng)建單例對(duì)象。

@Component:側(cè)重于通用的Bean類

@Service:標(biāo)識(shí)該類用于業(yè)務(wù)邏輯

@Controler:標(biāo)識(shí)該類為SpringMVC的控制器類

@Repository:標(biāo)識(shí)該類是一個(gè)實(shí)體類,只有屬性和Setter,Getter

@Component
public class User{
}

當(dāng)用于SpringBoot應(yīng)用時(shí),被注解的類必須在啟動(dòng)類的根路徑或者子路徑下,否則不會(huì)生效。

如果不在,可以使用@ComponentScan標(biāo)注掃描的路徑。

springxml也有相關(guān)的標(biāo)簽<component-scan/>

@ComponentScan(value={"com.microblog.blog","com.microblog.common"})
public class MicroblogBlogApplication {
public static void main(String args[]){
SpringApplication.run(MicroblogBlogApplication.class,args);
}
}

方式3:

使用@Bean注解,這種方式用在SpringBoot應(yīng)用中。

@Configuration標(biāo)識(shí)這是一個(gè)SpringBoot配置類,其將會(huì)掃描該類中是否存在@Bean注解的方法,比如如下代碼,將會(huì)創(chuàng)建User對(duì)象并放入容器中。

@ConditionalOnBean用于判斷存在某個(gè)Bean時(shí)才會(huì)創(chuàng)建UserBean.

這里創(chuàng)建的Bean名稱默認(rèn)為方法的名稱user。也可以@Bean("xxxx")定義。

@Configuration
public class UserConfiguration{
@Bean
@ConditionalOnBean(Location.class)
public User user(){
return new User();
}
}

Springboot還為我們提供了更多類似的注解。



也和方式2一樣,也會(huì)存在掃描路徑的問(wèn)題,除了以上的解決方式,還有使用Springbootstarter的解決方式

在resources下創(chuàng)建如下文件。META-INF/spring.factories.

SpringBoot在啟動(dòng)的時(shí)候?qū)?huì)掃描該文件,從何獲取到配置類UserConfiguration。



spring.factories.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.log.config.UserConfiguration

如果不成功,請(qǐng)引入該依賴

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

這個(gè)方式也是創(chuàng)建SpringBoot-starter的方式。

方式4:

使用注解@Import,也會(huì)創(chuàng)建對(duì)象并注入容器中

@Import(User.class)
public class MicroblogUserWebApplication {
public static void main(String args[]) {
SpringApplication.run(MicroblogUserWebApplication.class, args);
}
}

方式5:

使用ImportSelector或者ImportBeanDefinitionRegistrar接口,配合@Import實(shí)現(xiàn)。

在使用一些SpringBoot第三方組件時(shí),經(jīng)常會(huì)看到@EnableXXX來(lái)使能相關(guān)的服務(wù),這里以一個(gè)例子來(lái)實(shí)現(xiàn)。

創(chuàng)建測(cè)試類

@Slf4j
public class House {
public void run(){
log.info("House run ....");
}
}
@Slf4j
public class User {
public void run(){
log.info("User run ....");
}
}
@Slf4j
public class Student {
public void run(){
log.info("Student run ....");
}
}

實(shí)現(xiàn)ImportSelector接口

selectImports方法的返回值為需要?jiǎng)?chuàng)建Bean的類名稱。這里創(chuàng)建User類。

@Slf4j
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
log.info("MyImportSelector selectImports ...");
return new String[]{
User.class.getName()};
}
}

實(shí)現(xiàn)ImportBeanDefinitionRegistrar接口

beanDefinitionRegistry.registerBeanDefinition用于設(shè)置需要?jiǎng)?chuàng)建Bean的類名稱。這里創(chuàng)建House類。

@Slf4j
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
log.info("MyImportBeanDefinitionRegistrar registerBeanDefinitions .....");
BeanDefinition beanDefinition = new RootBeanDefinition(House.class.getName());
beanDefinitionRegistry.registerBeanDefinition(House.class.getName(),beanDefinition);
}
}

創(chuàng)建一個(gè)配置類

這里創(chuàng)建Student類。

@Configuration
public class ImportAutoconfiguration {
@Bean
public Student student(){
return new Student();
}
}

創(chuàng)建EnableImportSelector注解

EnableImportSelector注解上使用@Import,引入以上的三個(gè)類。

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import({MyImportSelector.class,ImportAutoconfiguration.class,MyImportBeanDefinitionRegistrar.class})
public @interface EnableImportSelector {
String value();
}

測(cè)試

@EnableImportSelector(value = "xxx")
@SpringBootApplication
public class ImportDemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ImportDemoApplication.class, args);
User user = context.getBean(User.class);
user.run();
Student student = context.getBean(Student.class);
student.run();
House house = context.getBean(House.class);
house.run();
}
}

輸出,可以看到,三個(gè)類UserStudentHouse都創(chuàng)建成功,都可從Spring容器中獲取到。

2019-06-20 17:53:39.528 INFO 27255 --- [ main] com.springboot.importselector.pojo.User : User run ....
2019-06-20 17:53:39.530 INFO 27255 --- [ main] c.s.importselector.pojo.Student : Student run ....
2019-06-20 17:53:39.531 INFO 27255 --- [ main] c.springboot.importselector.pojo.House : House run ....

方式6

手動(dòng)注入Bean容器,有些場(chǎng)景下需要代碼動(dòng)態(tài)注入,以上方式都不適用。這時(shí)就需要?jiǎng)?chuàng)建對(duì)象手動(dòng)注入。

通過(guò)DefaultListableBeanFactory注入。

registerSingleton(StringbeanName,Objectobject);

這里手動(dòng)使用new創(chuàng)建了一個(gè)Location對(duì)象。并注入容器中。

@Component
public class LocationRegister implements BeanFactoryAware {
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory)beanFactory;
Location location = new Location();
listableBeanFactory.registerSingleton("location1",location);
}
}

這種方式的應(yīng)用場(chǎng)景是為接口創(chuàng)建動(dòng)態(tài)代理對(duì)象,并向SPRING容器注冊(cè)。

比如MyBatis中的Mapper接口,Mapper沒(méi)有實(shí)現(xiàn)類,啟動(dòng)時(shí)創(chuàng)建動(dòng)態(tài)代理對(duì)象,將該對(duì)象注冊(cè)到容器中,使用時(shí)只要@Autowired注入即可使用,調(diào)用接口方法將會(huì)被代理攔截,進(jìn)而調(diào)用相關(guān)的SqlSession執(zhí)行相關(guān)的SQL業(yè)務(wù)邏輯。

可以看以下它的繼承體系

DefaultListableBeanFactory是ConfigurableListableBeanFactory的實(shí)現(xiàn)類。是對(duì)BeanFactory功能的擴(kuò)展。



測(cè)試代碼和以上一樣

Location location = context.getBean(Location.class);
location.run();

以上就是關(guān)于千鋒扣丁學(xué)堂Java培訓(xùn)之Spring創(chuàng)建Bean的6種方式詳解的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,想要了解更多關(guān)于Java開(kāi)發(fā)方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Java培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的Java講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的Java視頻教程定能讓你快速掌握J(rèn)ava從入門(mén)到精通開(kāi)發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Java技術(shù)交流群:850353792。


                        JavaEE/微服務(wù)/源碼解析/分布式/企業(yè)級(jí)架構(gòu)【VIP體驗(yàn)課】


     【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】       【掃碼進(jìn)入JavaEE/微服務(wù)VIP免費(fèi)公開(kāi)課】  



查看更多關(guān)于“Java開(kāi)發(fā)資訊”的相關(guān)文章>>

標(biāo)簽: Java培訓(xùn) Java視頻教程 Java多線程 Java面試題 Java學(xué)習(xí)視頻 springBoot項(xiàng)目

熱門(mén)專區(qū)

暫無(wú)熱門(mén)資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部