开发者

spring dependency injection in any java bean

开发者 https://www.devze.com 2022-12-13 19:20 出处:网络
I have application that look like below without spring (prior) UI-> service --> 开发者_开发技巧javabean

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?


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消