一、写Spring的配置文件
在resources目录下新建一个XML configuration file -- spring config文件,叫applicationContext.xml。其中的内容替换为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解的扫描,希望处理service和dao,controller不需要spring框架去处理 -->
<context:component-scan base-package="cn.itcast" >
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--spring整合mybatis框架,其实就是把之前在mybatis框架中的配置转移到了spring中去,在这里配制好之后,就可以把SqlMapConfig.xml文件删掉了-->
<!--配置连接池:就是连接数据库用的那些信息,密码之类的-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm?useUnicode=true&characterEncoding=UTF-8"/>
<!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true;characterEncoding=UTF-8;useJDBCCompliantTimezoneShift=true;useLegacyDatetimeCode=false;serverTimezone=UTC"/>-->
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置AccountDao接口所在的包,来指定需要生成那些包的代理对象:就是SqlMapConfig.xml中的映射配置文件,-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.dao"/>
</bean>
<!--配置Spring框架声明式事务管理-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置事务通知,引进上面定义的事务管理器-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--以find开头的方法,只能进行读操作-->
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))"/>
</aop:config>
</beans>
二、给服务类加上注解
//给AccountService和AccountServiceImpl两个类加上注解:
@Service("accountService")
三、写测试类
//在itcast目录下新建一个包test,里面新建一个类testSpring
package cn.itcast.test;
import cn.itcast.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author lei
* @date 2019-07-24 16:20
*/
public class TestSpring {
@Test
public void run1(){
//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
AccountService as = (AccountService) ac.getBean("accountService");
//调用方法
as.findAll();
}
}
四、单独执行这个测试类中的run1方法
五、消除上一步中出现的红字
将log4j.properties配置文件【链接】复制到resources目录下,就可以消除这里的红字
到这里,spring测试完成。