感谢各位帮忙回答的好人~~~ 祝好人一生平安!
SSH使用的全注解形式,junit4..10也是以注解的测试。
用tmocat启动正常可以creat bean,但用junit测试就会出现:
Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]
如果是测试的service层,错误如下:
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@19632847] to prepare test instance [com.wuliu.service.WlZyclxxServiceTest@1786ed7a]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.wuliu.service.WlZyclxxServiceTest(这是junit测试的类)': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'wlZyclxxService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'wlZyclxxDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
在测试类里面 @resource和@autowired 都尝试过。
测试目的:在service\dao\action测试。以手动设置值传递到dao层方法,看dao层在数据库执行结果。
Junit测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
public class WlZyclxxServiceTest extends AbstractTransactionalJUnit4SpringContextTests
{
// ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
@Resource
private WlZyclxxService wlZyclxxService;
@Before
public void setUp() throws Exception
{
System.out.println("Test before");
// wlZyclxxService = (WlZyclxxServiceImpl) ac.getBean("wlZyclxxService");
}
@Test
public void testFindById()
{
String checkIsExist = wlZyclxxService.findById("23");
System.out.println(checkIsExist);
}
}
applicationContext.xml 配置
<context:component-scan base-package="com.wuliu"></context:component-scan>
<!-- 加载外部配置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 alibaba-->
<bean id="dataSource" destroy-method="close" init-method="init" class="com.alibaba.druid.pool.DruidDataSource" >
........................
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="org.springframework.orm.hibernate4.JtaSpringSessionContext" value="jta"></property> -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/wuliu/entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 注意与create的区别 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<!-- 配置声明式事务 -->
<!-- 采用基于注解的方式 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 1配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
service实现类
@Service(value="wlZyclxxService")
@Transactional
public class WlZyclxxServiceImpl implements WlZyclxxService {
@Resource
private WlZyclxxDao wlZyclxxDao;
..............
}
dao实现类
@Repository(value="wlZyclxxDao")
public class WlZyclxxDaoImpl extends BaseDaoImpl<WlZyclxx, Serializable> implements WlZyclxxDao {
}
------解决思路----------------------
这是你的test类放错地方了,你的spring扫描到这个类,但是你的这个testService没有加@Service导致的。建议楼主重新建一个包,比如com.wuliu.test.service 然后放进去