独立 Java 应用程序,没有在 Spring 应用程序上下文中创建 bean? (IntelliJ, Java配置)
Standalone Java Application, no beans get created in the Spring Application Context? (IntelliJ, JavaConfig)
我是 Spring 的新手,所以我相信我可能忽略了一些简单的事情。我正在制作一个 独立 应用程序,使用 Spring 来帮助依赖注入和 AOP。
问题是当我 运行 应用程序时 none 的 bean 在 spring 应用程序上下文中可用。我要注入的 bean 的类型是:WatchKey。该应用程序似乎 运行 正常,但是当“watched”文件夹中发生事件时,我会收到 NullPointerException,指出 WatchKey 变量为空。
这些是我在配置文件中声明的 bean
@Configuration
public class FileManagerConfig
{
@Bean
public Path FTPPath ()
{
Path FTPDir = null;
try {
FTPDir = FileSystems.getDefault().getPath("PATH_TO_FOLDER");
} catch (Exception e) {
e.printStackTrace();
}
return FTPDir;
}
@Bean
public WatchService getWatcher ()
{
WatchService watcher = null;
try {
watcher = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return watcher;
}
@Bean
public WatchKey getKey(Path path, WatchService FTPWatcher)
{
WatchKey key = null;
try {
key = path.register(FTPWatcher, ENTRY_CREATE);
key = FTPWatcher.take();
} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
return key;
}
}
最后一个 bean 是我想在主应用程序中自动装配的那个。其他bean注入最后一个bean。
请注意我已经在 Project Structure > Facets 中告诉 IntelliJ 这个配置文件。
另请注意,当 getKey 为 运行 时,密钥不为空(我在 try 块中放置了一个 if 语句来检查)。
以下是需要WatchKey依赖的class:
public class FTPWatchZip extends Observable
{
private boolean run;
@Autowired
private WatchKey key;
public FTPWatchZip()
{
run = true;
watch(key);
}
@Autowired
private void watch(WatchKey key)
{
try {
while(run)
{
for (WatchEvent<?> event : key.pollEvents())
{
if (event.kind() == ENTRY_CREATE) {
System.out.println("New file detected in FTP Folder\nDownloading...");
TimeUnit.SECONDS.sleep(30);
System.out.println("Notify subscribers\n");
}
}
key.reset();
}
} catch(InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
我注意到 IntelliJ 告诉我(当我将鼠标悬停在@Autowired 上时):
必须在有效的 Spring bean (@Component|@Servuce|...) 中定义自动装配成员。
这表明我没有将必要的 bean 放入 Spring 应用程序上下文中。
为了完整起见,这是我的主 class,我尝试了不同版本的主来尝试解决这个问题,但目前是这样的:
public class DuckCreekMonitor
{
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(FileManagerConfig.class);
new FTPWatchZip();
}
}
当我 运行 程序将以下内容打印到控制台:
Jun 07, 2017 5:01:25 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef04b5: startup date [Wed Jun 07 17:01:25 BST 2017]; root of context hierarchy
然后它似乎在“监视”文件夹中的更改,每当它检测到该目录中的更改时,我都会收到 NullPointerException 和堆栈跟踪信息。
因此我不认为 bean 被自动连接到我的函数中。有人知道我可以尝试什么吗?
Spring 如何知道您的 FTPWatchZip class?你应该用@Component 或@Service 注释它,这就是 IntelliJ 告诉你的。或像为其他人一样将其制成豆子:
@Bean
public FTPWatchZip getFTPWatch ()
{
return new FTPWatchZip();
}
我是 Spring 的新手,所以我相信我可能忽略了一些简单的事情。我正在制作一个 独立 应用程序,使用 Spring 来帮助依赖注入和 AOP。
问题是当我 运行 应用程序时 none 的 bean 在 spring 应用程序上下文中可用。我要注入的 bean 的类型是:WatchKey。该应用程序似乎 运行 正常,但是当“watched”文件夹中发生事件时,我会收到 NullPointerException,指出 WatchKey 变量为空。
这些是我在配置文件中声明的 bean
@Configuration
public class FileManagerConfig
{
@Bean
public Path FTPPath ()
{
Path FTPDir = null;
try {
FTPDir = FileSystems.getDefault().getPath("PATH_TO_FOLDER");
} catch (Exception e) {
e.printStackTrace();
}
return FTPDir;
}
@Bean
public WatchService getWatcher ()
{
WatchService watcher = null;
try {
watcher = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return watcher;
}
@Bean
public WatchKey getKey(Path path, WatchService FTPWatcher)
{
WatchKey key = null;
try {
key = path.register(FTPWatcher, ENTRY_CREATE);
key = FTPWatcher.take();
} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
return key;
}
}
最后一个 bean 是我想在主应用程序中自动装配的那个。其他bean注入最后一个bean。
请注意我已经在 Project Structure > Facets 中告诉 IntelliJ 这个配置文件。
另请注意,当 getKey 为 运行 时,密钥不为空(我在 try 块中放置了一个 if 语句来检查)。
以下是需要WatchKey依赖的class:
public class FTPWatchZip extends Observable
{
private boolean run;
@Autowired
private WatchKey key;
public FTPWatchZip()
{
run = true;
watch(key);
}
@Autowired
private void watch(WatchKey key)
{
try {
while(run)
{
for (WatchEvent<?> event : key.pollEvents())
{
if (event.kind() == ENTRY_CREATE) {
System.out.println("New file detected in FTP Folder\nDownloading...");
TimeUnit.SECONDS.sleep(30);
System.out.println("Notify subscribers\n");
}
}
key.reset();
}
} catch(InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
我注意到 IntelliJ 告诉我(当我将鼠标悬停在@Autowired 上时):
必须在有效的 Spring bean (@Component|@Servuce|...) 中定义自动装配成员。
这表明我没有将必要的 bean 放入 Spring 应用程序上下文中。
为了完整起见,这是我的主 class,我尝试了不同版本的主来尝试解决这个问题,但目前是这样的:
public class DuckCreekMonitor
{
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(FileManagerConfig.class);
new FTPWatchZip();
}
}
当我 运行 程序将以下内容打印到控制台:
Jun 07, 2017 5:01:25 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef04b5: startup date [Wed Jun 07 17:01:25 BST 2017]; root of context hierarchy
然后它似乎在“监视”文件夹中的更改,每当它检测到该目录中的更改时,我都会收到 NullPointerException 和堆栈跟踪信息。
因此我不认为 bean 被自动连接到我的函数中。有人知道我可以尝试什么吗?
Spring 如何知道您的 FTPWatchZip class?你应该用@Component 或@Service 注释它,这就是 IntelliJ 告诉你的。或像为其他人一样将其制成豆子:
@Bean
public FTPWatchZip getFTPWatch ()
{
return new FTPWatchZip();
}