Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 976729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:47:37+00:00 2026-05-16T03:47:37+00:00

For a rich domain driven design I want to use Guice dependency injection on

  • 0

For a rich domain driven design I want to use Guice dependency injection on JPA/Hibernate entity beans. I am looking for a similar solution as the Spring @configurable annotation for non-Spring beans.

Does anybody know of a library? Any code examples?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:47:37+00:00Added an answer on May 16, 2026 at 3:47 am

    You can do this with AspectJ.

    Create the @Configurable annotation:

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public @interface Configurable {
    }
    

    Create an AspectJ @Aspect similar to this:

    @Aspect
    public class ConfigurableInjectionAspect {
        private Logger log = Logger.getLogger(getClass().getName());
    
        @Pointcut("@within(Configurable) && execution(*.new(..)) && target(instantiated)")
        public void classToBeInjectedOnInstantiation(Object instantiated) {}
    
        @After(value = "classToBeInjectedOnInstantiation(instantiated)", 
               argNames = "instantiated")
        public void onInstantiation(Object instantiated) {
            Injector injector = InjectorHolder.getInjector();
            if (injector == null) {
                log.log(Level.WARNING, "Injector not available at this time");
            } else {
                injector.injectMembers(instantiated);
            }
        } 
    }
    

    Create (and use) a holding class for your injector:

    public final class InjectorHolder {
    
        private static Injector injector;
    
        static void setInjector(Injector injector) {
            InjectorHolder.injector = injector;
        }
    
        public static Injector getInjector() {
            return injector;
        }
    }
    

    Configure META-INF/aop.xml:

    <aspectj>
        <weaver options="-verbose">
            <include within="baz.domain..*"/>
            <include within="foo.bar.*"/>
        </weaver>
        <aspects>
            <aspect name="foo.bar.ConfigurableInjectionAspect"/>
        </aspects>
    </aspectj>
    

    Start your VM with aspectjweaver:

    -javaagent:lib/aspectjweaver.jar
    

    Annotate your domain classes:

    @Entity
    @Table(name = "Users")
    @Configurable 
    public class User {
        private String username;
        private String nickname;
        private String emailAddress;
        @Inject
        private transient UserRepository userRepository
    
        public User() {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring MVC application trying to use a rich domain model, with
I use rich domain model in my app. The basic ideas were taken there
Is it possible to use DDD and a rich domain model if your application
On the Domain-Driven Design website I see: Concentrate all the code related to the
Despite having studied Domain Driven Design for a long time now there are still
I am developing a rich client application that will use the Entity Framework (with
I want to be clear on this. When I say domain anaemia, I mean
i'am writing some swing applications(not rich clients, running standalone) with a lot of domain
Our current O/RM tool does not really allow for rich domain models, so we
We use rich:orderingList with the following attributes listHeight=auto listWidth=auto, this causes it to auto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.