Spring框架/配置设置

1 :Spring

1.概念

	Spring是一款轻量级的框架,基于DI/IOC 于AOP的一款容器框架
		DI:依赖注入
		IOC:控制反转(和DI差不多)
		AOP:面向切面
		容器:Spirng,


2:Spring.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:mvc="http://www.springframework.org/schema/mvc"
	 xsi:schemaLocation="
	 http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context.xsd
	 http://www.springframework.org/schema/mvc
	 http://www.springframework.org/schema/mvc/spring-mvc.xsd
	 " >
	 
	 	<!--1 扫描包  -->
		<context:component-scan base-package="cn.pan.cms"></context:component-scan>	
		
	 	<!--2 读取配置文件  -->
	 	<context:property-placeholder location="classpath:jdbc.properties"/>
		
		<!--3,加载DataSource对象,配置4金刚  -->
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
			<property name="driverClassName" value="${jdbc.driverClassName}" />
			<property name="url" value="${jdbc.url}" />
			<property name="username" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
		</bean>
		<!--4,配置JdbcTemplate操作数据库  -->
		<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
			<property name="dataSource" ref="dataSource" />
		</bean>
</beans>

3:SpringMVC.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:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 " >
 <!--SpringMVC 5大组件  -->
 
<!--  	引入其他Sprng文件
 	<import resource="classpath:applicationContext.xml"/> -->
 	
 	<!--1,扫描包  -->
	<context:component-scan base-package="cn.pan.cms"></context:component-scan>
	
 	<!--2,注解生效 ,支持特有SpingMVC注解-->
	<mvc:annotation-driven />
	<!--3,支持静态资源  -->
	<mvc:default-servlet-handler />
	<!--4,视图解析器  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 	prefix:前缀 -->
		<property name="prefix" value="/WEB-INF/views/" />
		<!-- 	suffix后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!--5,上传解析器  -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<value>2000000000</value>
		</property>
	</bean>
</beans>

4:jdbc.properties配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///cms
jdbc.username=root
jdbc.password=XXXXX

5:web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>sping02</display-name>
	
	<!--0 监听器  -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
	</listener>
	
	
	<context-param>
		 <param-name>contextConfigLocation</param-name>
		 <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!--1,核心控制器  -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!--2,Spring核心文件路径  -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-mvc.xml</param-value>
		</init-param>
	<!--3 跟随启动创建  -->
			<load-on-startup>1</load-on-startup>
	</servlet>
	<!--4,过滤器  -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!--5,请求乱码!  -->

		<filter>
			<filter-name>CharacterEncodingFilter</filter-name>
			<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
			<init-param>
				<param-name>encoding</param-name>
				<param-value>utf-8</param-value>
			</init-param>
		</filter>
			 
		<filter-mapping>
			<filter-name>CharacterEncodingFilter</filter-name>
			<url-pattern>/*</url-pattern>
		</filter-mapping>
</web-app>

注意事项:

规范命名:
applicationContext-mvc.xml
applicationContext.xml
jdbc.properties

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值