繁体   English   中英

如何使用xml配置将服务bean传递到Spring Boot控制器中

[英]How to pass service bean into spring boot controller using xml configuration

我对注释不满意。 在我的Spring Boot应用程序中,我正在按应用程序上下文在控制器中检索Service 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)}
..............................

一种可能的解决方案是:

  1. 首先,验证xml bean文件是否位于资源文件夹中。
  2. 验证bean定义是否具有定义的ID

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

  1. 转到Main应用程序类,并在SpringBoot注释后添加bean xml文件的导入,在这种情况下,我使用spring-config.xml

@SpringBootApplication @ImportResource("classpath:spring-config.xml")

  1. 转到Controller类并自动连接应用程序上下文

@Autowired private ApplicationContext context;

  1. 如果您需要通过应用程序上下文获取s1 bean,请使用以下命令:

((Say)context.getBean("s1")).yourMethod();

  1. 您也可以在控制器上自动连线s1服务

@Autowired private Say sayBean;

  1. 您还可以在Say类中使用@Service批注并自动连接到控制器,具体取决于您的需求。

希望能帮助到你。

尝试这种方式以获得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);
    }

然后获取带有clazzname bean

Say say = SpringUtil.getBean(Say.class);

请更改您的代码

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

在xml中

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

看看有什么区别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM