我对注释感到不舒服。在我的春季启动应用程序中,我正在通过应用程序上下文检索控制器中的服务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)}
..............................
答案 0 :(得分:2)
一种可能的解决方案是:
<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;
希望它有所帮助。
答案 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);
}
然后使用bean
或clazz
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" />
并查看它是否有任何区别