M
M
Maks Burkov2017-04-07 15:54:38
Java
Maks Burkov, 2017-04-07 15:54:38

How to work with Application Context correctly?

I'm new to spring, I wanted to know if this construction is correct?
Every time a Coupon is created, the Application Context spring will be used.
Each time the -getBeanInstance()- method is called, will an Application Context instance be created or should it be one instance for the entire singleton application ?
What is the correct way to use the Application Context approach to create new instances of the object that is required, from the dependencies that are written in spring.xml ?

public ApplicationContext getBeanInstance(){
        return new ClassPathXmlApplicationContext("spring.xml");
    }

private static Coupon couponCreator(Company company , String message , String title, int amount,
                                        long id , String image , double price , CouponType type  , Date start_date , Date end_date){
        Coupon coupon = couponBeanFactory.getBeanInstance().getBean("coupon", Coupon.class);
        coupon.setCompany(company);
        coupon.setMessage(message);
        coupon.setAmount(amount);
        coupon.setId(id);
        coupon.setType(type);
        coupon.setTitle(title);
        coupon.setImage(image);
        coupon.setPrice(price);
        coupon.setEndDate(end_date);
        coupon.setStartDate(start_date);
        return coupon;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-07
@Maks00088

It is correct to create a context at the start of the application and not destroy it at all. And I see no reason to work with the bean factory directly. This leads to unnecessary body movements and increases connectedness. Use declarative bean management and dependency injection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question