【配置Spring Framework】

1.XML配置依赖关系

bean是由Springframework管理,我们自己编写bean,Spring也内建了bean,比如ApplicationContext、ResourceLoader、BeanFactory、MessageSource、ApplicationEventPublisher。

【Servle级别上下文】/WEB-INF/servletContext.xml

<beans>

  <mvc:anntation-driven />     <!-- 作用是 指示Spring使用@RequestMapping @RequestBody @RequestParam @PathParam @ResponseBody --->

  <bean name="greetingServiceImpl" class="com.wrox.GreetingServiceImpl" />

  <bean name="helloController" class="com.wrox.GreetingServiceImpl" >

    <property name="greetingService" ref="greetingServiceImpl" />

  </bean>

</beans>

【@RequestMapping细节】

在声明了DispatcherServlet后,@RequestMapping是相对于Dispatcher的URL-pattern而不一定是web程序根URL。

【上下文细节】

一般的程序都有两个上下文,【根上下文用来容纳业务逻辑类】+【Servlet级别上下文用来容纳控制器类】

【根上下文级别】/WEB-INF/rootContext.xml   声明的bean ,DispatherServlet的应用上下文也会继承所有根上下文的bean。

<beans>

  <bean name="greetingServiceImpl" class="com.wrox.GreetingServiceImpl" / >

</beans>

然后在web.xml部署描述符

<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>

【Spring日志细节】log4j-jcl 支持Spring用Commons Logging打印日志到Log4j。

……………………………………………………………………………………………………………………………………………………………………………………………………

2.混合配置依赖关系 =  组件扫描 + 注解配置。

@Component  组件注解 ,@Controller @Repository @Service   + @AutoWired

【上下文细节目标】根上下文容纳Service、Repository和其他业务逻辑,DispatcherServlet上下文容纳Controller。

/WEB-INF/rootContext.xml 【黑名单】

<beans>

  <context:annotation-config />

  <context:component-scan base-package="com.wrox" >

    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

  </context-component-scan>

</beans>

/WEB-INF/servletContext.xml 【白名单】

<beans>

  <mvc:annotation-driven />

  <context:annotation-config />

  <context:component-scan base-package="com.wrox" use-default-filters="false" />

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

  </context:component-scan>

</beans>

【组件扫描不到的bean】第三方已经编译成Class无法通过添加注解 都可以在配置中 声明bean补充组件扫描扫不到的地方。

……………………………………………………………………………………………………………………………………………………………………

【使用@Configuration配置Spring】XML难于调试、XML无法单元测试(只能集成测试)。   +  @Inject

1.引入外部Properties

@Configuration

@PropertySource({("classpath:com/wrox/config/settings.properties","file:config.properties")})

public class ExampleConfiguration{

  //如果需要访问properties的值

  @Inject Environment environment;

  @Value("my.property.key") String myPropertyValue;  //自动注入

}

2.拆分多个配置

@Configuration

@Import({DatabaseConfiguration.class,ClusterConfiguration.class})

@ImportResource("classpath:com/wrox/config/spring-security.xml")

public class ExampleConfiguration

@Configuration
@ComponentScan(
basePackages = "com.wrox.site",
excludeFilters = @ComponentScan.Filter(Controller.class)
)
public class RootContextConfiguration
{
}
@Configuration
@EnableWebMvc
@ComponentScan(
basePackages = "com.wrox.site",
useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(Controller.class)
)
public class ServletContextConfiguration
{
}
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("/");
}
}

Spring 快速开始 配置Spring Framework的更多相关文章

  1. 在eclipse中,使用spring tool suite配置spring环境

    本人第一次接触spring,在经过一天的努力之后,终于成功配置了spring环境. 使用spring tool suite配置 1.打开eclipse,选择help->Eclipse marke ...

  2. Spring 快速开始 启动Spring

    [启动Spring必须配置] [web.xml部署描述符方式] 1.配置Servlet级别上下文 <servlet> <servlet-name>springDispatche ...

  3. Spring MVC 零配置 / Spring MVC JavaConfig

    1. Spring MVC的核心就是DispatcherServlet类,Spring MVC处理请求的流程如下图所示: 2. Spring MVC中典型的上下文层次 当我们初始化一个Dispatch ...

  4. spring boot所有配置

    转载 http://blog.csdn.net/lpfsuperman/article/details/78287265 # 日志配置# 日志配置文件的位置. 例如对于Logback的`classpa ...

  5. Spring Boot自动配置如何工作

    通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...

  6. 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析

    Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...

  7. Spring全家桶之spring boot(一)

    spring boot框架抛弃了繁琐的xml配置过程,采用大量的默认配置简化我们的开发过程.使用spring boot之后就不用像以前使用ssm的时候添加那么多配置文件了,spring boot除了支 ...

  8. 【Spring Boot&&Spring Cloud系列】Spring Boot中使用数据库之MySql

    对于传统关系型数据库来说,Spring Boot使用JPA(Java Persistence API)资源库提供持久化的标准规范,即将Java的普通对象通过对象关系映射(ORM)持久化到数据库中. 项 ...

  9. Spring Boot系列二 Spring @Async异步线程池用法总结

    1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...

随机推荐

  1. HDU5909Tree Cutting

    题目大意 给定一颗树,每个点有点权,问对于每个m,有多少个联通块的权值异或和为m. 题解 解法1:可以考虑树形dp,设dp[u][i]表示以u为根的子树中u必须选,联通块权值异或值为i的联通块个数. ...

  2. POJ--3190 Stall Reservations(贪心排序)

    这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...

  3. 区块链使用Java,以太坊 Ethereum, web3j, Spring Boot

    Blockchain is one of the buzzwords in IT world during some last months. This term is related to cryp ...

  4. 如何设置 ssh secure shell 支持中文

    只需要设置下/etc/sysconfig/i18n 文件内容如清单 1 所示. 清单 1. 文件内容 1 2 3 LANG="zh_CN.GB18030" SUPPORTED=&q ...

  5. java ee wildfly 批处理 job 工作

    配置批处理job,同时启动两个并行任务processData,syncTableTask,执行往后执行第三个任务job:playDurationTask. xml配置如下: <job id=&q ...

  6. 第三十篇-ToolBar的使用

    效果图: 创建标题栏,将原本的Title隐藏,并在菜单栏中设置选项. 一.拖入一个ToolBar组件,并在res/values/styles.xml中将原本的标题栏隐藏,添加<item name ...

  7. semantic ui框架学习笔记一

    面包屑导航 面包屑导航经常用于多个栏目下的内容管理,是web页面里比较常用的组合.例如: <div class="ui breadcrumb"> <a class ...

  8. Python之黏包

    黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cmd.decode('utf-8'), shell ...

  9. 苹果电脑自带python安装tensorflow一直有问题

    对于有些程序是不支持python3而支持python2的,为了在苹果电脑安装tensorflow, 首先sudo su到管理员权限 export TF_BINARY_URL=https://stora ...

  10. (LIS DP) codeVs 1044 拦截导弹

    题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...