最近在学习Spring,没有听从前辈建议使用SpringBoot,在STS上直接new spring maven project,在配置jar包上吃了不少亏。下面我把以上所需的maven依赖以及配置Spring data JPA的Java类配置方式进行说明。
在配置Spring data JPA的jar包时,莫名其妙的出现一个异常:
java.lang.IllegalArgumentException:ResourceLoader must not be null!
执拙的我花了一下午的时间来处理这个异常,最后终于在google上找到了毛病:
原来是jpa的jar包版本与spring core的jar包版本冲突。
给Spring跪了。。。
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.irmgroupId>
<artifactId>labartifactId>
<name>Exam2name>
<packaging>warpackaging>
<version>1.0.0-BUILD-SNAPSHOTversion>
<properties>
<java-version>1.6java-version>
<org.springframework-version>4.3.0.RELEASEorg.springframework-version>
<org.aspectj-version>1.6.10org.aspectj-version>
<org.slf4j-version>1.6.6org.slf4j-version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${org.springframework-version}version>
<exclusions>
<exclusion>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${org.springframework-version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${org.springframework-version}version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-jpaartifactId>
<version>1.6.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${org.springframework-version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjrtartifactId>
<version>${org.aspectj-version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${org.slf4j-version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jcl-over-slf4jartifactId>
<version>${org.slf4j-version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${org.slf4j-version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.15version>
<exclusions>
<exclusion>
<groupId>javax.mailgroupId>
<artifactId>mailartifactId>
exclusion>
<exclusion>
<groupId>javax.jmsgroupId>
<artifactId>jmsartifactId>
exclusion>
<exclusion>
<groupId>com.sun.jdmkgroupId>
<artifactId>jmxtoolsartifactId>
exclusion>
<exclusion>
<groupId>com.sun.jmxgroupId>
<artifactId>jmxriartifactId>
exclusion>
exclusions>
<scope>runtimescope>
dependency>
<dependency>
<groupId>javax.injectgroupId>
<artifactId>javax.injectartifactId>
<version>1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.7version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>4.2.21.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-entitymanagerartifactId>
<version>4.2.21.Finalversion>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>2.2.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-pluginartifactId>
<version>2.9version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnatureprojectnature>
additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilderbuildcommand>
additionalBuildcommands>
<downloadSources>truedownloadSources>
<downloadJavadocs>truedownloadJavadocs>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.5.1version>
<configuration>
<source>1.6source>
<target>1.6target>
<compilerArgument>-Xlint:allcompilerArgument>
<showWarnings>trueshowWarnings>
<showDeprecation>trueshowDeprecation>
configuration>
plugin>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>exec-maven-pluginartifactId>
<version>1.2.1version>
<configuration>
<mainClass>org.test.int1.MainmainClass>
configuration>
plugin>
plugins>
build>
project>
便于清晰易懂,以下我将分为四个类进行配置。
package org.irm.lab.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/*
* 上下文配置
* */
@Configuration
//@ComponentScan(basePackages= {"org.irm.lab"},excludeFilters= {
// @ComponentScan.Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)
//})
@Import({JdbcConfig.class,JPAConfig.class,TransactionConfig.class})
public class RootConfig {
}
package org.irm.lab.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class JdbcConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db_test?autoReconnect=true");
dataSource.setUsername("root");
dataSource.setPassword("cheerup");;
System.out.println(dataSource);
return dataSource;
}
}
package org.irm.lab.configuration;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@Configuration
@EnableJpaRepositories(basePackages= {"org.irm.lab.dao"})
public class JPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
// 设置是否生成DDL被已经被初始化后,创建/更新所有相关表
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(vendorAdapter);
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.show_sql", "true");
/*
* hbm2ddl.auto: 生成表结构的策略配置
update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
如果存在表结构,并且表结构与实体一致,那么不做修改
如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
* */
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.format_sql", "false");
factoryBean.setJpaProperties(properties);
//扫描实体类所在的包
factoryBean.setPackagesToScan("org.irm.lab.entity");
factoryBean.setDataSource(dataSource);
return factoryBean;
}
}
package org.irm.lab.configuration;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory
localContainerEntityManagerFactoryBean) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(localContainerEntityManagerFactoryBean);
return txManager;
}
}
其他的配置就不作说明了,也可以通过Java类进行配置,网上资源很多。
综上。