1.pom.xml
里面加入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
2. 代码路径
其中resources路径是配置文件
/test/java下面是测试代码
3. 测试文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:config/applicationContext.xml"})
@Transactional
//@TransactionConfiguration(defaultRollback=false)
public class GenericDAOTest {
@Autowired
private AttrUnitDao attrUnitDao;
@Test
public void testInsert() {
List<Map<String, Object>> unitList = attrUnitDao.getUnitList();
System.out.println(unitList.size()+" --------------------------------");
AttrUnit tmp = new AttrUnit();
tmp.setCode("121");
tmp.setName("miqi");
tmp.setType(123);
attrUnitDao.save(tmp);
System.out.println(unitList.size()+" --------------------------------");
}
}
4 执行报错
a 首先提示 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.maxIdle' in string value "${redis.maxIdle}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
在下面文件中 找不到对应的redis.properties 文件
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
redis.xml 加入 <context:property-placeholder location="classpath:redis.properties" /> 依然无效
直接把redis.properties里对应的值填进到redis.xml里面
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="testOnBorrow" value="true" />
</bean>
问题暂时解决 涉及到这种的xml都要改
b 接着提示 java.lang.NoClassDefFoundError: org/springframework/cglib/core/CodeGeneratio。。。
加入一下依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
继续报异常 Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
查到是包依赖冲突
tomcat 这时候也起不来了。。<span style="font-family:Arial, Helvetica, sans-serif;">查找maven 依赖树 dependency hierarchy </span><pre name="code" class="html" style="font-family: Arial, Helvetica, sans-serif;">是加入<span style="font-family: Arial, Helvetica, sans-serif;">spring-core依赖导致的。。有版本冲突spring-webmvc里面版本是3.1.2但是去掉又会报</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">java.lang.NoClassDefFoundError: org/springframework/cglib/core/CodeGeneratio spring 项目里面jar都是3.1.2
应该是比spring4 少了些类 ,将spring版本统一提高到4.0.2
<span style="white-space:pre"> </span><org.springframework-version>4.0.2.RELEASE</org.springframework-version>
<span style="white-space:pre"> </span><org.aspectj-version>1.6.9</org.aspectj-version>
<span style="white-space:pre"> </span><org.slf4j-version>1.7.2</org.slf4j-version>
<span style="white-space:pre"> </span></properties>
<span style="white-space:pre"> </span><repositories>
<span style="white-space:pre"> </span><repository>
<span style="white-space:pre"> </span><id>public</id>
<span style="white-space:pre"> </span><!-- <url>http://192.168.2.115:8081/nexus/content/groups/public/</url> -->
<span style="white-space:pre"> </span><!-- <url>http://repository.sonatype.org/content/groups/public/</url> -->
<span style="white-space:pre"> </span><url>https://repo.maven.apache.org/maven2/</url>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span></repository>
<span style="white-space:pre"> </span></repositories>
<span style="white-space:pre"> </span><dependencies><span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- Spring -->
<span style="white-space:pre"> </span><dependency>
<span style="white-space:pre"> </span><groupId>org.springframework</groupId>
<span style="white-space:pre"> </span><artifactId>spring-webmvc</artifactId>
<span style="white-space:pre"> </span><version>${org.springframework-version}</version>
<span style="white-space:pre"> </span></dependency>
<span style="white-space:pre"> </span><dependency>
<span style="white-space:pre"> </span><groupId>org.springframework</groupId>
<span style="white-space:pre"> </span><artifactId>spring-jdbc</artifactId>
<span style="white-space:pre"> </span><version>${org.springframework-version}</version>
<span style="white-space:pre"> </span></dependency>
<span style="white-space:pre"> </span><dependency>
<span style="white-space:pre"> </span><groupId>org.springframework</groupId>
<span style="white-space:pre"> </span><artifactId>spring-orm</artifactId>
<span style="white-space:pre"> </span><version>${org.springframework-version}</version>
<span style="white-space:pre"> </span></dependency>
----------------
<span style="font-family:Arial, Helvetica, sans-serif;"> 问题ok 可以编译通过。。</span>
<span style="font-family:Arial, Helvetica, sans-serif;">
</span>
<span style="font-family:Arial, Helvetica, sans-serif;">测试代码事物自动回滚</span>
<span style="font-family:Arial, Helvetica, sans-serif;"></span><pre name="code" class="java">@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:config/applicationContext.xml"})
@Transactional
<strong>//@TransactionConfiguration(defaultRollback=false) //加入该句后依然还是事务回滚 有空看看什么情况,,</strong>
public class GenericDAOTest {
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>