为什么我的变量不能自动接线? - java

由于某种原因,我尝试使用@Autowired实例化的对象指针从未实例化。我尝试查看几个示例,但似乎没有任何效果!这是我的代码:

Testing.java

package com.example.core.service.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations={"/app-context.xml"})
public class Testing {

@Autowired
private IntegrationRestService integrationRestService;

public static void main(String args[])  {
    Testing t = new Testing();
    t.checkNull();
}

private void checkNull() {
    if(integrationRestService == null) System.err.println("FAIL...");
    else System.out.println("SUCCESS!");
}

}

IntegrationTestService.java

public interface IntegrationRestService {

public  FindSomething getFindSomethingResponse(String a, int b, int c);

public  FindSomethingElse getFindSomethingElseResponse(String urlToRead);
}

IntegrationRestServiceImpl.java

@Service
@Path("/test")
public class IntegrationRestServiceImpl implements IntegrationRestService {


    public IntegrationRestServiceImpl() {
        super();
     }
   ...
}

app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="constructor"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<context:annotation-config/>

<context:component-scan base-package="com.example.core"/>


<bean id="IntegrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" />
<bean id="Testing" class="com.example.core.service.integration.Testing" />

</beans>

有什么想法我做错了吗?

回答:

Testing.java

@Service
public class Testing {

@Autowired
private IntegrationRestService integrationRestService;

public static void main(String args[])  {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
    Testing testing = (Testing) context.getBean(Testing.class);
    testing.checkNull();
}


private void checkNull() {
    if(integrationRestService == null) System.err.println("FAIL...");
    else System.out.println("SUCCESS!");
}

}

app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
default-autowire="constructor"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<context:annotation-config/>

<context:component-scan base-package="com.example.core"/>

<bean id="testing" class="com.example.core.service.integration.Testing"/>

<bean id="integrationRestService" class="com.example.core.service.integration.IntegrationRestServiceImpl" />

</beans>

参考方案

试试这个:

@Service
public class Testing {

@Autowired
private IntegrationRestService integrationRestService;

public static void main(String args[])  {
    final AbstractApplicationContext context = new ClassPathXmlApplicationContext("/app-context.xml");
    Testing t = context.getBean(Testing.class);
    t.checkNull();
}

private void checkNull() {
    if(integrationRestService == null) System.err.println("FAIL...");
    else System.out.println("SUCCESS!");
}

}

@Autowired仅适用于 Spring Bean 。

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

Java Scanner读取文件的奇怪行为 - java

因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…

Java Globbing模式以匹配目录和文件 - java

我正在使用递归函数遍历根目录下的文件。我只想提取*.txt文件,但不想排除目录。现在,我的代码如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是这样做将不会匹配任何目录,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…

直接读取Zip文件中的文件-Java - java

我的情况是我有一个包含一些文件(txt,png,...)的zip文件,我想直接按它们的名称读取它,我已经测试了以下代码,但没有结果(NullPointerExcepion):InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); Buff…

Java RegEx中的单词边界\ b - java

我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…