1. package com.dxz.demo.configuration;
    2.  
    3. import org.springframework.context.annotation.Configuration;
    4.  
    5. @Configuration
    6. public class TestConfiguration {
    7. public TestConfiguration() {
    8. System.out.println("TestConfiguration容器启动初始化。。。");
    9. }
    10. }
  1. APP
    1. 方法1
        1. package com.dxz.demo.configuration;
        2.  
        3. import org.springframework.context.ApplicationContext;
        4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        5.  
        6. public class TestMain {
        7. public static void main(String[] args) {
        8.  
        9. // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
        10. ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        11.  
        12. // 如果加载spring-context.xml文件:
        13. // ApplicationContext context = new
        14. // ClassPathXmlApplicationContext("spring-context.xml");
        15. }
        16. }
        17.  
        18. public static void main(String[] args) {
        19.  
        20. // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext
        21. ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        22.  
        23. //获取bean
        24. TestBean tb = (TestBean) context.getBean("testBean");
        25. tb.sayHello();
        26. }
    2. 方法2
        1. public static void main(String[] args) {
        2. ApplicationContext ctx = new AnnotationConfigApplicationContext();
        3. ctx.register(AppContext.class)
        4. }
  2. webApp
    1. 方法1
        1. <web-app>
        2. <context-param>
        3. <param-name>contextClass</param-name>
        4. <param-value>
        5. org.springframework.web.context.
        6. support.AnnotationConfigWebApplicationContext
        7. </param-value>
        8. </context-param>
        9. <context-param>
        10. <param-name>contextConfigLocation</param-name>
        11. <param-value>
        12. demo.AppContext
        13. </param-value>
        14. </context-param>
        15. <listener>
        16. <listener-class>
        17. org.springframework.web.context.ContextLoaderListener
        18. </listener-class>
        19. </listener>
        20. <servlet>
        21. <servlet-name>sampleServlet</servlet-name>
        22. <servlet-class>
        23. org.springframework.web.servlet.DispatcherServlet
        24. </servlet-class>
        25. <init-param>
        26. <param-name>contextClass</param-name>
        27. <param-value>
        28. org.springframework.web.context.
        29. support.AnnotationConfigWebApplicationContext
        30. </param-value>
        31. </init-param>
        32. </servlet>
        33. </web-app>
    2. 方法2
        1. package com.jt;
        2.  
        3. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
        4.  
        5. public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
        6. @Override
        7. protected Class<?>[] getRootConfigClasses() {
        8. return new Class<?>[] { RootConfig.class };
        9. }
        10.  
        11. @Override
        12. protected Class<?>[] getServletConfigClasses() {
        13. return new Class<?>[] { WebConfig.class };
        14. }
        15.  
        16. @Override
        17. protected String[] getServletMappings() {
        18. return new String[] { "/" };
        19. }
        20.  
        21. }

Spring 梳理 - javaConfig在App和webApp中的应用的更多相关文章

  1. Spring 梳理 - JavaConfig、SPI、SCI、SpringSCI、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer、WebMvcConfigurationSupport

    总结1: SCI:Servlet容器(Tomcat)提供的初始化Servlet容器本身的接口,可替换web.xml SpringSCI:SpringServletContainerInitialize ...

  2. Spring 梳理 - JavaConfig实战(spring MVC)-原创

    目录结构 AppInitializer.java package com.jt; import org.springframework.web.servlet.support.AbstractAnno ...

  3. 利用spring boot创建java app

    利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...

  4. 手机APP有三类:原生APP,WebAPP,HybridApp

    搭建 AngularJS+Ionic+Cordova 开发环境并运行一个demo 目前的手机APP有三类:原生APP,WebAPP,HybridApp:HybridApp结合了前两类APP各自的优点, ...

  5. tomcat中session在两个webapp中实现共享

    现在遇到一个需求就是要求完成简单的单点登录,通过在一个tomcat实例中放置两个webapps应用ROOT应用和CEO应用来完成在ROOT应用登录后,在CEO可以直接使用,而未在ROOT应用登录时,不 ...

  6. 从App业务逻辑中提炼API接口

    2.1 从App业务逻辑中提炼API接口 业务逻辑思维导图 功能-业务逻辑思维导图 基本功能模块关系 功能模块接口UML(设计出API) 在设计稿标注API 编写API文档 2.2 设计API的要点 ...

  7. Spring 使用javaconfig配置

    除了使用xml,spring提供javaconfig配置,下面是简单的例子: 1.声明接口 /** * */ package com.junge.demo.spring.service; /** * ...

  8. Spring 梳理 - filter、interceptor、aop实现与区别 -第二篇

    spring mvc中的Interceptor可以理解为是Spring MVC框架对AOP的一种实现方式.一般简单的功能又是通用的,每个请求都要去处理的,比如判断token是否失效可以使用spring ...

  9. Spring 梳理-跨重定向请求传递数据-Flash

    Spring MVC Flash Attribute 的讲解与使用示例 1. Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/R ...

随机推荐

  1. HTTP head请求

    GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体. PUT: 从客户端向服务器传送的数据取代指定 ...

  2. linux安装redis详细步骤(系统centos 6.4 )

    1.安装redis 进入安装目录下载: cd  /usr/local/redis wget http://download.redis.io/releases/redis-3.0.7.tar.gz 解 ...

  3. 技术改变生活| 免费看VIP视频,屏蔽广告,解锁新姿势!

    说到这个,我就忍不住的要介绍一下今天的主角 Tampermonkey 了.Tampermonkey 是一款免费的浏览器扩展和最为流行的用户脚本管理器,它适用于Chrome, Microsoft Edg ...

  4. 超越Storm,SparkStreaming——Flink如何实现有状态的计算

    流式计算分为无状态和有状态两种情况.无状态计算观察每个独立的事件,Storm就是无状态的计算框架,每一条消息来了以后和前后都没有关系,一条是一条.比如我们接收电力系统传感器的数据,当电压超过240v就 ...

  5. lightoj 1140 - How Many Zeroes?(数位dp)

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  6. codeforces 799 D. Field expansion(dfs+思维剪枝)

    题目链接:http://codeforces.com/contest/799/problem/D 题意:给出h*w的矩阵,要求经过操作使得h*w的矩阵能够放下a*b的矩阵,操作为:将长或者宽*z[i] ...

  7. Play on Words UVA - 10129

    题目: Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has ...

  8. uiautomator2 实现App九宫格解锁

    App九宫格解锁 之前在testerhome社区看见codeskyblue大佬写过一种方法,但是这种办法存在一个弊端,那就是多个点的坐标是写死的,也就是说要是换了部手机,九宫格解锁就行不通了,于是就想 ...

  9. js中的this介绍

    今天跟大家一起简单的来了解一下js中一个有趣的东西,this. 在js中我们用面向对象的思想去编写的时候,各个模块之间的变量就不那么容易获取的到了,当然也可以通过闭包的方式拿到其他函数的变量,如果说每 ...

  10. 【Spring】 AOP Base

    1. AOP概述 2. AOP的术语: 3. AOP底层原理 4. Spring 中的AOP 4.1 概述 4.2 分类 4.3 Spring的传统AOP 针对所有方法的增强:(不带有切点的切面) 带 ...