Java 没有定义名为的bean
我正在使用JUnit测试Spring的组件,抛出错误:Java 没有定义名为的bean,java,spring,junit,Java,Spring,Junit,我正在使用JUnit测试Spring的组件,抛出错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wsVehicleService' is defined 这是我的测试课: package com.zw.ws.service.impl; import org.junit.Test; import org.junit.runner.RunWith; import org.spr
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wsVehicleService' is defined
这是我的测试课:
package com.zw.ws.service.impl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/*.xml")
public class WsVehicleServiceTest {
@Autowired
ApplicationContext ctx;
@Test
public void getHistoryAlarmInfo() throws Exception {
try {
String requestJson = "\"{\"VEHICLEID\":\"cae21196-cb66-4256-88a6-7cdfb23e2c78\",\"STARTTIME\":1476115200,\"ENDTIME\":1476201599,\"TYPE\":1}\"";
WsVehicleService vehicleService = (WsVehicleService) ctx.getBean("wsVehicleService");
vehicleService.getHistoryAlarmInfoImpl(requestJson);
} catch (BeansException e) {
e.printStackTrace();
}
}
}
这是将要测试的类:
@Component
public class WsVehicleService {
public List<HistoryAlarmInfo> getHistoryAlarmInfo(String requestJson) {
try {
return getHistoryAlarmInfoImpl(requestJson);
} catch (Exception e) {
log.error(e);
return null;
}
}
}
PS:这不是XML配置,所有bean都使用autoscan。更改行
WsVehicleService vehicleService = (WsVehicleService) ctx.getBean(WsVehicleService .class);
或
1) 第一个解决方案:
@ContextConfiguration
初始化Spring上下文,默认情况下,在与单元测试相同的包中查找Spring XML文件,该文件名与后缀为“-context.XML”的类相同
尝试在com.mycompany.annotation
包中添加WsVehicleServiceTest-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="wsVehicleService"
class="com.mycompany.annotation.WsVehicleService">
</bean>
</beans>
注意:如果要使用@ContextConfiguration(“classpath:/*.xml”)
请确保
<bean id="wsVehicleService"
class="com.mycompany.annotation.WsVehicleService">
</bean>
3) 第三种解决方案:
您已经提到,您不使用任何xml配置文件,并且您的异常告诉org.springframework.beans.factory.NoSuchBeanDefinitionException
您的WsVehicleService可能不存在于上下文中的一个原因是,它可能是在Spring未扫描的包中定义的
确保WsVehicleService存在于@ComponentScan(“com.mycompany.annotation”)
包中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="wsVehicleService"
class="com.mycompany.annotation.WsVehicleService">
</bean>
</beans>
1) 带注释的应用程序配置类
2) 你的服务级别
package com.mycompany.annotation;
import org.springframework.stereotype.Component;
@Component
public class WsVehicleService {
public String sayHi(){
return "HI !!!!!!!!!!";
}
}
3) Junit测试类
package com.mycompany.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class WsVehicleServiceTest {
@Autowired
ApplicationContext ctx;
@Test
public void getHistoryAlarmInfo() throws Exception {
try {
WsVehicleService vehicleService = (WsVehicleService) ctx
.getBean("wsVehicleService");
String hi = vehicleService.sayHi();
System.out.println(hi);
} catch (BeansException e) {
e.printStackTrace();
}
}
}
输出:
HI !!!!!!!!!!
有关更多信息,请参阅和
当出现以下情况时,将抛出“未定义名为'xxx'的bean”的更多参考:
- 您的bean有不同的名称李>
- 您的bean实际上不在上下文中
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AppTestConfig.class})
public class WsVehicleServiceTest {
...
}
您还可以使用
@Autowire
启动您的服务,并让Spring完成布线工作。Spring注释代码
//main class
//@Import(value=AppConfig.class)
public class AppAnno {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext(AppConfig.class);
TodayDate date = ctx.getBean("todayDate",TodayDate.class);
System.out.println(date.getTodyDate());
}
}
@Configuration
@ComponentScan(basePackages={"com.cb.service.anno","com.cb.dao.anno"})
根据您的基本包进行更改
public class AppConfig {
@Bean
public Date date(){
return new Date();
}
}
import java.util.Date;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Named("todayDate")//you have to add inject jar to your build path
public class TodayDate {
@Autowired
Date date;
public String getTodyDate(){
return date.toString( );
}
}
@ComponentScan
必须添加到测试类中!请显示您的XML配置并添加包names@Dolphin:查看我的答案,让我知道它是否对您有效。@未知它现在不起作用,但我会继续尝试。我有一个区域尝试的一个方法,因为它是一个工作的项目,配置有点复杂。
HI !!!!!!!!!!
@Configuration
@ComponentScan(basePackages = {"com.zw.ws"})
public class AppTestConfig {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AppTestConfig.class})
public class WsVehicleServiceTest {
...
}
//main class
//@Import(value=AppConfig.class)
public class AppAnno {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new
AnnotationConfigApplicationContext(AppConfig.class);
TodayDate date = ctx.getBean("todayDate",TodayDate.class);
System.out.println(date.getTodyDate());
}
}
@Configuration
@ComponentScan(basePackages={"com.cb.service.anno","com.cb.dao.anno"})
public class AppConfig {
@Bean
public Date date(){
return new Date();
}
}
import java.util.Date;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Named("todayDate")//you have to add inject jar to your build path
public class TodayDate {
@Autowired
Date date;
public String getTodyDate(){
return date.toString( );
}
}