Spring

时间:2017-06-12 22:22:20

标签: java xml spring spring-mvc

我已经在StackOverflow上阅读了大量关于ApplicationContextProvider主题的例子,但我找不到一个解决我的问题的例子。我有一个我不相信的xml文件正在读入并初始化我在其中记录的bean。我有以下课程:

package com.tacticalenterprisesltd.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextProvider implements ApplicationContextAware
{
    @Autowired
    private static ApplicationContext context;

    public ApplicationContext getApplicationContext()
    {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        System.out.println("ApplicationContext Initialized");
        context = ac;
    }       
}

我的applicationContext.xml文件:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <mvc:annotation-driven />
    <tx:annotation-driven />
    <context:component-scan base-package="com.tacticalenterprisesltd" />
    <bean id="applicationContextProvider" class="com.tacticalenterprisesltd.spring.ApplicationContextProvider" />
    <bean id="springDatabase" class="com.tacticalenterprisesltd.spring.SpringDatabase" />

</beans>

JedSpringTestApplication:

package com.tacticalenterprisesltd.jedspringtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@ComponentScan(basePackages = {"com.tacticalenterprisesltd"})
public class JedSpringTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(JedSpringTestApplication.class, args);

    }
}

最后,PersonelSearch类:

package com.tacticalenterprisesltd.jedspringtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.tacticalenterprisesltd.Field;
import com.tacticalenterprisesltd.Query;
import com.tacticalenterprisesltd.WhereCondition;
import com.tacticalenterprisesltd.spring.ApplicationContextProvider;
import com.tacticalenterprisesltd.spring.SpringDatabase;

@Controller
public class PersonnelSearch 
{
    @Autowired
    ApplicationContextProvider applicationContextProvider ;

    @RequestMapping("/search")
    public String search(@RequestParam(value="lastname", required=true) String lastname, Model model) {
        model.addAttribute("lastname", lastname);
        SpringDatabase sd = (SpringDatabase)applicationContextProvider .getApplicationContext().getBean("springDatabase", com.tacticalenterprisesltd.spring.SpringDatabase.class);
        Query query = new Query(Query.Type.SELECT, "employees");
        Field field1 = new Field("employees", "id", Field.Type.INT);
        Field field2 = new Field("employees", "first_name", Field.Type.STRING);
        Field field3 = new Field("employees", "last_name", Field.Type.INT);
        Field field4 = new Field("employees", "email", Field.Type.INT);
        Field[] fields = new Field[4];
        fields[0] = field1;
        fields[1] = field2;
        fields[2] = field3;
        fields[3] = field4;
        query.setFields(fields);
        WhereCondition where = new WhereCondition(field3, lastname, "=");
        WhereCondition[] conditions = new WhereCondition[1];
        conditions[0] = where;
        query.setWhereConditions(conditions);
        sd.setQuery(query);
        String[][] strData = sd.executeSelect();
        String out = "";
        for(int i = 0; i < strData.length; i++)
        {
            out += " | ";
            for(int j = 0; j < strData[0].length; j++)
            {
                out += strData[i][j] + " | ";
            }
            out += "\n";
        }
        System.out.println("Table Results:\n" + out);
        return out;
    }
}

当我作为Spring Boot应用程序运行时,我得到:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personnelSearch': Unsatisfied dependency expressed through field 'applicationContextProvider'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tacticalenterprisesltd.spring.ApplicationContextProvider' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
***************************
APPLICATION FAILED TO START
***************************

    Description:

Field applicationContextProvider in com.tacticalenterprisesltd.jedspringtest.PersonnelSearch required a bean of type 'com.tacticalenterprisesltd.spring.ApplicationContextProvider' that could not be found.


Action:

Consider defining a bean of type 'com.tacticalenterprisesltd.spring.ApplicationContextProvider' in your configuration.

我认为没有读入applicationContext.xml文件,否则当应用程序加载时,其中记录的bean将可用。请指教。

2 个答案:

答案 0 :(得分:0)

您没有@Autowired您的应用程序上下文。

你需要这样做:

@Component
public class ApplicationContextProvider
{
    @Autowired
    private ApplicationContext context;

    public ApplicationContext getApplicationContext()
    {
        return context;
    }

}

答案 1 :(得分:0)

acp类中将变量名称从applicationContextProvider更改为PersonnelSearch或在@Qualifier("applicationContextProvider") @Autowired之后添加PersonnelSearch acp变量。

e.g。

@Controller
public class PersonnelSearch 
{
    @Autowired
    ApplicationContextProvider applicationContextProvider;

@Controller
public class PersonnelSearch 
    {
        @Autowired
        @Qualifier("applicationContextProvider")
        ApplicationContextProvider acp;

另外,在@ComponentScan(basePackages = {"com.tacticalenterprisesltd"})上添加JedSpringTestApplication为:

package com.tacticalenterprisesltd.jedspringtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@ComponentScan(basePackages = {"com.tacticalenterprisesltd"})
public class JedSpringTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(JedSpringTestApplication.class, args);

    }
}