applicationContext.xml内部发生了什么 [英] What is happening inside applicationContext.xml
问题描述
请问有人解释下面的applicationContext.xml
文件在做什么?有关更多信息,请参见 http://persistentdesigns.com/wp/jersey- spring-and-jpa/.
Will someone please explain what the following applicationContext.xml
file is doing? For greater context, I get it from http://persistentdesigns.com/wp/jersey-spring-and-jpa/.
我的一些问题(并非详尽无遗,因为我确实不太了解):
Some of my questions (not exhaustive, since I really don't understand much):
-
对于
id="dataSource"
,dataSource
是关键字还是我要使用的数据源的名称?例如,如果我的数据源名称是实际的learningRestDS,是否可以将dataSource
替换为learningRestDS
?
For
id="dataSource"
isdataSource
a keyword or is it the name of the datasource I am to use? For example, if the name of my datasource is actuall learningRestDS, do I replacedataSource
withlearningRestDS
?
为什么数据源类名是org.springframework.jdbc.datasource.DriverManagerDataSource
而不是com.mysql.jdbc.jdbc2.optional.MysqlDataSource
?
Why is the datasource class name org.springframework.jdbc.datasource.DriverManagerDataSource
instead of com.mysql.jdbc.jdbc2.optional.MysqlDataSource
?
applicationConext.xml:
applicationConext.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Scan for both Jersey Rest Annotations a -->
<context:component-scan base-package="com.persistent.rest,com.persistent.service,com.persistent.service.jpa"/>
<context:annotation-config />
<tx:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/jpa"
p:username="user" p:password="password" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL" p:showSql="true" />
更新:错误:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService':
Injection of persistence methods failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]:
Invocation of init method failed; nested exception is java.lang.AbstractMethodError:
org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
推荐答案
"dataSource"
是一个任意的Bean名称(ID),稍后您将使用该名称来引用该Bean:
"dataSource"
is an arbitrary bean name (ID) you use later to reference that bean:
p:dataSource-ref="dataSource"
如果您的数据源名为"learningRestDS"
,则应使用:
If your data source was named "learningRestDS"
you would use:
p:dataSource-ref="learningRestDS"
p:dataSource-ref
是DriverManagerDataSource
属性的名称(您将在该类上找到用于注入learningRestDS
的setDataSource()
方法.
The p:dataSource-ref
is the name of the property of DriverManagerDataSource
(you'll find setDataSource()
method on that class that will be used to inject learningRestDS
.
在定义数据源时,您可以自由使用任何实现DataSource
接口(抽象的力量)的类.该XML的作者选择使用 DriverManagerDataSource
而不是MysqlDataSource
.只要它们都遵守DataSource
合同,它们的表现就一样好 1 .
When defining a data source you are free to use any class that implements DataSource
interface (the power of abstraction). The author of this XML chose to use DriverManagerDataSource
instead of MysqlDataSource
. They will work just as good1 as long as they both follow the DataSource
contract.
DataSource
还有许多其他可能的实现,例如 dbcp 和 c3p0 连接池.它们的优点是它们可以与所有JDBC驱动程序/数据库一起使用.
There are many other possible implementations of DataSource
, e.g. dbcp and c3p0 connection pooling libraries. The advantage of them is that they work with all JDBC drivers/databases.
1 实际上DriverManagerDataSource
仅应用于测试,因为它的性能非常差,但从功能的角度来看,它与任何其他DataSource
一样好.
1 actually DriverManagerDataSource
should only be used for testing as it performs very poorly, but from functional perspective, it's as good as any other DataSource
.
更新以回答您的评论.您正在看到:
UPDATE to answer your comments. You are seeing:
Invalid property 'driverClassName' of bean class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource]: No property 'driverClassName' found
这意味着Spring尝试向driverClassName中注入一些东西="nofollow"> MysqlDataSource
-不具有此属性(设置程序).这也意味着您引用的应用程序上下文XML文件是不完整/不一致/不正确的,因为您使用的是DriverManagerDataSource
.确保这是Spring使用的文件,如果使用MySQL数据源,则删除driverClassName
属性.
which means Spring tries to inject something to property driverClassName
of MysqlDataSource
- which doesn't have such property (setter). This also means that the application context XML file you are quoting is incomplete/inconsistent/incorrect as according to you it uses DriverManagerDataSource
. Make sure this is the file Spring uses and remove driverClassName
property if MySQL data source is used.
这篇关于applicationContext.xml内部发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!