三大框架整合,无非就是一个导jar包,修改配置文件的过程。完了就没事了。
还是有很多细节性的问题
比如在spring中写applicationContext.xml文件时不提示:
解决方法如下:
如果写xml不提示:
Window-preferences-myeclipse-xml-xml catalog-user specified entries-add-
1. location:spring-beans-3.1.xsd的路径,在D:学习Javaspringspring-framework-3.2.1.RELEASE-distspring-framework-3.2.1.RELEASEschemaeans(这是我的路径,写上自己的路径即可)
2. uri 路径,同上
3. key type:Schema Location
4. key: http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
xml文件在xmlcatalog中找与key中字符串相同的文件即spring-beans-3.1.xsd
.xsd文件,指明了被这个文件指明的xml文件中能写什么不能写什么
项目中的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
Spring对Dao层的支持:
Spring提供了Dao框架,让开发人员无须耦合特定的数据库技术就能进行应用程序的开发
通过DAO接口进行开发,接口的实现通过spring注入
如:User user = new User();
//通过spring配置文件获得UserDao实现类
UserDao userdao=getUserDao();//getUserDao()就是context.getBean();
//再调用dao层的方法
Userdao.insert(user);
getUserDao()就是context.getBean();因此,由于依赖于接口,可以通过依赖注入随时替换UserDao接口的实现类,而应用程序完全不用了解接口和底层数据库的操作细节。
Spring使用持久层,必须知道数据源在哪里,所以要注入数据源。用注入数据源的方式注入数据库。连接数据的方式称为“数据源”,比如JDBC,连接池或者JNDI
Spring通过依赖注入的方式配置数据源:不同系统,数据源的管理更多是针对底层的行为,这些行为不应该影响业务。更换数据源只需要修改bean定义的内容,而不需要修改任何一行代码。
配置datasource
在配置文件中配置数据源:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSouerce">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:ORCL</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
配置PersonDaoBean:datasource所要注入的对象
<bean id="personDao" class="spring.jdbc.dao">
<property name="dataSource" ref="dataSource"/>
</bean>
通过ref属性,spring启动时,dataSource属性就注入到PersonDaoImpl中了。
过程:根据ref中的dataSource去查找persondaoImpl中的setdataSource方法,
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
上面的数据源配置通过参数dataSource传入persondaoImpl的成员变量里。