Spring assembly
automated assembly
1 ) Component assembly @Component , Autowirring
2 ) Autowire @Configuration @ComponentScan Component
Interface creation :
package com.bean;
public interfaceTest_Auto_player {
void player();
}
Interface implementation:
package com.bean;
import org.springframework.stereotype.Component;
// You can name the bean scanned by the component , for example: ,, //@Component("Test_Auto_player") or: //@Name("Test_Auto_player")
@Component// Define the component here
public classTest_Auto_playerImpl implements Test_Auto_player {
privateStringtitle="title";
privateStringcontent="content";
public void player() {
System.out.println(title+"content****:"+content);
}}
Scan package:
package com.bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
/**
* Display configuration (reason: component scanning is not enabled)
* @ComponentScan : will scan the current package by default
*/
@Configuration
@ComponentScan
public classTest_AutoConfig {
}
test:
package com.bean;
import staticorg.junit.Assert.*;
import org.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Test_AutoConfig.class)
public class AutoTest{
@Autowired// Inject components. Generally, components in enterprise projects are interfaces. When calling, the direct spring mechanism will directly call the class method corresponding to the implementation of the interface
private Test_Auto_player cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
cd.player();// Call the method directly: it is actually the method implemented by the injection interface
}
}
An error was reported during the test:
The reason is the lack of jar package: commons-logging
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:91)
atsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
atjava.lang.reflect.Constructor.newInstance(Unknown Source)
atorg.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
atorg.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
atorg.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
atorg.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
atorg.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
atorg.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by:java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
atjava.net.URLClassLoader$1.run(Unknown Source)
atjava.net.URLClassLoader$1.run(Unknown Source)
atjava.security.AccessController.doPrivileged(Native Method)
atjava.net.URLClassLoader.findClass(Unknown Source)
atjava.lang.ClassLoader.loadClass(Unknown Source)
atsun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
atjava.lang.ClassLoader.loadClass(Unknown Source)
...18 more*/
Use the xml scanning component:
test_autoConfig.xml
<context:component-scanbase-package="com.bean"></context:component-scan>
@ContextConfiguration("classpath:test_autoConfig.xml")