IDEA spring配置容器引入配置文件的坑

 

 

spring中导入外部配置文件的常见两种方式

方式一:


       


方式二:

 

 

注意:方式二这种导入外部配置文件的方式用户名不能使用username 改成name即可

IDEA spring配置容器引入配置文件的坑_第1张图片
运行结果:若使用username会出现如下错误

C:\JDK\jdk1_8.0.131\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Idea2018\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=26101:D:\Idea2018\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Idea2018\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar;D:\Idea2018\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit-rt.jar;D:\Idea2018\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit5-rt.jar;C:\JDK\jdk1_8.0.131\jre\lib\charsets.jar;C:\JDK\jdk1_8.0.131\jre\lib\deploy.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\access-bridge-64.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\cldrdata.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\dnsns.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\jaccess.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\jfxrt.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\localedata.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\nashorn.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\sunec.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\sunjce_provider.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\sunmscapi.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\sunpkcs11.jar;C:\JDK\jdk1_8.0.131\jre\lib\ext\zipfs.jar;C:\JDK\jdk1_8.0.131\jre\lib\javaws.jar;C:\JDK\jdk1_8.0.131\jre\lib\jce.jar;C:\JDK\jdk1_8.0.131\jre\lib\jfr.jar;C:\JDK\jdk1_8.0.131\jre\lib\jfxswt.jar;C:\JDK\jdk1_8.0.131\jre\lib\jsse.jar;C:\JDK\jdk1_8.0.131\jre\lib\management-agent.jar;C:\JDK\jdk1_8.0.131\jre\lib\plugin.jar;C:\JDK\jdk1_8.0.131\jre\lib\resources.jar;C:\JDK\jdk1_8.0.131\jre\lib\rt.jar;E:\WorkSpace\框架\Mybatis\Mybatis\target\test-classes;E:\WorkSpace\框架\Mybatis\Mybatis\target\classes;C:\Users\一枝叶\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\一枝叶\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\一枝叶\.m2\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar;C:\Users\一枝叶\.m2\repository\org\mybatis\mybatis\3.2.6\mybatis-3.2.6.jar;C:\Users\一枝叶\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-core\5.1.5.RELEASE\spring-core-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-jcl\5.1.5.RELEASE\spring-jcl-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-beans\5.1.5.RELEASE\spring-beans-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-context\5.1.5.RELEASE\spring-context-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-expression\5.1.5.RELEASE\spring-expression-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\一枝叶\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\一枝叶\.m2\repository\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-aop\5.1.5.RELEASE\spring-aop-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-jdbc\5.1.5.RELEASE\spring-jdbc-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\springframework\spring-tx\5.1.5.RELEASE\spring-tx-5.1.5.RELEASE.jar;C:\Users\一枝叶\.m2\repository\org\mybatis\mybatis-spring\1.2.2\mybatis-spring-1.2.2.jar;C:\Users\一枝叶\.m2\repository\commons-pool\commons-pool\1.6\commons-pool-1.6.jar;C:\Users\一枝叶\.m2\repository\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 TestSpring,getSpring

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user '一��'@'localhost' (using password: YES))

	at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:305)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:378)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy21.getCount(Unknown Source)
	at TestSpring.getSpring(TestSpring.java:50)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user '一��'@'localhost' (using password: YES))
	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
	at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
	at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:262)
	... 30 more
Caused by: java.sql.SQLException: Access denied for user '一��'@'localhost' (using password: YES)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
	at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:832)
	at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
	at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
	at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
	at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
	at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
	... 33 more


Process finished with exit code -1

在网上查看资料总结的原因大概是:windows系统中的用户名的属性也叫username,和外部配置文件中设置的username冲突,而
                                                       且控制台中显示的用户名称还是乱码,这是因为配置文件时gbk的缘故,所以在spring配置文件                                                         中使用${username}取值时,获取的是windows中的username的值,也就是我控制台中显乱码
                                                       部分,所以在连接数据库的时候用户名不正确获取不到连接

你可能感兴趣的:(IDEA spring配置容器引入配置文件的坑)