当前位置: 首页 > news >正文

最权威的做网站的公司哪家好网络营销的目的和意义

最权威的做网站的公司哪家好,网络营销的目的和意义,wordpress采集到草稿,做包装设计的网站在实际开发中,SpringMVC 负责扫描和加载 Controller 层的 Bean 对象,而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么,如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢?为了解决这个问题&a…

在实际开发中,SpringMVC 负责扫描和加载 Controller 层的 Bean 对象,而业务层和数据层等其他模块的 Bean 则由 Spring 框架负责扫描和加载。那么,如何控制 Spring 仅加载除了 Controller 层之外的其他 Bean 呢?为了解决这个问题,通常可以采取以下两种方案,来避免 Spring 错误地加载 SpringMVC 的 Bean:

  • 直接指定:在 Spring 的配置中精确设定扫描范围,只加载特定的包,例如 service 包和 dao 包。
  • 过滤排除:将 Spring 的扫描范围设定为 com.it(假设 groupId 为 com.it),并排除 controller 包内的 Bean。

当然,在实际开发中,有时也会选择不区分 Spring 和 SpringMVC 的环境,将它们加载到同一个环境中。这里创建一个入门版的 SpringMVC 项目进行演示,创建过程可以参考 SpringMVC 快速入门。假设,现在的 SpringMVC 配置类和启动配置类如下:

@Configuration
@ComponentScan("com.it.controller")
public class SpringMvcConfig {
}
public class InitConfig extends AbstractDispatcherServletInitializer {// 用于加载 SpringMVC 容器配置@Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}// 设置哪些请求归属 SpringMVC 处理@Overrideprotected String[] getServletMappings() {// 设置所有的请求归属 SpringMVC 处理return new String[]{"/"};}// 加载 Spring 容器配置@Overrideprotected WebApplicationContext createRootApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(SpringConfig.class);return ctx;}
}

方式 1:直接指定

在 Spring 配置类中,通过 @ComponentScan 直接指定多个待扫描模块:

@Configuration
@ComponentScan({"com.it.service", "com.it.dao"})
public class SpringConfig {
}

方式 2:过滤排除

在 Spring 配置类中,通过 @ComponentScan 排除不扫描模块:

@Configuration
@ComponentScan(value = "com.it",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)
)
public class SpringConfig {
}

这里按照注解类型排除不需要扫描的模块,说明一下:@ComponentScan 存在两个重要的属性:

  • excludeFilters:排除扫描路径中加载的 Bean,需要指定类别(type)与具体项(classes)
  • includeFilters:加载指定的 Bean,需要指定类别(type)与具体项(classes)

测试

在 com.it 下创建一个 App 类,观察运行后是否能获取到 Controller 层 Bean:

public class App {public static void main(String[] args) {AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);System.out.println(ctx.getBean(UserController.class));}
}

测试前,需要将 SpringMvcConfig 类上的 @Configuration 注解先注释掉,因为 SpringConfig 在设置扫描 com.it 包时,如果包中某个类添加了 @Configuration 注解,那么这个类被扫描的同时,该类 @ComponentScan 指定的 com.it.controller 也会被扫描,这样会影响 Bean 加载控制的测试效果。

优化

在实际开发中,可以优化 InitConfig 类中加载 Spring 和 SpringMVC 容器配置的过程。通过继承 AbstractDispatcherServletInitializer 类的子类,使得配置过程更加简洁:

public class InitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}
}
http://www.mmbaike.com/news/37739.html

相关文章:

  • 北京网站建设解决方案百度竞价排名服务
  • 在vs2010里怎么做网站昆明seo网站管理
  • 跑腿个人网站怎么做广州十大营销策划公司
  • 佛山最好的网站建设牛排seo系统
  • 如何做网站系统个人博客登录首页
  • 网站建设托管优化大师手机版
  • wordpress主题knowseo 培训教程
  • 经典网站设计信息发布网站有哪些
  • 自己做的网站可以运营不百度app广告
  • ae射频电源成色汕头seo外包公司
  • 和网站建设相关的行业百度搜索引擎原理
  • 网站程可以自己做吗引流推广平台
  • 黄冈做网站公司2022近期时事热点素材摘抄
  • 新公司刚成立做网站今晚日本比分预测
  • 南昌网站外包成都seo培
  • 企业网站必备模块廊坊关键词排名优化
  • 怎么做一家网站公司网站模版
  • 小程序开发语言seo评测论坛
  • 天津城市建设招标网站制定营销推广方案
  • 做的网站怎么样才能再网上看到今日国内新闻头条大事
  • 可以货代从哪些网站开发客户全网推广平台有哪些
  • 做网站公司郑州郑州的网站建设公司哪家好北京网站建设
  • 传奇手游变态版本互联网seo是什么意思
  • o2o网站建设信息网站综合查询工具
  • 做网站还得买域名吗西安百度关键词优化排名
  • 公安局网站不备案google官方下载app
  • 做文交所的都有哪些网站竞价推广账户竞价托管费用
  • 网站建设合同的性质江北seo页面优化公司
  • 蚌埠哪有做网站的在线数据分析工具
  • wordpress全站登陆可见深圳关键词优化怎么样