如何使用xml配置将服务bean传递到spring引导控制器

时间:2017-07-25 01:24:22

标签: java xml spring spring-mvc spring-boot

我对注释感到不舒服。在我的春季启动应用程序中,我正在通过应用程序上下文检索控制器中的服务bean。(我已跳过命名约定)

Say s1 = (Say) applicationContext.getBean("s1");

似乎我的应用程序与spring紧密耦合,它正在制作样板代码。那么有没有办法通过xml配置将服务bean自动装入控制器?

我尝试过以下方式。但我收到的错误如“org.springframework.beans.factory.NoSuchBeanDefinitionException”。下面我给出了代码:

控制器:

@RestController
public class Controller_Test implements SpringApplicationContextInterface
    {
         @Autowired
         private Say s1;

        @RequestMapping(value = "/test10")
        public String Test1()
        {
            s1.test();
            return "@RequestMapping(value = test10)";
        }


    }

public interface SpringApplicationContextInterface 
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanConfig.xml");

}

XML配置文件:

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

        <!--<context:annotation-config />-->

    <bean class="com.fossians.maven_courses.Say"  />
</beans>

我也尝试过:

<bean class="com.fossians.maven_courses.Say" id="s1" />

错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field s1 in com.fossians.maven_courses.Controller_Test required a bean of type 'com.fossians.maven_courses.Say' that could not be found.


Action:

Consider defining a bean of type 'com.fossians.maven_courses.Say' in your configuration.
...........................


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller_Test': Unsatisfied dependency expressed through field 's1'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fossians.maven_courses.Say' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
................................................


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fossians.maven_courses.Say' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
..............................

3 个答案:

答案 0 :(得分:2)

一种可能的解决方案是:

  1. 首先验证xml bean文件是否位于资源文件夹中。
  2. 验证bean定义是否具有id definied
  3. <bean class="com.fossians.maven_courses.Say" id="s1">

    1. 进入Main应用程序类,在SpringBoot批注之后添加bean xml文件的导入,在本例中我使用了spring-config.xml
    2. @SpringBootApplication @ImportResource("classpath:spring-config.xml")

      1. 转到Controller类并自动连接应用程序上下文
      2. @Autowired private ApplicationContext context;

        1. 如果您需要在应用程序上下文中获取s1 bean,请使用:
        2. ((Say)context.getBean("s1")).yourMethod();

          1. 您也可以在控制器上自动加载s1服务
          2. @Autowired private Say sayBean;

            1. 您也可以在Say类中使用@Service注释并自动连接到控制器,这取决于您的需求。
            2. 希望它有所帮助。

答案 1 :(得分:1)

尝试这种方式获取applicationContext

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null){
            SpringUtil.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    //通过name获取bean
    public Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取bean
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name和class返回指定bean
    public static <T> T getBean(String name, Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

然后使用beanclazz

获取name
Say say = SpringUtil.getBean(Say.class);

答案 2 :(得分:0)

请在

中更改您的代码
<bean class="com.fossians.maven_courses.Say"  />

in xml to

<bean class="com.fossians.maven_courses.Say" id ="s1"  />

并查看它是否有任何区别