How to work with Application Context?

I'm new to spring, I wanted to know whether this design ?
Each time a Coupon is used as the spring Application Context ..
Each method call getBeanInstance()- creates an instance of Application Context or it should be one instance for all app type singleton ?

How to use the approach with Application Context to create new instances of the object you want, from zavisimoti which are written in spring.xml ?

getBeanInstance public ApplicationContext(){
 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;
 }
June 27th 19 at 15:24
1 answer
June 27th 19 at 15:26
Solution
The right to create context at the start of the application and does not destroy it altogether. And I see no reason to work with factory bins directly. This leads to unnecessary movements and increases the connectivity. Use declarative management of beans and dependency injection.
"This leads to unnecessary movements and increases connectivity" - can you clarify what you have in mind here ? - stephon_Abshire commented on June 27th 19 at 15:29
player is the idea of Spring - design loosely coupled code where you can override the implementation of almost any part and the app will not even notice it. Ideally, loosely coupled application can even be run in the container is not Spring, and CDI. Yes, and much easier to write
@Inject
Coupon coupon;

than
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);
- samir39 commented on June 27th 19 at 15:32
: Do not understand one moment , how can I put data coming from the request with this design , I used to create manually an instance of and pass back data through setters or the constructor , but then you wrote :

@Inject 
Coupon coupon;


That will create an instance of this is understandable but how it is initialized with the data ? Can write a simple code as an example , a request comes in with data and I use Spring injection to create an instance with these data and transfer to database ? Just do not understand this point or give a link to a code example of SETI ? - stephon_Abshire commented on June 27th 19 at 15:35
: I highly recommend you to read the book Spring in Action. It is in Russian. The concept of DI and AOP is a topic difficult to understand by a novice, and architecture of applications based on these concepts even more. Therefore, it will be difficult to master them in an article in the network, it requires a systematic approach in the presentation of the material. - samir39 commented on June 27th 19 at 15:38
: Yes of course the approach is different , not the usual ! Thank you! - stephon_Abshire commented on June 27th 19 at 15:41
: Another question for you ... the Book is excellent already 130 pages .. any advice on a systematic approach to spring ? - stephon_Abshire commented on June 27th 19 at 15:44
: hands to type every code example from the book and experiment with it. - samir39 commented on June 27th 19 at 15:47

Find more questions by tags Java