SSM_基于JavaConfig(不使用XML配置文件)整合Spring&SpringMVC&Mybatis

  1. 在pom.xml中导入相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.ssmgroupId>
  <artifactId>ssm-integrationartifactId>
  <version>1.0.0version>
  <packaging>warpackaging>
  
  <properties>
  	<spring-version>5.2.2.RELEASEspring-version>
  	<mybatis-version>3.4.6mybatis-version>
  	<mybatis-spring-version>2.0.3mybatis-spring-version>
  	<dbcp-version>2.7.0dbcp-version>
  	<mysql-version>5.1.47mysql-version>
  	<log4j-version>1.2.17log4j-version>
  	<war-plugin-version>3.2.3war-plugin-version>
  properties>
  <dependencies>
  	
  	 
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-webmvcartifactId>
	    <version>${spring-version}version>
	dependency>
  	 
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-jdbcartifactId>
	    <version>${spring-version}version>
	dependency>
	  	 
	<dependency>
	    <groupId>org.mybatisgroupId>
	    <artifactId>mybatisartifactId>
	    <version>${mybatis-version}version>
	dependency>
	  	 
	<dependency>
	    <groupId>org.mybatisgroupId>
	    <artifactId>mybatis-springartifactId>
	    <version>${mybatis-spring-version}version>
	dependency>
	  	 
	<dependency>
	    <groupId>org.apache.commonsgroupId>
	    <artifactId>commons-dbcp2artifactId>
	    <version>${dbcp-version}version>
	dependency>
	  	 
	<dependency>
	    <groupId>mysqlgroupId>
	    <artifactId>mysql-connector-javaartifactId>
	    <version>${mysql-version}version>
	dependency>
	  	 
	<dependency>
	    <groupId>log4jgroupId>
	    <artifactId>log4jartifactId>
	    <version>${log4j-version}version>
	dependency>
	  	 
  dependencies>
  
  <build>
  	<plugins>
  		
  		<plugin>
	        <groupId>org.apache.maven.pluginsgroupId>
	        <artifactId>maven-war-pluginartifactId>
	        <version>${war-plugin-version}version>
	      plugin>
  	plugins>
  build>
project>
  1. 创建WebAppInit类(相当于web.xml)
package com.aowin;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * web.xml
 * @author 83998
 *
 */
public class WebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {
	/**
	 * applicationContext.xml
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] { RootConfig.class };
	}

	/**
	 * springDispatcherServlet-servlet.xml
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { WebMvcConfig.class };
	}

	/**
	 * springDispatcherServlet-servlet.xml's url-pattern
	 */
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}
}

  1. 定义WebMVCConfig类
package com.aowin;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * springDispatcherServlet-servlet.xml
 * @author 83998
 *
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.test.controller")
public class WebMvcConfig implements WebMvcConfigurer {
	/**
	 * 可以加载静态资源
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
}
  1. 定义数据源配置类DataSourceConfig
package com.aowin;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
	@Value("${jdbc.driver}")
	private String driver;
	@Value("${jdbc.url}")
	private String url;
	@Value("${jdbc.username}")
	private String username;
	@Value("${jdbc.password}")
	private String password;

	/**
	 * 配置数据源 dbcp2
	 * @return
	 */
	@Bean
	public DataSource dataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		return dataSource;
	}
}

  1. 定义RootConfig类
package com.aowin;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * applicationContext.xml
 * @author 83998
 *
 */
@Configuration
@Import(DataSourceConfig.class)
@EnableTransactionManagement
@ComponentScan("com.test.service.impl")
public class RootConfig {

	/**
	 * 创建sqlSessionFactory
	 * @return
	 * @throws Exception 
	 */
	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		return sqlSessionFactoryBean.getObject();
	}

	/**
	 * 创建Mapper接口的代理对象
	 * @return
	 */
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.test.dao");
		return mapperScannerConfigurer;
	}

	/**
	 * 事务管理器
	 * @param dataSource
	 * @return
	 */
	@Bean
	public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
		DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
		dataSourceTransactionManager.setDataSource(dataSource);
		return dataSourceTransactionManager;
	}
}

注意:

  1. RootConfig和WebMvcConfig都是在WebAppInit调用时new出来的两个IOC容器,所以这两个类中不能通过添加@AutoWired或者@Value等注解给属性赋值。因为通过注解完成依赖注入是IOC容器的任务,IOC容器不能在自身初始化的过程中给自己完成依赖注入。

你可能感兴趣的:(Spring)