Spring:Autowired bean为null

时间:2015-07-08 09:21:53

标签: java spring spring-mvc nullpointerexception autowired

我正在开发一个Spring-MVC应用程序,我在其中自动装配一个字段,但它被设置为NULL。我在网上检查了同样问题的解决方案,正如他们所描述的那样,我不应该在工作时创建一个新的类实例,而是从spring开始。我也试过了,但它总是抱怨找不到资源。

这是班级:

@Service
@Transactional
public class GoogleAuthorization{

    @Autowired
    private DriveQuickstart driveQuickstart;  // This guy is null

    public void saveCredentials(final String authCode) throws IOException {

        GoogleTokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(CALLBACK_URI).execute();
        Credential credential = flow.createAndStoreCredential(response, null);
        System.out.println(" Credential access token is "+credential.getAccessToken());
        System.out.println("Credential refresh token is "+credential.getRefreshToken());
// It fails at below point.
        this.driveQuickstart.storeCredentials(credential);
    }

控制器代码:

@RequestMapping(value = "/getgooglelogin")
    public String getGoogleLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session,Model model) {

       /* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("../WEB-INF/spring/root-context.xml");
        GoogleAuthorization helper = (GoogleAuthorization) applicationContext.getBean("helper");*/
        GoogleAuthorization helper = new GoogleAuthorization();
}

我尝试通过root-context.xml获取它,如上所示,但无论我放置什么路径,它都找不到它。

DriveQuickStartImpl:

@Service
@Transactional
public class DriveQuickstartImpl implements DriveQuickstart{
// All these below guys work just fine. 
@Autowired
    private PersonService personService;

    @Autowired
    private GoogleDriveService googleDriveService;

    @Autowired
    private GroupAttachmentsService groupAttachmentsService;

    @Autowired
    private GroupAccountService groupAccountService;
}

我做错了什么。你能帮忙的话,我会很高兴。非常感谢。

更新

root-context.xml所属的图片:

enter image description here

3 个答案:

答案 0 :(得分:1)

问题出在这里:

 GoogleAuthorization helper = new GoogleAuthorization();

当您使用GoogleAuthorization创建new时,Spring容器不会知道我们可能认为是一个Spring bean的类,因此它无法{{1}它。确保完成Autowire的类是一个spring bean。尝试使用FileSystemXmlApplicationContext @Autowiring实例化为:

GoogleAuthorization

答案 1 :(得分:1)

只需自动挂载GoogleAuthorization帮助程序(您可以执行此操作,因为您已经在Spring管理的控制器中):

@Service
@Transactional
public class DriveQuickstartImpl implements DriveQuickstart{

    // Other stuff...

    @Autowired 
    private GoogleAuthorization helper;


    @RequestMapping(value = "/getgooglelogin")
public String getGoogleLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session,Model model) {

       // use "helper"
}

如果您自动装配帮助程序,它将由Spring注入和管理。所以Spring将正确构建并自动装配它。

答案 2 :(得分:1)

如果您自己创建一个'new'的实例,则根本就没有弹簧布线。

如果您在Web-App上下文中使用Spring,则可以使用ContextLoaderListener初始化Spring-Context。

    <!-- inside WEB-INF/web.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- path to your Spring context configuration-->
        <!-- for example -->
        <param-value>/WEB-INF/spring-webapp-config.xml</param-value>
    </context-param>

然后您也可以在控制器中注入GoogleAuthorization Bean,因此无需掌握应用程序上下文来执行“查找”操作。 ...如果你使用弹簧试着让弹簧接线而不是依赖于查找&#39;。