Java 为什么赢了';我的变量不是自动连线的吗?

Java 为什么赢了';我的变量不是自动连线的吗?,java,spring,web,annotations,autowired,Java,Spring,Web,Annotations,Autowired,由于某些原因,我试图用@Autowired实例化的对象指针从未被实例化。我试着看了几个例子,但似乎没有任何效果!这是我的密码: Testing.java package com.example.core.service.integration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration;

由于某些原因,我试图用@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!");
}

}
@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!");
}

}
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>
<?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>
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>
<?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>

bean将只注入到spring管理的实例中。由于您正在使用
new
操作符创建
Testing
的实例,因此它不会注入
integrationRestService

你能做的就是

  • 从应用程序上下文中获取
    测试的实例

    ClassPathXmlApplicationContext context = 
        new ClassPathXmlApplicationContext("Spring.xml");
    Testing testing = (Testing) context.getBean(Testing.class);
    
  • 使用
    Testing
    类上的注释,这样使用
    new
    操作符创建的实例也将被spring管理。但这是一个相当麻烦的过程
试试这个:

@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仅适用于SpringBean

您的对象不是Springbean,因为您自己创建它

您没有任何代码来初始化applicationContext<代码>@ContextConfiguration
,AFAIK仅用于单元测试

Spring并不神奇,你需要在它工作之前调用它

如果使用main方法,则需要自己创建applicationContext,然后从中获取bean

ApplicationContext ctx = new ClasspathXmlApplicationContext("/app-context.xml");
Testing testing = (Testing) ctx.getBean("testing");
干得好

但是,使用

@Autowired
private Testing testing;

这就是失败

是的,这就是我申请的问题!谢谢你的反馈!谢谢,我以前不知道!