最近研究了ssm(spring + springMVC + Mybatis),现将心得总结如下:
一、web工程自己写的监听器可以在执行父类的方法时初始化applicationContext,代码如下
public class CacheTest extends ContextLoaderListener {
private static Logger logger = Logger.getLogger(CacheTest.class);
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("开始加载Cache!!---执行父类contextInitialized方法----");
super.contextInitialized(event);
logger.info("开始加载Cache!!---CacheTest----");
//获取WebApplicationContext
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
// context.
org.apache.commons.dbcp.BasicDataSource dataSource = (BasicDataSource) context.getBean("dataSource");
logger.info("getInitialSize--->>" + dataSource.getInitialSize());
logger.info("getNumActive--->>" + dataSource.getNumActive());
logger.info("getNumIdle--->>" + dataSource.getNumIdle());
// JdbcTemplate jdbcTemplate;
}
Spring配置文件如下:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http://www.springframework.org/schema/context classpath:org/springframework/context/config/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc classpath:org/springframework/web/servlet/config/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx classpath:org/springframework/transaction/config/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop classpath:org/springframework/aop/config/spring-aop-3.1.xsd
http://www.springframework.org/schema/task classpath:org/springframework/scheduling/config/spring-task-3.1.xsd
http://www.springframework.org/schema/security classpath:org/springframework/security/config/spring-security-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
">
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:config.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<!-- 自动扫描, 对被注释为@Controller,@Service,@Repository,@Component的类进行扫描,以完成Bean创建的功能 -->
<context:component-scan base-package="com.zkl" use-default-filters="true" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${pool.initialSize}"/>
<property name="validationQuery" value="${pool.validationQuery}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath:com/zkl/mybatis/modules/*.xml</value>
</list>
</property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zkl.mybatis.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--5.开启注解进行事务管理 transaction-manager:引用上面定义的事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Spring配置文件中,可以配置datasource,beans,缓存等。其步骤为:
1、引入配置文件。引入方式有多种,引入配置文件后,在类中可以通过注解获得配置文件的值。
@Value("${FTP_IP}")
private String ftpIp;
@Value("${FTP_PORT}")
private String ftpPort;
2、<!-- 自动扫描, 对被注释为@Controller,@Service,@Repository,@Component的类进行扫描,以完成Bean创建的功能 -->
<context:component-scan base-package="com.zkl" use-default-filters="true" />
3、数据源及持久层配置。我在持久层用了mybatis,在其中指定了mybatis配置文件的路径,Spring与mybatis可以完美融合;在数据源这里经过了:
(1)DriverManagerDataSource(DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用)
(2)BasicDataSource(真正使用了连接池技术)
(3)jndi (能够通过JNDI获取DataSource)
数据源各配置方式借鉴下文: spring配置datasource三种方式
最近的数据源配置大家都流行一种说法,说alibaba 的数据源Druid全世界最快,配置和上述模式差不多,其中增加了许多功能,待研究。
4、事物管理
二、SpringMVC在web.xml中作为servlet来加载启动,在servlet的参数中指定MVC的配置文件
"org.springframework.web.context.ContextLoaderListener"启动Spring,有以下两张方式:
1、可通过listener
2、或者继承ContextLoaderListener类,执行父类的方法时初始化applicationContext;方法中有取context的例子,从context中可以取到beans
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
三、mybatis的配置文件
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<typeAliases>
<!-- <typeAlias alias="Idnoinfo" type="com.zkl.pojo.Idnoinfo"/> -->
<package name="com.zkl.pojo"/>
</typeAliases>
<!-- <mappers>
<mapper resource="com/zkl/mybatis/modules/Idnoinfo.xml" />
<package name="com.zkl.mybatis.modules"/>
</mappers> -->
</configuration>