Xml version 1 with component scanning enabled
There was one for Java before To enable component scanning,
Now let's introduce the xml configuration version. Most of the codes are the same. There is less record player Config file and more audio system xml file, the ApplicationContext type used can be changed once.
I record interface code
package Sound system; public interface I record { void play(); }
package Sound system; import org.springframework.stereotype.Component; /** * * There is no need to explicitly configure the "tianzhida album" Bean, * Because this class uses the ` @ Component 'annotation, * `@Component`Indicates that this class will be used as a component class, * So Spring will take care of things for you. * * However, * * component scanning is not enabled by default * *. */ @Component public class Heaven album implements I record { private String singer = "Fei Yuqing"; private String Album name = "Heaven's great"; public void play() { System.out.println("Play album<" + Album name + "> by " + singer); } }
The specific content of tianzhida album is not important. You should note that it uses @ Component.
@Component indicates that the class will be used as a component class and tells Spring to create beans for this class. There is no need to explicitly configure the tianzhida album Bean, because this class uses the @ Component annotation, so Spring will handle things for you. (it is necessary to enable component scanning, which will be described in [enable component scanning] later)
I media player interface code
package Sound system; public interface I media player { void play(); }
package Sound system; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Record player implements I media player { private I Record record; @Autowired public Record player(I record cd) { this.record = cd; } public void play() { record.play(); } }
If you don't insert (inject) I record into I media player, I media player is not very useful. So, it can be said that I media player depends on I record to complete its mission.
Enable component scanning
Component scanning is not enabled by default. We also need to explicitly configure Spring to command Spring to find classes with @ Component annotation and create beans for them.
If you prefer to use XML to enable component scanning, you can use the < context: component scan > element of the Spring context namespace.
src/main/resources/META-INF/spring / sound system xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="Sound system" /> </beans>
Look at the effect
package Sound system; import org.springframework.context.support.ClassPathXmlApplicationContext; public class play Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "META-INF/spring/Sound system.xml"); I Media player = context.getBean(I media player .class); player.play(); context.close(); } }
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Record player Config.class);
Spring comes with many types of ApplicationContext. The following are some of the things you are most likely to encounter.
Java
- AnnotationConfigApplicationContext
Load ApplicationContext from one or more Java based configuration classes. - AnnotationConfigWebApplicationContext
Load WebApplicationContext from one or more Java based configuration classes.
XML
- ClassPathXmlApplicationContext
Load the ApplicationContext definition from one or more XML configuration files under the classpath, and take the definition file of the application context as the class resource. - FileSystemXmlapplicationcontext
Load ApplicationContext definitions from one or more XML configuration files under the file system. - XmlWebApplicationContext
Load the ApplicationContext definition from one or more XML configuration files under the Web application.
Output:
August 07, 2021 10:12:23 morning org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh information: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7106e68e: startup date [Sat Aug 07 10:12:23 CST 2021]; root of context hierarchy Play the album "heaven" by Fei Yuqing August 07, 2021 10:12:23 morning org.springframework.context.annotation.AnnotationConfigApplicationContext doClose information: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7106e68e: startup date [Sat Aug 07 10:12:23 CST 2021]; root of context hierarchy
For all codes, see:
https://gitee. COM / virhuiai / spring wiring example / tree / Master / Xml version 1 with component scanning enabled
Comparison with Java version: