测试类导入Dao层

第一种方法:@Test方法

1.@Autowired
功能:
a.为我们注入一个定义好的bean
b.主要还是能解决重复生成实例的问题
c. 将 Spring 容器中的 bean 自动的和我们需要的这个 bean 的类组装在一起

最常被使用的ApplicationContext接口实现:
     FileSystemXmIApplicationContext:
			         该容器从XML文件中加载已被定义的 bean。
			         在这里,你需要提供给构造器XML文件的完整路径。
     ClassPathXmIApplicationContext:
			         该容器从XML文件中加载已被定义的bean。
			         在这里,你不需要提供XML文件的完整路径,
			         只需正确配置CLASSPATH 环境变量即可,
			         因为,容器会从CLASSPATH中搜索bean配置文件。
     WebXmIApplicationContext:
			          该容器会在一个web应用程序的范围内加载在XML文件中已被定义的 bean。

.getBean方法:
通过ApplicationContext的getBean方法来获取Spring容器中已初始化的bean

public class TestUtil {1.//新建测试类
    @Autowired
    UserPrintDao userPrintDao;2.//注入需要的dao层
    @Test
    public void test1(){3.//单元测试
    	4.//获取容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
        5.//获取接口
        UserPrintDao userPrintDao = context.getBean(UserPrintDao.class);
        6.//使用接口方法
		userPrintDao.addList(list);
}```

第二种方法:mian方法

public class TestUtil {1.//新建测试类
    public static void main(String[] args){2.//mian方法测试
    	3.//获取容器对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");//classpath可省略
        4.//获取接口
        UserPrintDao userPrintDao = context.getBean("userPrintDao",UserPrintDao.class);//userPrintDao可省略
        5.//使用接口方法
		userPrintDao.addList(list);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大风~刮过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值