簡體   English   中英

如何從主類訪問Spring Bean

[英]How to access Spring beans away from main class

到目前為止,我所走過的大多數教程都像這樣從主類訪問bean:

public class JdbcDemo {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        SimpleJdbcDaoImpl dao = ctx.getBean("simpleJdbcDaoImpl", SimpleJdbcDaoImpl.class);

        System.out.println("Circle count from main class is: " + dao.getCircleCount());
    }
}

和SimpleJdbcDaoImpl類:

public class SimpleJdbcDaoImpl extends SimpleJdbcDaoSupport {

    public int getCircleCount() {
        String sql = "SELECT COUNT(*) FROM CIRCLE";
        return this.getJdbcTemplate().queryForInt(sql);
    }
}

要通過從主類中調用該bean來訪問它,我必須付出很多努力,例如:

主要類別:

public class JdbcDemo {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        System.out.println(ctx.getBean(SomeOtherClass.class).accessSimpleJdbcDaoImpl());
    }
}

然后在其他一些類SomeOtherClass

@Service
public class SomeOtherClass implements ApplicationContextAware {

    private ApplicationContext ac;

    public String accessSimpleJdbcDaoImpl() {
        return "We're getting into some other secondary class.\n" + ac.getBean(SecondaryClass.class).printSecCount();
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        this.ac = ac;
    }
}

然后在SecondaryClass

@Service
public class SecondaryClass {

    @Autowired
    private SimpleJdbcDaoImpl simpleJdbcDaoImpl;

    public String printSecCount() {
       return "Secondary class Circle count is: " + simpleJdbcDaoImpl.getCircleCount();
    }

}

這似乎使項目與Spring的聯系過於緊密。 另外,在我的項目中,有些類僅在某些類中需要,因此從主調用它們是沒有必要的,或者就是行不通的。

是否有辦法實現ApplicationContextAware而不是從主類調用它們?

更新:

如何從下面的SecondaryClass訪問類AwayTest.printAwayTest()

@Component
public class AwayTest {

    public String printAwayTest() {
        return "Away here\n";
    }

}

這什么也沒給。

@Service
public class SecondaryClass {

    @Autowired
    private AwayTest awayTest;

    private SimpleJdbcDaoImpl simpleJdbcDaoImpl;

    @Autowired
    public void setSimpleJdbcDaoImpl(SimpleJdbcDaoImpl impl) {
        simpleJdbcDaoImpl = impl;
    }

    public String printSecCount() {
        return "Away Test: " + awayTest.printAwayTest() + ". Secondary class Circle count is: " + simpleJdbcDaoImpl.getCircleCount();
    }

}

只需為依賴項注入創建適當的setter。 如果需要SimpleJdbcDaoImpl的實例,則不應聲明私有的自動裝配字段。 而是創建一個setter或構造函數參數。 它將消除對使用反射設置字段的依賴。

@Service
public class SomeOtherClass {
    private SecondaryClass secondary;

    @Autowired
    public void setSecondary(SecondaryClass secondary) {
        this.secondary = secondary;
    }

    public String accessSimpleJdbcDaoImpl() {
        return "We're at Some Other Class: " + secondary.printSecCount();
    }
}

@Service
public class SecondaryClass {

    private SimpleJdbcDaoImpl simpleJdbcDaoImpl;

    @Autowired
    public void setSimpleJdbcDaoImpl(SimpleJdbcDaoImpl impl) {
        simpleJdbcDaoImpl = impl;
    }

    public String printSecCount() {
       return "Secondary class Circle count is: " + simpleJdbcDaoImpl.getCircleCount();
    }
}

我認為您可以直接自動連接私有屬性,而無需使用setter和getter方法。 根據我的理解,以下代碼是正確的。 我的建議是:檢查配置,確保AwayTest和SecondaryClass都在以下component-scan base-pakcage中:

<context:component-scan base-package="xxx.xxx.xx" />

您的代碼:

@Service public class SecondaryClass {

    @Autowired
    private AwayTest awayTest;

    private SimpleJdbcDaoImpl simpleJdbcDaoImpl;
    .....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM