配置applicationcontext.xml
SpringIOC:是用标签的形式代替类的创建.
配置:DataSource(spring-jdbc包)连接数据库,获得数据源对象
property属于set/get方法注入
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="username" value="用户名"></property>
<property name="password" value="密码"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/数据库?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"></property>
</bean>
配置factory工厂 获得sqlsessionfactory(spring-mybatis)对象
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property> <!--sqlsessionfactory对象是已经 连接数据库 的sqlsessionfactory对象-->
<property name="typeAliasesPackage" value="com.bjsxt.pojo"></property> <!--设置实体类 使用别名-->
</bean>
配置mapper(spring-mybatis包) bean , 通过sqlsessionfactory创建出的具体的实例化mapper对象(它会扫描mapper层自动创建具体的mapper的bean ,可以在impl层直接获取)
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <!--注入factory工厂 -->
<property name="basePackage" value="com.bjsxt.mapper"></property> <!--扫描mapper层 创建具体的实例化mapper对象-->
</bean>
配置SpringAOP 拓展业务,底层使用JDK动态代理
①前置通知/后置通知/循环通知/异常通知 : 每个通知都需要创建一个对应的类,实现不同的接口并重写方法
②在applicationcontext.xml中配置相应的,
③配置: 配置切点 + 通知bean的ID ==> 形成切面
<bean id="mybefor" class="com.bjsxt.advice.Mybefor"></bean>
<aop:config> 切面
<aop:pointcut id="aop" expression="execution(* com.bjsxt.service.impl.*.*(..))"/> 切点(可以使用通配符 也可以具体到方法)
<aop:advisor advice-ref="mybefor" pointcut-ref="aop"></aop:advisor> 将通知的id注入到切点中
</aop:config>
配置SpringTX(事务对象在spring-jdbc包)
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property><!--配置事务当然 需要连接到数据库 -->
</bean>
<!--配置TX 引入事务对象 并且缩小事务管理范围-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="sel*"/><!--这些方法会执行事务 支持通配符-->
<tx:method name="up*"/>
<tx:method name="in*"/>
<tx:method name="del*"/>
</tx:attributes>
</tx:advice>
<aop:config> <!--TX事务 底层使用的是AOP 所以要配置切点 支持通配符-->
<aop:pointcut id="qd" expression="execution(* com.bjsxt.service.impl.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="qd"></aop:advisor>
</aop:config>
配置springmvc.xml
SpringIOC/SpringAOP/SpringTX/SpringMVC它们分别对应一个自己的注解解析器(想使用注解配置就必须配置它们对应的注解解析器或者理解为注解扫描);
由于我的SpringAOP和SpringTX没有使用注解,所以只需要SpringIOC和SpringMVC的注解解析器就可以了
SpringIOC注解解析器
<context:component-scan base-package="com.bjsxt.controller,com.bjsxt.service.impl"></context:component-scan>
SpringMVC注解驱动 它主要是扫描方法上的@RequestMapping(设置路径) @ResponseBody(设置ajax请求) 等...
<mvc:annotation-driven></mvc:annotation-driven>
由于在web.xml中配置了servlet 由springmvc容器自动创建DispatcherServlet对象并且是全局的,所以需要配置一个静态资源放行,在用户访问静态资源时,不进入Controller层;
配置静态资源放行
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/imgs" location="/imgs/"></mvc:resources>
配置自定义视图解析器(spring-webmvc包),如果项目的目录层次较多或者访问资源在WEB-INF下的安全资源时使用(注意:只能够在后台请求转发页面的时候有效,并且return:’…'不能带有forward,不能说明forward关键字否则也不能生效);
配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--所有请求追加前缀-->
<property name="prefix" value="/WEB-INF/"></property>
<!--所有请求追加后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
配置Web.xml
由于在启动项目的时候自动加载web.xml,所以要在web.xml中指定springmvc.xml和applicationcontext.xml的位置,让程序去扫描它们
<!--指定applicationcontext.xml的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--配置监听器 使用监听器扫描解析applicationcontext.xml 监听器是spring-web包下的-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置servlet springmvc入口 applicationcontext也可以配置在一起 路径用 , 隔开-->
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <!--配置初始化参数 初始化加载springmvc.xml配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!--配置servlet创建优先级-->
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/</url-pattern> <!--全部访问都拦截 都进入DispatcherServlet-->
</servlet-mapping>
<!--配置编码过滤器-->
<filter>
<filter-name>filter</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>filter</filter-name>
<url-pattern>/*</url-pattern> <!--拦截所有访问 同一配置内不能用一样的通配符 所以用/*-->
</filter-mapping>
如果有多个xml文件 如何配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
</param-value>
</context-param>