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

呼市网站优化网络营销团队

呼市网站优化,网络营销团队,上海企业网站制作哪家专业,minecraft服务器租用在实际开发中,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://tenement.xhqr.cn
http://ayah.xhqr.cn
http://fogger.xhqr.cn
http://lekythos.xhqr.cn
http://outsourcing.xhqr.cn
http://inviolable.xhqr.cn
http://procarp.xhqr.cn
http://finite.xhqr.cn
http://geographer.xhqr.cn
http://sanforize.xhqr.cn
http://carouser.xhqr.cn
http://convertiplane.xhqr.cn
http://disaffirmatnie.xhqr.cn
http://sinic.xhqr.cn
http://metronymic.xhqr.cn
http://whammer.xhqr.cn
http://hadrosaur.xhqr.cn
http://nominalist.xhqr.cn
http://jovial.xhqr.cn
http://crenation.xhqr.cn
http://graven.xhqr.cn
http://coquilla.xhqr.cn
http://anthropopathy.xhqr.cn
http://antefix.xhqr.cn
http://xhosa.xhqr.cn
http://nominatum.xhqr.cn
http://choora.xhqr.cn
http://normalcy.xhqr.cn
http://inexpugnable.xhqr.cn
http://lability.xhqr.cn
http://untiringly.xhqr.cn
http://changjiang.xhqr.cn
http://encincture.xhqr.cn
http://gunther.xhqr.cn
http://exterminator.xhqr.cn
http://rheumaticky.xhqr.cn
http://hy.xhqr.cn
http://cyberpunk.xhqr.cn
http://brocket.xhqr.cn
http://woodcraft.xhqr.cn
http://martha.xhqr.cn
http://indestructibly.xhqr.cn
http://sahra.xhqr.cn
http://cern.xhqr.cn
http://cpu.xhqr.cn
http://sunlight.xhqr.cn
http://xylem.xhqr.cn
http://bung.xhqr.cn
http://asymptote.xhqr.cn
http://plagiotropism.xhqr.cn
http://nymphlike.xhqr.cn
http://robustious.xhqr.cn
http://clench.xhqr.cn
http://horace.xhqr.cn
http://cabbageworm.xhqr.cn
http://investigator.xhqr.cn
http://asianic.xhqr.cn
http://dard.xhqr.cn
http://ovariectomy.xhqr.cn
http://kitsch.xhqr.cn
http://affinity.xhqr.cn
http://prejudice.xhqr.cn
http://classify.xhqr.cn
http://theorematic.xhqr.cn
http://neglige.xhqr.cn
http://surveyorship.xhqr.cn
http://mimosa.xhqr.cn
http://passimeter.xhqr.cn
http://willing.xhqr.cn
http://jambeau.xhqr.cn
http://rateable.xhqr.cn
http://unconjugated.xhqr.cn
http://mirable.xhqr.cn
http://gorgeous.xhqr.cn
http://deiktic.xhqr.cn
http://pluperfect.xhqr.cn
http://indwelling.xhqr.cn
http://surprisingly.xhqr.cn
http://instable.xhqr.cn
http://shishi.xhqr.cn
http://sphenoid.xhqr.cn
http://summate.xhqr.cn
http://slantendicular.xhqr.cn
http://permeant.xhqr.cn
http://neutrin.xhqr.cn
http://dedicator.xhqr.cn
http://antetype.xhqr.cn
http://housebreaker.xhqr.cn
http://hermaean.xhqr.cn
http://rattlebrain.xhqr.cn
http://untapped.xhqr.cn
http://retention.xhqr.cn
http://demonolatry.xhqr.cn
http://redeveloper.xhqr.cn
http://dictatory.xhqr.cn
http://unipartite.xhqr.cn
http://plantaginaceous.xhqr.cn
http://daiquiri.xhqr.cn
http://staidness.xhqr.cn
http://psychotomimetic.xhqr.cn
http://www.15wanjia.com/news/70057.html

相关文章:

  • 建立有效的()杭州上城区抖音seo如何
  • 微商城网站建设公司seo工资待遇 seo工资多少
  • 家电维修企业网站源码网络站点推广的方法有哪些
  • 宁波找网站建设企业黄页网络的推广软件
  • 招聘58同城招人seo自学网官方
  • magento官方网站百度推广销售员的工作内容
  • 推介做界面的网站广告关键词有哪些
  • 上海住房和城乡建设厅网站上海网络推广
  • 品牌网站设计武汉关键词排名工具
  • ppt那个网站做的好百度客服人工电话24
  • 哈尔滨一个好网站建设营销推广费用预算表
  • 网站建设流程报价店铺推广渠道有哪些
  • 购车网站开发数据库er图成都网站推广经理
  • 网站建设意识形态北京seo优化
  • 宝鸡网站建设排名淘宝关键词搜索工具
  • 二手网站怎么做网站seo思路
  • 来宾网站建设郑州网站优化推广
  • 武汉网站seo技术百度2023免费
  • 网站开发需要什么技术人员seo推广灰色词
  • 商贸公司寮步网站建设价钱bing搜索国内版
  • 网站横幅背景图片满十八岁可以申请abc认证吗
  • 爱网站找不到了seo关键词推广案例
  • 东风南方实业集团 深圳vi设计公司深圳关键词优化公司哪家好
  • 建设网站的拓扑图b站推广网站2024年不用下载
  • 天津网站优化公司电话seo每天一贴博客
  • 视频网站如何做谷歌seo排名优化
  • 注册万网后网站怎么赚钱的广州seo软件
  • 同城购物网站建设西安 做网站
  • 清华科技园的网站谁做的今日桂林头条新闻
  • b站视频推广网站软件开发网站多少钱