1、使用properties属性文件方式配置数据源。
1.1、在applicationContext.xml配置文件中,引入database.properties配置文件:
<!-- 快捷方式 Ctrl+Shift+H 直接拿到对象完全限定名-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<!-- 引入配置文件 -->
<value>classpath:database.properties</value>
</property>
</bean>
<!-- 配置数据源对象DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接信息 -->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
1.2、测试类使用Junit单元测试输出:
/*
* add用户信息 UserService调用
*/
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
//原来是通过映射接口去调用
//UserMapper userMapper = (UserMapper) context.getBean("userMapper");
//获取所有JavaBean的名称
String[] bean = context.getBeanDefinitionNames();
for (String string : bean) {
System.out.println(string);
}
//现在是通过业务接口去调用
UserService service = (UserService) context.getBean("userService");
User user = new User();
user.setUserCode("zhangxiulian");
user.setUserName("张秀连666");
user.setUserPassword("123999");
user.setGender(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
user.setBirthday(sdf.parse("2010-9-17"));
} catch (ParseException e) {
e.printStackTrace();
}
user.setPhone("13608880888");
user.setAddress("养育巷49");
user.setUserRole(2);
user.setCreatedBy(1);
user.setCreationDate(new Date());
Integer userRole = 2;
//int num = userMapper.addUser(user);
int num = service.saveUser(user);
if (num > 0) {
logger.info("增加成功!");
} else {
logger.error("增加失败!");
}
}
1.3、控制台输出结果:
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0
dataSource
sqlSessionFactory
org.mybatis.spring.mapper.MapperScannerConfigurer#0
userService
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
userMapper
- (715 ms) - 2019-9-29 16:55:12[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean 'userService'
- (749 ms) - 2019-9-29 16:55:12[DEBUG](DataSourceUtils.java:110) Fetching JDBC Connection from DataSource
- (765 ms) - 2019-9-29 16:55:12[DEBUG](DataSourceUtils.java:327) Returning JDBC Connection to DataSource
- (766 ms) - 2019-9-29 16:55:12[ INFO](SpringTest.java:81) 增加成功!
2、使用Jndi方式配置数据源。
2.1、改造配置文件applicationContext.xml:
<!--配置数据源Jndi-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/java</value>
</property>
</bean>
2.2、注释配置文件applicationContext.xml中的数据源:
<!--数据源 start-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 连接信息 -->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--数据源 end-->
2.3、找到安装Tomcat的文件夹,在conf目录下打开context.xml配置文件,添加如下代码:
<Resource name="jdbc/java"
type="javax.sql.DataSource"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:///p19_java7_mybatis"
username="root"
password="aaa"
maxActive="100"
maxIdle="50"
maxWait="1000"
/>
2.4、在包com.javaxyz.servlet下,创建UserServlet.java文件
/**
* @Author:DongGaoYun
* @Description:
* @Date 2019-10-9 下午5:11:10
* @Version 1.0
* @Company: www.springhome.org
*/
package com.javaxyz.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.javaxyz.entity.User;
import com.javaxyz.service.UserService;
import com.javaxyz.test.SpringTest;
/**
* @ClassName:UserServlet.java
* @Description:描述信息
* @Author:DongGaoYun
* @Author English name:Andy
* @URL:www.javaxyz.com 或 www.gyun.org
* @Email:DongGaoYun@qq.com
* @QQ:1050968899
* @WeiXin:QingYunJiao
* @WeiXinGongZhongHao: JavaForum
* @Date:2019-10-9 下午5:11:10
* @Version:1.0
*/
public class UserServlet extends HttpServlet {
private static Logger logger = Logger.getLogger(UserServlet.class);
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* add用户信息 UserService调用 配置声明式事务
*/
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
// 原来是通过映射接口去调用
// UserMapper userMapper = (UserMapper) context.getBean("userMapper");
//获取所有JavaBean的名称
String[] bean = context.getBeanDefinitionNames();
for (String string : bean) {
System.out.println(string);
}
// 现在是通过业务接口去调用
UserService service = (UserService) context.getBean("userService");
User user = new User();
user.setUserCode("zhangxiulian");
user.setUserName("张秀连666");
user.setUserPassword("123999");
user.setGender(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
user.setBirthday(sdf.parse("2010-9-17"));
} catch (ParseException e) {
e.printStackTrace();
}
user.setPhone("13608880888");
user.setAddress("养育巷49");
user.setUserRole(2);
user.setCreatedBy(1);
user.setCreationDate(new Date());
Integer userRole = 2;
// int num = userMapper.addUser(user);
List<User> listUsers = new ArrayList<User>();
listUsers.add(user);
listUsers.add(user);
int num = service.saveUser(listUsers);
if (num > 0) {
logger.info("增加成功!");
} else {
logger.error("增加失败!");
}
}
}
注意:使用jndi数据源,显示结果需要启动Tomcat服务器,执行web路径: http://localhost:9999/java7_chapter7_spring2_jndi/userServlet
2.5、控制台输出结果:
- (1061 ms) - 2019-10-9 17:15:13[DEBUG](PropertySourcesPropertyResolver.java:103) Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0
dataSource
sqlSessionFactory
org.mybatis.spring.mapper.MapperScannerConfigurer#0
userService
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
transactionManager
org.springframework.aop.config.internalAutoProxyCreator
org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0
org.springframework.transaction.interceptor.TransactionInterceptor#0
org.springframework.transaction.config.internalTransactionAdvisor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
userMapper
- (1061 ms) - 2019-10-9 17:15:13[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean 'userService'
- (1063 ms) - 2019-10-9 17:15:13[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
- (1066 ms) - 2019-10-9 17:15:13[DEBUG](AbstractFallbackTransactionAttributeSource.java:107) Adding transactional method 'UserServiceImpl.saveUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
- (1069 ms) - 2019-10-9 17:15:13[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean 'transactionManager'
- (1076 ms) - 2019-10-9 17:15:13[DEBUG](AbstractPlatformTransactionManager.java:366) Creating new transaction with name [com.javaxyz.service.impl.UserServiceImpl.saveUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
- (1078 ms) - 2019-10-9 17:15:13[DEBUG](DataSourceTransactionManager.java:204) Acquired Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver] for JDBC transaction
- (1087 ms) - 2019-10-9 17:15:13[DEBUG](DataSourceTransactionManager.java:221) Switching JDBC Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver] to manual commit
- (1093 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Creating a new SqlSession
- (1099 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a]
- (1138 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) JDBC Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver] will be managed by Spring
- (1141 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ooo Using Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver]
- (1146 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ==> Preparing: INSERT INTO smbms_user (userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
- (1172 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ==> Parameters: zhangxiulian(String), 张秀连666(String), 123999(String), 0(Integer), 2010-09-17 00:00:00.0(Timestamp), 13608880888(String), 养育巷49(String), 2(Integer), 1(Integer), 2019-10-09 17:15:13.769(Timestamp)
- (1175 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a]
- (1176 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a] from current transaction
- (1177 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ooo Using Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver]
- (1178 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ==> Preparing: INSERT INTO smbms_user (userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
- (1178 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) ==> Parameters: zhangxiulian(String), 张秀连666(String), 123999(String), 0(Integer), 2010-09-17 00:00:00.0(Timestamp), 13608880888(String), 养育巷49(String), 2(Integer), 1(Integer), 2019-10-09 17:15:13.769(Timestamp)
- (1180 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a]
- (1180 ms) - 2019-10-9 17:15:13[DEBUG](AbstractPlatformTransactionManager.java:753) Initiating transaction commit
- (1181 ms) - 2019-10-9 17:15:13[DEBUG](DataSourceTransactionManager.java:267) Committing JDBC transaction on Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver]
- (1188 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a]
- (1188 ms) - 2019-10-9 17:15:13[DEBUG](JakartaCommonsLoggingImpl.java:46) Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cd1130a]
- (1191 ms) - 2019-10-9 17:15:13[DEBUG](DataSourceTransactionManager.java:325) Releasing JDBC Connection [jdbc:mysql:///p19_java7_mybatis, UserName=root@localhost, MySQL-AB JDBC Driver] after transaction
- (1191 ms) - 2019-10-9 17:15:13[DEBUG](DataSourceUtils.java:327) Returning JDBC Connection to DataSource
- (1192 ms) - 2019-10-9 17:15:13[ INFO](UserServlet.java:126) 增加成功!
3、bug场景:引入database.properties配置文件时报错:
- 报错详情:
- (369 ms) - 2019-10-9 16:34:56[ WARN](AbstractApplicationContext.java:486) Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'users' in string value "${users}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'users' in string value "${users}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:223)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:696)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:671)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.javaxyz.test.SpringTest.test9(SpringTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'users' in string value "${users}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:206)
... 31 more
- (371 ms) - 2019-10-9 16:34:56[ INFO](DefaultSingletonBeanRegistry.java:444) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6a109ac: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,userService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,userMapper]; root of factory hierarchy
- 重点:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'users' in string value "${users}"
意思是:无法解析字符串值“${users}”中的占位符“users”
- 报错位置:
<property name="username" value="${users}"></property>
- 报错原因是配置文件database.properties里用户名的key是user,而不是users
user=root
- 正确的配置方式:
<property name="username" value="${user}"></property>
- Java入门-Java学习路线课程第一课:初识JAVA
- Java入门-Java学习路线课程第二课:变量与数据类型
- Java入门-Java学习路线课程第三课:选择结构
- Java入门-Java学习路线课程第四课:循环结构
- Java入门-Java学习路线课程第五课:一维数组
- Java入门-Java学习路线课程第六课:二维数组
- Java入门-Java学习路线课程第七课:类和对象
- Java入门-Java学习路线课程第八课:方法和方法重载
- Java入门-Java学习路线扩展课程:equals的使用
- Java入门-Java学习路线课程面试篇:取商 / 和取余(模) % 符号的使用
- Java进阶-Java学习路线课程第一课:Java集合框架-ArrayList和LinkedList的使用
- Java进阶-Java学习路线课程第二课:Java集合框架-HashSet的使用及去重原理
- Spring框架-Java学习路线课程第一课:Spring核心
- Springboot框架-Java学习路线课程:Springboot框架的搭建之maven的配置
- java学习:在给学生演示用Myeclipse10.7.1工具生成War时,意外报错:SECURITY: INTEGRITY CHECK ERROR
- 使用jquery发送Ajax请求的几种异步刷新方式
- idea Springboot启动时内嵌tomcat报错- An incompatible version [1.1.33] of the APR based Apache Tomcat Native
- 一个简单的SSM框架Demo(登录(包含拦截器)和注销