org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'userDAO'的bean - 上下文未被传递

时间:2016-10-31 04:06:42

标签: java spring spring-mvc javabeans

以下是我的文件和我的目录:

弹簧Module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

<import resource="Spring-Datasource.xml" />
<import resource="Spring-User.xml" />

弹簧Datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="VALID_URL" />
    <property name="username" value="USER" />
    <property name="password" value="PASS" />
</bean>

弹簧User.xml

<beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="userDAO" class="hello.JdbcUserDAO">
    <property name="dataSource" ref="dataSource" />
</bean>

Application.java

package hello;
<import statements>
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("Spring-Module.xml");
    SpringApplication.run(Application.class, args);

    UserDAO userDAO = (UserDAO) context.getBean("userDAO");
    }
}

RegistrationController.java

package hello;
@RestController
public class RegistrationController implements ApplicationContextAware {

private static final String template = "Hello, %s!";
//    @Autowired <-- using this instead of interface doesn't work either
private static ApplicationContext context;
private final AtomicLong counter = new AtomicLong();

@RequestMapping(value = "/api/register", method = RequestMethod.POST)
public Response registerUser(@RequestParam Map<String,String> req) {


    User user = new User(counter.incrementAndGet(),
                        req.get("first"), req.get("last"), req.get("email"), 
                        req.get("age"), Double.parseDouble(req.get("height")), 
                        Double.parseDouble(req.get("weight")), Double.parseDouble(req.get("bmi"))
                        );

    UserDAO userDAO = (UserDAO) context.getBean("userDAO"); // <--- error here
    userDAO.insert(user);


    return new Response(counter.get(), "200");
}

public static ApplicationContext getApplicationContext() {
    return context;
}

@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
    this.context = ac;
    }

}

当我从Application类访问bean userDAO时,一切都按预期完美运行。问题是当我尝试使用上下文从我的路由类访问userDAO时,它抛出错误'bean userDAO not found'。因此,我怀疑必须在我的路线中再次创建上下文?

我对Spring相对较新,完全难过!我会尽力澄清你们有任何疑问。

编辑:添加目录

   src/main/java/hello
      /resources
                /Spring-User.xml
                /Spring-Module.xml
                /Spring-Datasource.xml

2 个答案:

答案 0 :(得分:0)

我找到了解决方案 - 感谢您的帮助。我不确定为什么这样做(或者更确切地说,为什么实现接口不起作用)。

RegistrationController.java

package hello;


@RestController
public class RegistrationController implements ApplicationContextAware {

@Autowired
private static ApplicationContext context;

@RequestMapping(value = "/api/register", method = RequestMethod.POST)
public Response registerUser(@RequestParam Map<String,String> req) {
    context = new ClassPathXmlApplicationContext("Spring-Module.xml"); // <--- added this line


    User user = new User(counter.incrementAndGet(),
                    req.get("first"), req.get("last"), req.get("email"), 
                    req.get("age"), Double.parseDouble(req.get("height")), 
                    Double.parseDouble(req.get("weight")), Double.parseDouble(req.get("bmi"))
                    );

    UserDAO userDAO = (UserDAO) context.getBean("userDAO"); 
    userDAO.insert(user);


    return new Response(counter.get(), "200");
}

public static ApplicationContext getApplicationContext() {
    return context;
}

@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
    this.context = ac;
    }

}

答案 1 :(得分:0)

实际上,您需要为带注释的组件设置类路径扫描并启用注释配置

<context:annotation-config/>
<context:component-scan base-package="com.example.dao, com.example.service, com.example.controller, etc..."/>

但2016年最好的建议是完全忘记xml配置并开始使用java配置。