Java – what is the smart way to organize classes for component scanning in spring 3?

I've started to develop a new project using spring 3, and I'm using annotations I like that I can connect my classes to inject dependencies, but I know that context is a bad practice: component scanning starts from the basic package

I'm using the dispatcher servlet, which has its own XML configuration file This is also a context: component scanning When I first started learning spring, there was overlap in my component scan and beans were created many times I want to avoid this situation

What is a good way to organize my package or component scans to cover all beans without duplication?

At present, I have such bags:

my.package.controller
my.package.dao
my.package.entity
my.package.service
my.package.util

If I have beans in all these packages, it seems that the easiest way is to place < context: component scan base package = "my. Package" > < / context: component scan > into ApplicationContext XML and complete it

Scan the scheduler's XML for my package. Controller and ApplicationContext Would the rest of the XML (excluding my. Package. Controller) be better?

Or should I arrange all annotated courses in one area and others in another area? It's like:

my.package.spring.controller
my.package.spring.dao
my.package.spring.entity
my.package.spring.service
my.package.spring.util
my.package.notannotated
my.package.notannotated2

I'm using @ Autowired to add logging to most (if not all) classes, so I don't know I'll have any classes that won't be annotated

I hate getting stuck in configuration... I'd rather be stuck in code, so if someone can provide any hint, I'd be happy to welcome them

thank you!

Solution

Yes – scan everything except the controller in the primary context

<context:component-scan base-package="my.package">
    <context:exclude-filter type="regex" expression="my\.package\.controller.*"/>
 </context:component-scan>

In the context of dispatcher servlet, only controller packages are scanned

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>