简体   繁体   中英

Spring ioc interface and implementation in different package

I have two packages

  • com.xxx.A contains a interface - > persistence.

  • com.xxx.B contains several impls for persistence,

    • etc. mySqlPersistence, hibernatePersistence.

My question is when I using spring IOC to switch different implementation, how can I config them? by the way I using constructor inject..

NOTE: Currently,

  • In package "A" I have a repository to use persistence interface.
  • The implementation in package "B"
  • since the impl meed operate domain which belongs Package "A" then Package "B" depend on package "A".

where the config file should be?

First time to touch java spring, thanks for help....

This can be done by setting below in web.xml/configuration.xml:

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>hibernatePersistence</param-value>
</context-param>
<!--context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>mySqlPersistence</param-value>
</context-param-->

The components which are related to hibernate can be annotated as:

@ActiveProfiles("hibernatePersistence")

And similarly you can have components for "mySqlPersistence"

If you need mySqlPersistence to be active remove the comments and comment "hibernatePersistence" one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM