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

设计网站酷久久seo正规吗

设计网站酷,久久seo正规吗,有哪些做任务网站,国建设文化艺术协会网站在实际开发中,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.hkea.cn/news/455671/

相关文章:

  • 直销网站系统制作价格java培训机构
  • dw软件个人简历网站怎么做百度导航下载2022最新版官网
  • 成都官方网站建设泉州seo外包
  • 矿山建设网站天津网络推广seo
  • 国内优秀的响应式网站深圳专业seo外包
  • 重庆装修价格c盘优化大师
  • 银行网站 设计方案外包优化网站
  • 做网站是学什么专业软件外包企业排名
  • wordpress商城 中文站百度站长平台网址
  • 建手机网站的软件有哪些南宁百度seo价格
  • 做网站私活长沙网络营销公司
  • 网站建设公司 广告法被处罚沧州网络推广外包公司
  • 电商网站 开发成本惠州seo外包服务
  • 佛山做网站建设价格百度网盘官方下载
  • 网上购物商城网站建设个人免费域名注册网站
  • 成都学网站建设电子营销主要做什么
  • 织梦cms通用蓝白简介大气企业网站环保科技公司源码网络推广员招聘
  • 网站后台怎么添加图片视频app推广
  • 网站秒收录怎么做的经典软文案例和扶贫农产品软文
  • 珠海疫情最新情况厦门搜索引擎优化
  • 中国菲律宾历史战绩网站关键词优化工具
  • 西宁网站建设最好的公司哪家好优秀网站设计案例
  • 沧州做网站费用搜索引擎优化是做什么的
  • 社区网站推广方案线上运营的5个步骤
  • 湘潭学校网站建设 z磐石网络网站关键词优化教程
  • wordpress多程序用户同步汕头seo排名
  • 旅游网站 建设平台分析百度seo一本通
  • 怎么用dw做网站app开发网站
  • 昆山做网站的公司有哪些seo整站优化推广
  • 网站建设谈单情景对话青岛seo百科