1、我们在使用springmvc进行配置的时候一般初始化都是在web. 里面进行的,但是自己在使用的时候经常会测试一些数据,这样就只有加载spring-mvc. 的配置文件来实现。为了更方便的使用注解,而不影响具体的实现效果,我今天看到了一个初始化的方式,就是实现ApplicationListener接口

2、导包pom.  

<project  ns="http://maven.apache.org/POM/4.0.0"  ns:xsi="http://www.w3.org/2001/ Schema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.troy</groupId>  <artifactId>springInit</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <dependencies>      <dependency>      <groupId>org.spring work</groupId>      <artifactId>spring-webmvc</artifactId>      <version>3.2.8.RELEASE</version>    </dependency>  </dependencies></project>

3、web. 配置

<?  version="1.0" encoding="UTF-8"?><web-app  ns:xsi="http://www.w3.org/2001/ Schema-instance"  ns="http://java.sun.com/ /ns/javaee" xsi:schemaLocation="http://java.sun.com/ /ns/javaee http://java.sun.com/ /ns/javaee/web-app_2_5.xsd" version="2.5">  <display-name>springInit</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <servlet>      <servlet-name>springmvc</servlet-name>      <servlet-class>org.spring work.web.servlet.DispatcherServlet</servlet-class>      <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:spring/spring-mvc. </param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>springmvc</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping>  <filter>      <filter-name>encodingFilter</filter-name>      <filter-class>org.spring work.web.filter.CharacterEncodingFilter</filter-class>      <init-param>          <param-name>encoding</param-name>          <param-value>UTF-8</param-value>      </init-param>      <init-param>          <param-name>forceEncoding</param-name>          <param-value>true</param-value>      </init-param>  </filter>  <filter-mapping>      <filter-name>encodingFilter</filter-name>      <url-pattern>/</url-pattern>  </filter-mapping></web-app>

4、spring-mvc. 的配置

<?  version="1.0" encoding="UTF-8"?><beans  ns="http://www.spring work.org/schema/beans"      ns:xsi="http://www.w3.org/2001/ Schema-instance"     ns:context="http://www.spring work.org/schema/context"      ns:jdbc="http://www.spring work.org/schema/jdbc"       ns:jee="http://www.spring work.org/schema/jee"      ns:tx="http://www.spring work.org/schema/tx"     ns:aop="http://www.spring work.org/schema/aop"      ns:mvc="http://www.spring work.org/schema/mvc"     ns:util="http://www.spring work.org/schema/util"     ns:jpa="http://www.spring work.org/schema/data/jpa"    xsi:schemaLocation="        http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans-3.2.xsd        http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-3.2.xsd        http://www.spring work.org/schema/jdbc http://www.spring work.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.spring work.org/schema/jee http://www.spring work.org/schema/jee/spring-jee-3.2.xsd        http://www.spring work.org/schema/tx http://www.spring work.org/schema/tx/spring-tx-3.2.xsd        http://www.spring work.org/schema/data/jpa http://www.spring work.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop-3.2.xsd        http://www.spring work.org/schema/mvc http://www.spring work.org/schema/mvc/spring-mvc-3.2.xsd        http://www.spring work.org/schema/util http://www.spring work.org/schema/util/spring-util-3.2.xsd">    <mvc:annotation-driven/>    <context:component-scan  -package="com.troy"/>    <bean class="com.troy.springInit.SpringListener"/></beans>

5、实现类,为了不影响具体想过我谢了两个类来展示具体的操作逻辑
  1)需要初始化的方法

public class Init {    public static void init(){        System.out.println("初始化======================初始化");    }}

  2)spring容器加载bean完成后进行初始化

public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{    public void onApplicationEvent(ContextRefreshedEvent event) {        if (event.getApplicationContext().getParent() == null) {            Init.init();        }    }}

6、这种方式在数据库初始化,获取配置文件等初始化,都很不错不用再通过具体的servlet来手动实现
7、另外想要对bean进行操作,可以参考:http://www.cnblogs.com/ll409546297/p/6433420.html

 

收藏 打印