context.xml的简单介绍

http://www.itjxue.com  2023-02-22 19:46  来源:未知  点击次数: 

为什么Tomcat的context.xml会自动还原

你要在eclipse的一个项目Servers中,找到你的tomcat,打开有各种配置文件,在这里改,才会在使用eclipse启动tomcat后不会自动恢复。我也是刚刚发现并解决了这个问题的。

SSH2框架中 applicationContext.xml怎么配置?

以下是详解Spring的applicationContext.xml文件代码:

!--?头文件,主要注意一下编码?--

?xml?version="1.0"?encoding="UTF-8"???

!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN""

beans??

!--?建立数据源?--

bean?id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"

!--?数据库驱动,我这里使用的是Mysql数据库?--

propertynamepropertyname="driverClassName"??

????valuecom.mysql.jdbc.Driver/value??

???/property??

!--?数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦!?--

property?name="url"??

????value??

??????jdbc:mysql://localhost:3306/tie?useUnicode=truecharacterEncoding=utf-8??

???/value??

???/property

!--?数据库的用户名?--

property?name="username"??

????valueroot/value??

???/property

!--?数据库的密码?--

property?name="password"??

????value123/value??

???/property??

/bean

!--?把数据源注入给Session工厂?--

bean?id="sessionFactory"??

??class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"??

???property?name="dataSource"??

????ref?bean="dataSource"?/??

???/property

?!--?配置映射文件?--

property?name="mappingResources"??

????list??

?????valuecom/alonely/vo/User.hbm.xml/value??

????/list??

???/property??

/bean

!--?把Session工厂注入给hibernateTemplate?--

!--?解释一下hibernateTemplate:hibernateTemplate提供了很多方便的方法,在执行时自动建立?HibernateCallback?对象,例如:load()、get()、save、delete()等方法。?--

bean?id="hibernateTemplate"??

??class="org.springframework.orm.hibernate3.HibernateTemplate"??

???constructor-arg??

????ref?local="sessionFactory"?/??

???/constructor-arg??

/bean

!--?把DAO注入给Session工厂?--

bean?id="userDAO"?class="com.alonely.dao.UserDAO"??

???property?name="sessionFactory"??

????ref?bean="sessionFactory"?/??

???/property??

/bean

!--?把Service注入给DAO?--

bean?id="userService"class="com.alonely.service.UserService"??

???property?name="userDAO"??

????ref?local="userDAO"?/??

???/property??

/bean

!--?把Action注入给Service?--

bean?name="/user"class="com.alonely.struts.action.UserAction"??

???property?name="userService"??

????ref?bean="userService"?/??

???/property??

/bean??

/beans

以上Spring的applicationContext.xml文件我是用的SSH架构,如果您用Spring的MVC架构,其原理也是一样的。

下面补充说一下SSH2的方法:

1.导入其他的配置文件。在ssh项目中可以导入其他的配置文件,导入的格式为:

import?resource="classpath:**/applicationContext-bms-sendxml-quartz.xml"/

这个是导入一个spring的定时任务的配置文件。

2.配置扫描类包并将相应注解纳入spring?bean容器,以及启动注解自动注入:

context:component-scan?base-package="com.XX"?/

3.spring自己的事务管理:

tx:annotation-driven?/

4.配置配置文件路径(.properties):

bean?id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"

property?name="locations"

list

valueclasspath:jdbc.properties/value

valueclasspath:sysconfig.properties/value

/list

/property

/bean

5.配置数据源:

bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close"

property?name="driverClass"

value${c3p0.jdbc.driverClass}/value

/property

property?name="jdbcUrl"

value${c3p0.jdbc.url}/value

/property

property?name="user"

value${c3p0.jdbc.username}/value

/property

property?name="password"

value${c3p0.jdbc.password}/value

/property

!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default:?3?--

property?name="initialPoolSize"

value${c3p0.jdbc.initialPoolSize}/value

/property

!--连接池中保留的最小连接数。?--

property?name="minPoolSize"

value${c3p0.jdbc.minPoolSize}/value

/property

!--连接池中保留的最大连接数。Default:?15?--

property?name="maxPoolSize"

value${c3p0.jdbc.maxPoolSize}/value

/property

!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default:?0?--

property?name="maxIdleTime"

value${c3p0.jdbc.maxIdleTime}/value

/property

!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:?3?--

property?name="acquireIncrement"

value${c3p0.jdbc.acquireIncrement}/value

/property

!--每60秒检查所有连接池中的空闲连接。Default:?0?--

property?name="idleConnectionTestPeriod"

value${c3p0.jdbc.idleConnectionTestPeriod}/value

/property

/bean

bean?id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"

property?name="dataSource"

ref?bean="dataSource"?/

/property

property?name="hibernateProperties"

props

!--?数据库方言?--

prop?key="hibernate.dialect"

${hibernate.dialect}

/prop

!--?显示Hibernate持久化操作所生成的SQL语句?--

prop?key="hibernate.show_sql"false/prop

!--?将SQL脚本进行格式化后再输出?--

prop?key="hibernate.format_sql"false/prop

/props

/property

property?name="packagesToScan"

list

valuecom.avit/value

/list

/property

/bean

java中怎样设置tomcat中conf/context.xml文件解决跨域每次sessionid不一样问题

在tomcat.6.0.27以上版本,跨域问题有简单解决方法:

在tomcat 的conf 目录下面 编辑:context.xml

将里面的Context修改为以下即可:

ContextsessionCookiePath=""sessionCookieDomain=".×××.cn"/

问题得到解决

之前的tomcat6跨域解决方法是自己创建jar包。

找不到“applicationContext.xml”怎么办?

初学Spring在用Resource rs=new ClassPathResource("applicationContext.xml"),出现文件找不到的问题,或者你在new ClassPathResource("F://...//applicationContext.xml"),一样会出现找不到的情况。

原因:

ClassPathResource中找不到applicationContext.xml是因为这个xml文件在建工程的时候默认放在了 WebContent/WEB-INF/下面,但是用ClassPathResource是在当前目录也就是这个java文件所在的目下进行寻找。

解决方法:

方法一:把这个xml文件移动到src目录下面就ok了。

方法二:如果用FileSystemXmlApplicationContext呢,它是根据指定的路径来进行寻找,所以要把路径写完整。现在xml路径在 src文件夹下。那就要写:

ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");

这是比较直接简单的写法。对于FileSystemXmlApplicationContext也可以采用:

1.加上classpath:前缀(这个时候xml要放在当前目录也就是src下)

ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

2.加上file:把路径写全(用这个方法xml可以放任意地方)

ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext ctx=new ClassPathXmlApplicationContext("file:F:/workspace/SpringExercis/src/applicationContext.xml");

(责任编辑:IT教学网)

更多

推荐广告特效文章