0

I have application that look like below

without spring (prior)

UI-> service --> javabean

p.s: my ui call service (not using DI) and i want to remain this way

new service()

I want my javabean to do DI for certain bean from applicationcontext.xml file. Should i use Applicationcontext.getBean(the ..xml) file in javabean or is there any better way to do it without changing the service layer and only modify the javabean in bold?

1 Answer 1

5

You should look at @Configurable annotation (Tutorial).

The basic idea is that you add this annotation to your bean class. It will then inject any properties as soon as you create a new instance:

@Configurable
public class Person {
    private IPeopleDAO _dao;

    // SNIP

    public save() {
        _dao.save(this);
    }
}

// use it like this
new Person("john", "doe").save();
Sign up to request clarification or add additional context in comments.

2 Comments

With the very large caveat that it uses load-time weaving, which only works in some classloader environments.
alternatively can use compile time weaving, but i havent found way to do it on netbeans without maven.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.