无法让Spring注入我的依赖 [英] Can't get Spring to inject my dependencies

查看:66
本文介绍了无法让Spring注入我的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让Spring在我的应用程序中注入一个@Autowired依赖项,但无济于事。我在做什么错?

I've been trying to get Spring to inject an @Autowired dependency into my application without avail. What am I doing wrong?

我创建了一个名为TimeService的bean。

I created a bean called TimeService. Its job is to return the current time to anyone that asks.

package com.foxbomb.springtest.domain.time;

import java.util.Date;
import org.springframework.stereotype.Service;

@Service
public class TimeService {

    public TimeService() {
        super();
        System.out.println("Instantiating TimeService...");
    }

    public Date getTime() {
        return new Date();
    }
}

当然,我需要告诉Spring,因此我在web.xml中添加了以下内容:

Of course, I need to tell Spring about this, so I added the following to web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>  

很好,还有一些Spring配置:

Great, and some Spring configuration:

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

    <context:annotation-config base-package="com.foxbomb.springtest.domain" />

</beans>

现在,我们需要的是一个希望使用此依赖项的调用类。不幸的是,这里的@Autowired似乎什么也没做:

Now all we need is a calling class that would like to use this dependency. Unfortunately, the @Autowired here seems to do nothing:

package com.foxbomb.springtest;

import...

@Configurable
public class TimeManager {

    public TimeManager() {
        super();
        System.out.println("Instantiating TimeManager...");
    }

    @Autowired
    private TimeService timeService;

    public Date getTime() {
        return timeService.getTime();
    }

}

最后,想要一个JSP显示时间:

And lastly, a JSP that wants to display the time:

<%@page import="com.foxbomb.springtest.ApplicationContextImpl"%>
<%@page import="com.foxbomb.springtest.TimeManager"%>

<html>
    <head>
        <title>Spring Test</title>
    </head>
    <body>
        <h1>Autowired Dependencies....</h1>
        <p>
            Time is now <%=new TimeManager().getTime()%>!
        </p>

    </body>
</html>

但是我得到的只是:

java.lang.NullPointerException
    com.foxbomb.springtest.TimeManager.getTime(TimeManager.java:26)


推荐答案

最后!

我在上面的代码中使用了它例。诀窍是设置Aspect / J和Weaving。这允许@Configurable带注释的类自动实现其对Spring的依赖以注入依赖项。

I got it working with the code in the above example. The trick was to set up Aspect/J and Weaving. This allows the @Configurable annotated class to automatically realize that its dependent on Spring to inject dependencies.

基本包没有多大问题:)我不小心使一个xml错字,尝试将base-package属性添加到context:annotation-config标记。正确的定义是:

There wasn't much wrong with the base package :) I accidentally made a xml typo, trying to add the base-package attribute to the context:annotation-config tag. The correct definition was:

<context:annotation-config/>
<context:component-scan base-package="com.foxbomb.springtest"/>

我实际上最终需要将其更改为com.foxbomb.springtext,以包括尝试使用的类

I need actually eventually need to change it to com.foxbomb.springtext to include the class that was trying to access the bean too - as in the end it's also annotated.

感谢大家的帮助!

这篇关于无法让Spring注入我的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆