I have successfully run this code. see my code below.
@Service public class Uploader{ @Autowired private UploadTask task; public void display(){ task.run(); task.soso(); } } @Component public class UploadTask implements Runnable{ @Async public void soso(){ System.out.println("Upload task running---"); } public void run(){ System.out.println("Running the class UploadTask---------"); } }
Spring.xml
<mvc:annotation-driven/> <context:component-scan base-package="com.exp" />
Below is my test class
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath*:/META-INF/spring/spring.xml"}) public class TestScope { @Autowired Uploader uploader = null; @Test public void testScope() { uploader.display(); } }
Below is my Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringAsync-Exmp</groupId> <artifactId>SpringAsync-Exmp</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
When I run the test class, it successfully prints sysout in UploadTask.
UPDATE I changed the encoding to run using @EnableAsync. See the modified code below.
Bootloader class
@EnableAsync @Service public class Uploader{ @Autowired private UploadTask uploadTask; public UploadTask getUploadTask() { return uploadTask; } public void setUploadTask(UploadTask uploadTask) { this.uploadTask = uploadTask; } public void display(){ uploadTask.run(); uploadTask.soso(); } }
UploadTask Interface
@Component public interface UploadTask extends Runnable{ @Async public void soso(); public void run(); }
Class UploadTaskImpl
public class UploadTaskImpl implements UploadTask{ public void soso() { System.out.println("Inside the class----"); } public void run() { System.out.println("Inside the class--run--"); } }
All the rest are the same. This time I created UploaderTask as an interface and wrote a new class to implement it. This solves the problem.
I think @EnableAsync converts any component using @Async to a proxy server that implements the same interfaces used by this class of components, so when autwire with a particular class encounters a type conflict between this particular class and the proxy.
source share