我在看這個例子:Building Applications in JavaFX 2.0他們顯示自定義SpringFxmlLoader: 爲什麼需要一個特定的春天FXML裝載機
import java.io.IOException;
import java.io.InputStream;
import javafx.fxml.FXMLLoader;
import org.springframework.context.ApplicationContext;
public class SpringFxmlLoader
{
private ApplicationContext context;
public SpringFxmlLoader(ApplicationContext context)
{
this.context = context;
}
public Object load(String url, Class<?> controllerClass) throws IOException
{
InputStream fxmlStream = null;
try
{
fxmlStream = controllerClass.getResourceAsStream(url);
Object instance = context.getBean(controllerClass);
FXMLLoader loader = new FXMLLoader();
loader.getNamespace().put("controller", instance);
return loader.load(fxmlStream);
}
finally
{
if (fxmlStream != null)
{
fxmlStream.close();
}
}
}
}`
爲什麼一個需要創建一個特定的春天FXML裝載機?我的意思是,即使使用簡單的fxml加載程序,當您加載如下所示的fxml時:
AnchorPane page = (AnchorPane) FXMLLoader.load(TabePaneGraph.class.getResource("Sample.fxml"));
示例控制器被調用,任何初始化仍然完成。我想了解這個特定的自定義SpringFxmlLoader實現背後的動機。