我有一个Spring-Boot-Application作为maven中的multimodule-Project.结构如下:
Parent-Project
|--MainApplication
|--Module1
|--ModuleN
Run Code Online (Sandbox Code Playgroud)
在MainApplication项目中有main()方法类注释用@SpringBootApplication等等.此项目一如既往地自动加载application.properties文件.所以我可以使用@Value注释访问值
@Value("${myapp.api-key}")
private String apiKey;
Run Code Online (Sandbox Code Playgroud)
在我的Module1中,我也想使用属性文件(称为module1.properties),其中存储了模块配置.只能在模块中访问和使用此文件.但是我无法加载它.我尝试过@Configuration,@PropertySource但没有运气.
@Configuration
@PropertySource(value = "classpath:module1.properties")
public class ConfigClass {
Run Code Online (Sandbox Code Playgroud)
如何使用Spring-Boot加载属性文件并轻松访问这些值?找不到有效的解决方案.
我的配置
@Configuration
@PropertySource(value = "classpath:tmdb.properties")
public class TMDbConfig {
@Value("${moviedb.tmdb.api-key}")
private String apiKey;
public String getApiKey() {
return apiKey;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Run Code Online (Sandbox Code Playgroud)
调用配置
@Component
public class TMDbWarper {
@Autowired
private TMDbConfig tmdbConfig;
private TmdbApi tmdbApi;
public TMDbWarper(){
tmdbApi = …Run Code Online (Sandbox Code Playgroud) 我创建了一个带有DockPanel和Button的自定义TabItem.
XAML:
<TabItem
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="MovieDB.UI.UserControlls.SearchTab" d:DesignWidth="500.038" d:DesignHeight="309.055">
<DockPanel Background="#FFE5E5E5">
<Button x:Name="button" Content="Button" Height="100" Width="75" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</DockPanel>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
C#:
namespace MovieDB.UI.UserControlls
{
/// <summary>
/// Interaktionslogik für SearchTab.xaml
/// </summary>
public partial class SearchTab : TabItem
{
private SearchContainer<SearchMovie> results;
public SearchTab()
{
InitializeComponent();
this.Header = "Suche";
}
public SearchTab(SearchContainer<SearchMovie> results):this()
{
this.updateSearch(results);
}
public void updateSearch(SearchContainer<SearchMovie> results)
{
clear();
if(results.TotalResults == 0)
{
}
else
{
this.results = results;
Debug.WriteLine("Results: " + results.Results.Count());
}
} …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的程序找不到我的自定义验证规则 f\xc3\xbcr 我的文本框。我定义验证规则所在的命名空间
\n\nxmlns:valRule="clr-namespace:MovieDB.UI.Validation" \nRun Code Online (Sandbox Code Playgroud)\n\n我将我的规则绑定到文本框
\n\n <TextBox x:Name="textBoxName" Height="23" Margin="59,13,10,0" TextWrapping="Wrap" VerticalAlignment="Top">\n <TextBox.Text>\n <Binding Path="archiveName">\n <Binding.ValidationRules>\n <valRule:ArchiveNameValidationRule />\n </Binding.ValidationRules>\n </Binding>\n </TextBox.Text>\n </TextBox>\nRun Code Online (Sandbox Code Playgroud)\n\n我在这里定义我的规则
\n\nnamespace MovieDB.UI.Validation\n{\n public class ArchiveNameValidationRule : ValidationRule\n {\n\n private static char[] FORBIDEN_CHARS = new char[] { \'*\', \'&\', \'#\', \'/\', \' \', \'\\\\\', \'+\', \'=\', \'?\', \')\', \'(\', \']\', \'[\', \'}\',\n \'{\', \'%\', \'$\', \'\xc2\xa7\', \'"\', \'!\', \'\xc3\xb6\', \'\xc3\xbc\' ,\'\xc3\xa4\', \'\xc3\x84\', \'\xc3\x96\', \'\xc3\x9c\', \':\', \'.\', \';\', \',\', };\n\n public override ValidationResult Validate(object …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 JAVA 控制台应用程序。用户启动 jar 后,他应该得到一个类似于 Linux shell 的命令行,他可以在其中输入命令。这是我以前从未编程过的东西。通常我是在写 GUI 或者我用一些被解析的参数来启动 jar。
基本上我有2个问题:
是否有提供“无限输入”的最佳实践?我找到的所有解决方案都有 10 年或更长时间,例如:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("Enter String");
String s = br.readLine();
}
Run Code Online (Sandbox Code Playgroud)
这仍然是最佳实践还是今天有更好的方法?
我很擅长使用C#进行线程处理.我有一个Searchfield,它会在按下的每个Key上触发针对API的HTTP-Request.在请求之后,Searchresult显示在我的SearchTab中的ListView中.当然,这会冻结UI几毫秒,直到HTTP-Request完成.所以我试着让Request Async.
private async void textBoxSearch_KeyUp(object sender, KeyEventArgs e)
{
SearchTab searchTab;
Task<SearchContainer<SearchMovie>> searchTask = searchMovie(textBoxSearch.Text);
if (searchTabExists())
{
searchTab = getSearchTab();
}
else
{
searchTab = new SearchTab();
mainTabControl.Items.Insert(mainTabControl.Items.Count, searchTab);
}
searchTab.IsSelected = true;
SearchContainer<SearchMovie> results = await searchTask;
searchTab.updateSearch(results);
}
async Task<SearchContainer<SearchMovie>> searchMovie(String query)
{
var today = await Task.FromResult<SearchContainer<SearchMovie>>(tmdbClient.SearchMovie(query, "de"));
return today;
}
Run Code Online (Sandbox Code Playgroud)
这段代码使它更好一些,但它仍然冻结了UI,因为在某些时候它必须等待API调用.我希望能够在不中断的情况下平滑地键入我的搜索,并在进行另一次搜索(KeyPressed)之前及时完成API调用时显示结果.
你如何在C#中解决这样的问题?在JAVA中,我将使用AsyncWorker,它在搜索完成后调用Display方法(或者如果进行另一次搜索则取消worker),这样就不会在Main-Thread中进行搜索和显示.
C#中是否有类似的结构?如果我搜索多线程,我只能找到异步等待解决方案.或者可以按照我希望的方式使用它.
c# ×3
wpf ×3
java ×2
async-await ×1
jline ×1
jline2 ×1
namespaces ×1
spring ×1
spring-boot ×1
validation ×1