深入学习SSH框架(Spring MVC +Spring FrameWork +Hibernate +Spring Security)《四:Spring Framework配置篇》

如今不会spring框架都不好意出去找工作,足以说明spring的使用是多么的广泛,接下来我们来初步了解一下spring的一些概念。

1.了解应用上下文。

spring Framework容器以一个或多个应用上下文的形式存在,由ApplicationContext接口来表示,一个应用上下文管理着一组bean,执行业务逻辑的Java对象,执行任务、持久化和获取持久化数据、响应HTTP请求等。由spring管理的bean可以自动进行依赖注入、消息通知、定时方法执行、bean验证和执行其他关键的spring服务。一个应用程序至少得有一个应用上下文,一般有一个父上下文和多个子上下文,需要注意的是spring管理的bean都可以访问相同的应用上下文、父亲应用上下文、父亲的父亲应用上下文中的bena,但是不允许访问兄弟或者孩子应用上下文中的bean.

2.ApplicationContext接口

ApplicationContext接口的实现类:
1.ConfigurableApplicationContext接口,它是可配置的。
2.WebApplicationContext和ConfigurableWebApplicationContext接口,一般用于在servlet容器中运行的javaee Web应用程序,他们提供了对底层ServletContext和ServletConfig的访问
3.ClassPathXmlApplicationContext和FileSystemXmlAplicationContext被设计用于在独立运行的应用程序从xml文件加载spring配置,而XmlWebApplicationContext被设计用于在JAVA EE Web应用程序实现相同目标。

4.如果使用java而不是Xml以编程的方式配置,就可以用AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContext,它们可以运行在独立的JavaEE Web和独立的应用程序中。

3.DispatcherServlet派发器Servlet

在Javaee Web程序中,spring将使用派发去servlet来处理web请求,该servlet把进入的请求委托给合适的控制器,并按照需要对请求和响应的实体进行转换。注意:每个dispatherServlet都有自己的上下文,我们可以创建多个dispatherServlet,比较通用的模式是,一个根应用上下文来处理数据访问层和业务逻辑层,一个或多个子上下文来处理web请求(spring mvc)


4.启动spring framework

xml方式配置启动
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>Spring Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rootContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/resource/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <url-pattern>*.jspf</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <scripting-invalid>true</scripting-invalid>
            <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html</default-content-type>
        </jsp-property-group>
    </jsp-config>

    <session-config>
        <session-timeout>30</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
        </cookie-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>

    <distributable />

</web-app>
上面演示一个rootContext根上下文和servletContext上下文,注意 ContextLoaderListener类是启动的关键,因为它实现了servletContextListener

编程式启动:
@SuppressWarnings("unused")
public class Bootstrap implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext container) throws ServletException
    {
        container.getServletRegistration("default").addMapping("/resource/*");

        AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContextConfiguration.class);
        container.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext servletContext =
                new AnnotationConfigWebApplicationContext();
        servletContext.register(ServletContextConfiguration.class);
        ServletRegistration.Dynamic dispatcher = container.addServlet(
                "springDispatcher", new DispatcherServlet(servletContext)
        );
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

@Configuration
@ComponentScan(
        basePackages = "com.wrox.site",
        excludeFilters = @ComponentScan.Filter(Controller.class)
)
public class RootContextConfiguration
{
}
ServletContextConfiguration.class
@Configuration
@EnableWebMvc
@ComponentScan(
        basePackages = "com.wrox.site",
        useDefaultFilters = false,
        includeFilters = @ComponentScan.Filter(Controller.class)
)
public class ServletContextConfiguration
{
}






5.总结

本次学习源代码地址项目源代码
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值