2017-02-23 79 views
1

您可以將您的Stormpath組對象(即ADMIN)用作Spring Boot應用程序中的類嗎?我的意思是,我想保留這個對象的一些狀態,我不想保留一個記者POJO,它將有ADMIN的電子郵件和密碼,以及另一個數據,如性別/生日,但只是將整個狀態和行爲stormpath組ADMIN的實例。使用stormpath組作爲彈簧引導對象

這可以實現嗎?

回答

2

當你說使用Stormpath組對象'作爲類'時,我假設你的意思是'作爲一個Spring bean'?如果是這樣,那麼絕對是!

例如:

import com.stormpath.sdk.client.Client; 
import com.stormpath.sdk.group.Group; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Bean; 

// ... 

@Autowired 
private Client client; 

//I just made this property up - use any property name you want and 
//define it in your Spring config and ensure the value is the 
//fully qualified Stormpath href corresponding to your admin group: 
@Value("${groups.admin.href}") 
private String adminGroupHref; 

@Bean 
public Group adminGroup() { 
    return client.getResource(adminGroupHref, Group.class); 
} 

然後,在你的Spring配置的其他地方,你可以使用adminGroup()引用組(在保存Java配置類文件)。或者,如果你想要引用本集團在其他Java配置類或@Component豆豆,只是它的自動裝配:

@Autowired 
private Group adminGroup; 

但是,如果你在你的Spring Java配置中定義多個Group豆(也許?

@Autowired 
@Qualifier("adminGroup") 
private Group adminGroup; 

所有Stormpath資源實例(AccountGroupApplication,E:其他羣體一樣usersemployees),那麼你就會自動裝配時要確保正確Group實例注入需要一個@Qualifier tc)是線程安全的,如果需要,可以安全地用作單例。