想必用过
Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好)。以至于有的初学者都不知道Spring配置文件的意思,就拿来用了。所以今天我给大家详细解释一下Spring的applicationContext.xml
文件。Ok,我还是通过代码加注释的方式为大家演示:
以下是详解Spring的applicationContext.xml文件代码: <!-- 头文件,主要注意一下编码 -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 建立数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动,我这里使用的是Mysql数据库 --> <property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦! -->
<property name="url">
<value>
jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8
</value>
</property>
<!-- 数据库的用户名 --> <property name="username">
<value>root</value>
</property>
<!-- 数据库的密码 --> <property name="password">
<value>123</value>
</property>
</bean>
<!-- 把数据源注入给Session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 配置映射文件 -->
<property name="mappingResources">
<list>
<value>com/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架构,其原理也是一样的。
分享到:
相关推荐
ApplicationContext.xml 是 Spring 框架中用于配置应用程序的核心配置文件。通过该文件,可以定义 Bean、数据源、Session 工厂、 Hibernate 配置等相关信息,从而实现应用程序的自动装配和依赖注入。 一、XML 声明...
ApplicationContext.xml是Spring框架中的核心配置文件,它是Spring的IOC(Inverse of Control,控制反转)容器的核心组件。该文件用于定义和配置Spring应用程序中的各种Bean,对于Spring应用程序的开发和维护起着至...
Spring applicationContext.xml常见部分属性的使用备注
spring4的“所有jar包”+applicationContext.xml+web.xml====全套
包含applicationContext.xml、db.properties、log4j.properties、spring-mvc.xml文件,方便搭建,省去写代码。。
详解Spring的applicationContext.xml文件代码:
spring+jpa的applicationContext.xml配置
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件4
Spring ApplicationContext.xml配置的12个技巧演示教学.pdf
本篇文章主要介绍了详解spring applicationContext.xml 配置文件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring定时任务必须的包和spring配置说明
applicationContext.xml+hiberniate属性+Session属性+自动代理+拦截器!!!! 上传的限制去除了jar包!
NULL 博文链接:https://mutourenoo.iteye.com/blog/1156865
Spring[applicationContext.xml]灵活性代理
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件6
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件1
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件3
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件5