Use Java code to create a Spring context at some time points, and parse Pring

Source: Internet
Author: User

Use Java code to create a Spring context at some time points, and parse Pring
The previous article describes how to use Spring to implicitly create beans, but when we need to introduce a third-party class library to our logic, @ Conponent and @ Autowired cannot be added to the class. In this case, automatic assembly is not applicable. We need to use Java code to explicitly implement bean. Following the previous CD routine, two interfaces are called MediaPlayer/CompactDisc. The MediaPlayer implementation is CDPlayer, And the CompactDisc implementation is CompactDisc_guangliang/CompactDisc_wangfei/example/CompactDisc_xusong. (The reason for this is to understand the mechanism of multiple use of Spring beans at a time. In general, Spring creates only one object for each bean.) the code can be changed from the previous article, this time, the annotation creation and scanning annotation are removed. Some code is as follows. Other implementations are similar to the following:

Package com. spring. javabeans. cd; import org. springframework. beans. factory. annotation. autowired; public class CDPlayer implements MediaPlayer {private CompactDisc cd; @ Autowired // indicates that Spring should satisfy bean dependencies as much as possible after initialization, here it injects a CompactDisc object public CDPlayer (CompactDisc cd) {this. cd = cd ;}@ Overridepublic void player () {System. out. println ("Play with CD! ");}}

@ Autowired only indicates that Spring injects a required bean to the constructor when it calls the constructor.

Package com. spring. javabeans. cd; public class CompactDisc_guangliang implements CompactDisc {private String title = "fairy tale"; private String artist = "Guang Liang"; @ Overridepublic void play () {System. out. println ("play:" + title + "from artist:" + artist );}}

After @ Component is removed, CompactDisc becomes a common class. Spring is not responsible for this class.

Next, write the configuration class. The Code is as follows:
Package com. spring. javabeans. cd; import org. springframework. context. annotation. bean; // use java code to define the spring Assembly mechanism public class CDPlayerConfig {@ Beanpublic CompactDisc randomBeatlesCD () {int key = (int) Math. floor (Math. random () * 4); if (key = 0) {return new CompactDisc_zhoujielun ();} else if (key = 1) {return new CompactDisc_xusong ();} else if (key = 2) {return new CompactDisc_guangliang ();} else {return new CompactDisc_wangfei () ;}@ Beanpublic CDPlayer cdplayer (CompactDisc cd) {return new CDPlayer (cd );}}

@ Bean indicates that Spring needs to create a bean here.

Similarly, we use one to create a JUnit4 test. The Code is as follows:
package com.spring.javabeans.cd;import static org.junit.Assert.*;import org.junit.runner.RunWith;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=CDPlayerConfig.class)public class CDTest {@Autowiredprivate MediaPlayer player;@Autowiredprivate CompactDisc cd;@Testpublic void test1() {assertNotNull(player);assertNotNull(cd);}@Testpublic void test2(){player.player();cd.play();}}

@ Autowired indicates the dependency injection relationship.

 

 

I love sharing and refuse to come here, and my blog spirit will survive-cvrigo

13:40:56

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.