简体   繁体   English

为什么我的变量不能自动装配?

[英]Why won't my variable be autowired?

For some reason the object pointer I am trying to instantiate with @Autowired is never instantiated. 由于某种原因,我试图用@Autowired实例化的对象指针永远不会被实例化。 I've tried looking at several examples, but nothing seems to work! 我试过看几个例子,但似乎什么都没有用! Here is my code: 这是我的代码:

Testing.java 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 IntegrationTestService.java

public interface IntegrationRestService {

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

public  FindSomethingElse getFindSomethingElseResponse(String urlToRead);
}

IntegrationRestServiceImpl.java IntegrationRestServiceImpl.java

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


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

app-context.xml 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>

Any ideas what on I'm doing wrong? 我有什么想法我做错了吗?

Answer: 回答:

Testing.java 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 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>

Try this: 试试这个:

@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 works just with spring beans. @Autowired只适用于春豆。

Your object is not a Spring bean, since you're creating it yourself. 你的对象不是Spring bean,因为你自己创建它。

You don't have any code to initialize your applicationContext. 您没有任何代码来初始化applicationContext。 @ContextConfiguration , AFAIK, is only used for unit testing. @ContextConfiguration ,AFAIK,仅用于单元测试。

Spring is not magical, you need to invoke it before it works. Spring不是神奇的,你需要在它工作之前调用它。

If you use a main method, you need to create your applicationContext yourself, then get your bean from it. 如果使用main方法,则需要自己创建applicationContext,然后从中获取bean。

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

The beans will get injected only into spring-managed instances. bean只会被注入到spring管理的实例中。 Since you are creating an instance of Testing using the new operator, it won't get the integrationRestService injected. 由于您使用new运算符创建了Testing实例,因此不会注入integrationRestService

what you can do is 你能做的是

  • Get the instance of testing from the application context. 从应用程序上下文中获取testing实例。

     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); Testing testing = (Testing) context.getBean(Testing.class); 
  • Use the @Configurable annotation on Testing class so that instances created using new operator will also be spring managed. Testing类上使用@Configurable批注,以便使用new运算符创建的实例也将进行弹簧管理。 But this is quite a cumbersome process. 但这是一个非常繁琐的过程。
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
Testing testing = (Testing) context.getBean(Testing.class);

is well done! 干得好!

but,use 但是,使用

@Autowired
private Testing testing;

is fail. 失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM