一.改变的地方:
1.配置实体类注解,此时可以删除掉set方法,而是用注解Autowired注入数据
2.还有创造实例对象此时也采用注解,删除掉xml相关操作;
3.由于此时使用注解,所以要多一个context环境,告诉Spring构造容器时所要扫描的包;
二.问题
1.开发过程中,软件开发和软件测试是两个属性。junit单元测试中,集成了一个main方法。会判断那里有@Test。但是Junit不知道我们是否采用了Spring框架。但是没有IOC容器,使用Atuowired根本不会起作用,即无法实现注入。
2.Spring整合JUnit:
1.导入坐标Spring-test;
2.使用Spring提供的注解替换掉JUnit的main方法;
@RunWith();
3.告知Spring运行器,spring和IOC是给予注解还是xml配置的,
@ContextConfiguration()
locations:指定xml文件的位置,加上classpath关键字,表示在类路径下;
classes‘’:指定注解所在的位置
注意:JUnit版本必须是4.12以上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class testDemo {
@Autowired
private IAccountService as=null;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
来源:CSDN
作者:kekeoneone
链接:https://blog.csdn.net/m0_45703532/article/details/104826671