2016-02-29 98 views
0

我們啓動了一個具有gradle bootRun的Spring Boot應用程序,該應用程序觸發了x.Application類,該類有註釋@SpringBootApplication。啓動後,我們可以訪問x包中的REST服務,但無法訪問其他包中的REST服務。你怎樣才能相應地配置類路徑?Spring Boot REST控制器類路徑

+1

聽起來像組件掃描問題。建議將「Application」類放入「根」包中,這樣掃描將遞歸地遍歷子包。否則,您可以添加@ComponentScan來掃描其他包 –

回答

1

假設你x包是像com.example.mybootapp和你的主要Application.classx包內,那麼你需要在你的主Application.class方法或配置文件中添加此

@SpringBootApplication 
@ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"}) 

@SpringBootApplication本身包含@Configuration @EnableAutoConfiguration @ComponentScan註釋,所以@ComponentScan默認basePackge(即包掃描)到封裝的主要Application.class的,這就是爲什麼春季是無法檢測到你的其他@Controlers這是主要的包之外。

如果按上面建議的方式構建代碼(在根包中查找應用程序類),則可以不加任何參數地添加@ComponentScan。所有應用程序組件(@Component,@Service,@Repository,@Controller等)將自動註冊爲Spring Beans。

請參閱此document關於如何構建您的代碼。

+0

謝謝。 '@ComponentScan({「com.example.mybootapp」,「com.example.someother」,「one.more.pack」})'訣竅。 –

相關問題