SSM2.3【Spring:Spring集成Junit】

 

 

 

 

 

 

 

 

 

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="
 6        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9 
10     <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
11         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
12         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
13         <property name="user" value="root"></property>
14         <property name="password" value="root"></property>
15     </bean>
16 
17     <!--加载外部的properties文件-->
18     <!--需要在顶部beans中配置命名空间context,并在xsi中添加对应的约束路径-->
19     <context:property-placeholder location="classpath:jdbc.properties"/>
20     <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource">
21         <property name="driverClassName" value="${jdbc.driver}"></property>
22         <property name="url" value="${jdbc.url}"></property>
23         <property name="username" value="${jdbc.username}"></property>
24         <property name="password" value="${jdbc.password}"></property>
25     </bean>
26 
27 
28     <bean id="userDao" class="com.haifei.dao.impl.UserDaoImpl"></bean>
29     <bean id="userService" class="com.haifei.service.impl.UserServiceImpl">
30         <property name="userDao" ref="userDao"></property>
31     </bean>
32 
33     <!--配置组件扫描-->
34     <!--使用注解进行开发时(代替配置bean方式开发),需要在applicationContext.xml中配置组件扫描,
35     作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法
36     不配置这个的话,运行会报异常NoSuchBeanDefinitionException-->
37     <!--<context:component-scan base-package="com.haifei" />-->
38 
39     <!--<import resource="" />-->
40 
41 </beans>
复制代码
复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.haifei</groupId>
 8     <artifactId>SSM2_spring_ioc_anno</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11 
12     <dependencies>
13         <dependency>
14             <groupId>org.springframework</groupId>
15             <artifactId>spring-context</artifactId>
16             <version>5.0.5.RELEASE</version>
17         </dependency>
18         <dependency>
19             <groupId>junit</groupId>
20             <artifactId>junit</artifactId>
21             <version>4.12</version>
22         </dependency>
23         <dependency>
24             <groupId>mysql</groupId>
25             <artifactId>mysql-connector-java</artifactId>
26             <version>5.1.26</version>
27         </dependency>
28         <dependency>
29             <groupId>c3p0</groupId>
30             <artifactId>c3p0</artifactId>
31             <version>0.9.1.2</version>
32         </dependency>
33         <dependency>
34             <groupId>com.alibaba</groupId>
35             <artifactId>druid</artifactId>
36             <version>1.0.9</version>
37         </dependency>
38         <!--手动导入spring集成junit的test测试依赖-->
39         <dependency>
40             <groupId>org.springframework</groupId>
41             <artifactId>spring-test</artifactId>
42             <version>5.0.5.RELEASE</version>
43         </dependency>
44     </dependencies>
45 
46 
47     <build>
48         <plugins>
49             <plugin>
50                 <groupId>org.apache.maven.plugins</groupId>
51                 <artifactId>maven-compiler-plugin</artifactId>
52                 <version>3.1</version>
53                 <configuration>
54                     <target>1.8</target>
55                     <source>1.8</source>
56                 </configuration>
57             </plugin>
58         </plugins>
59     </build>
60 
61 
62 </project>
复制代码
复制代码
 1 package com.haifei.test;
 2 
 3 import com.haifei.config.SpringConfiguration;
 4 import com.haifei.service.UserService;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import javax.sql.DataSource;
12 import java.sql.SQLException;
13 
14 //使用@Runwith注解替换原来的运行期
15 @RunWith(SpringJUnit4ClassRunner.class) //SpringJUnit4ClassRunner requires JUnit 4.12 or higher  把pom中junit版本调高
16 //@ContextConfiguration("classpath:applicationContext.xml") //使用@ContextConfiguration指定配置文件
17 @ContextConfiguration(classes = {SpringConfiguration.class}) //使用@ContextConfiguration指定配置类
18 public class SpringJunitTest {
19 
20 //    使用@Autowired注入需要测试的对象
21     @Autowired //根据数据类型注入到spring中
22     private UserService userService;
23 
24     @Autowired
25     private DataSource dataSource_c3p0; //注意在使用"classpath:applicationContext.xml"时,变量名须与bean-id值一致
26 
27     @Test
28     public void test1() throws SQLException {
29         userService.save();
30         System.out.println(dataSource_c3p0.getConnection());
31     }
32 
33 }
复制代码
posted @   yub4by  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示