![](/img/trans.png)
[英]Conditionally load a bean in spring boot using xml configuration?
[英]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)}
..............................
一种可能的解决方案是:
<bean class="com.fossians.maven_courses.Say" id="s1">
@SpringBootApplication @ImportResource("classpath:spring-config.xml")
@Autowired private ApplicationContext context;
((Say)context.getBean("s1")).yourMethod();
@Autowired private Say sayBean;
希望能帮助到你。
尝试这种方式以获得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);
}
然后获取带有clazz
或name
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.