如何从主类访问Spring bean

时间:2014-07-29 16:24:05

标签: java spring

我到目前为止大多数教程都是从主类中访问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。另外在我的项目中有一些我只在某些类中需要的bean,因此不需要从main调用它们,或者只是不起作用。

有没有办法实现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();
    }

}

2 个答案:

答案 0 :(得分:1)

为依赖注入创建适当的setter。如果您需要SimpleJdbcDaoImpl的实例,则不应声明私有自动装配字段。而是创建一个setter或constructor参数。它将消除对Spring的依赖,使用反射来设置字段。

@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();
    }
}

答案 1 :(得分:1)

我认为您可以直接自动装配私有财产,而无需使用setter和getter方法。根据我的理解,您的代码下方是正确的。我的建议是:检查你的配置,确保AwayTest和SecondaryClass都在以下组件扫描base-pakcage中:

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

您的密码:

@Service public class SecondaryClass {

    @Autowired
    private AwayTest awayTest;

    private SimpleJdbcDaoImpl simpleJdbcDaoImpl;
    .....