W
W
wolf-98302016-09-28 20:24:32
Java
wolf-9830, 2016-09-28 20:24:32

How to get bean in spring?

I master the configuration of a spring application through annotations, I try to create a bean like this:

@Configuration
class Service{
    @Bean(name="service")
    public Service service(){
        return new Service();
    }
    public double getDouble(){
        return 01.02;
    }
}

And I call accordingly:
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Service service = (Service)context.getBean("service");

The compiler swears at getBean, what's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2016-09-28
@kuftachev

I'm confused by the ClassPathXmlApplicationContext if the configuration is via annotations.
In theory, AnnotationConfigApplicationContext should help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question