How can I limit the scanning of Spring components to just files in my war?

I am using the <w760 component> to search for beans in my application.

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

However, the performance when deploying an application on our intermediate server (JBoss 5 with a large number of applications) is much slower than in development (also JBoss 5, but several applications). Scanning components takes a lot of time. I suspect this is due to a much larger class?

Is there an easy way to get Spring just for finding beans in my war file? those. WEB-INF / classes and WEB-INF / lib? Or is there another solution?

+6
source share
5 answers

Two tips;

  • try to be as specific as possible with the base package (s): you can provide multiple packages in the attribute of the base package, separated by commas.
  • Use filters check documents here . - you can specify annotations that need to be scanned (for example, if you use only @Component), and you can also specify a regular expression that should match the class name.
+3
source

I do not know if you have a solution, but a colleague, and I have a potential solution.

In full disclosure, we are working for Red Hat in the Snowdrop project, which provides support for Spring applications on JBoss.

A potential solution is to use component scanning in the jdoss namespace for snowdrops - https://github.com/snowdrop/snowdrop/tree/CustomBeanScanner

Using this, you can add a custom component scanner to use <jboss:component-scan base-package="foo.bar"/> instead of <context:component-scan base-package="foo.bar"/> <jboss:component-scan base-package="foo.bar"/> .

We know this works, but we don’t know if anyone wants to change their application to use this feature. Is intentional communication the cost of increasing speed?

The code is listed in the branch above for those who try / use.

thanks Joshua

+3
source

You must set the scan options for the path to your package (for example, "com.foo"). However, the number of classes in your class path should not differ much in comparison with development. This will be your WAR plus the server classes, which should be approximately the same. In particular, you will not have classes from other WARs in your class path.

I suspect the server is overloaded. You should get some statistics from this machine: that is, what is CPU usage? Lots of disk I / O? Is paging / rearrangement excessive? Also check your JVM memory usage - maybe it spends a lot of time collecting garbage.

+2
source

What to do if you specify

 <context:component-scan base-package="com.myapp" resource-pattern="/WEB-INF/classes/**/*.class"/> 

or any alternative to this approach?

+2
source

Could you be more specific in the directive of the base package?

0
source

Source: https://habr.com/ru/post/890733/


All Articles