为什么Spring Boot在部署WAR时会忽略@Named注释?

时间:2015-11-20 12:11:01

标签: java tomcat spring-boot war

我必须在Tomcat 8中将Spring Boot应用程序部署为WAR。

通过嵌入式Tomcat启动应用程序时,一切正常,但是当我部署到独立的Tomcat时,只有在注释@Component注释(或其衍生产品,如@Service或{ {1}})。如果我使用JSR-330 @Controller注释,则找不到bean,并且容器无法启动应用程序,因为它找不到任何符合接线条件的bean:

@Named

并非所有JSR-330注释都被忽略,因为Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [TestService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 有效。

在像Tomcat这样的独立(非嵌入式)容器中运行时,有没有办法配置Spring Boot以支持JSR-330注释?

相关代码部分: 的pom.xml

@Inject

ContextConfiguration(在根包中):

<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>group</groupId>
    <artifactId>art</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

TestService(在根包中)

import javax.inject.Inject;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@Slf4j
public class ContextConfiguration extends SpringBootServletInitializer{

}

TestRestController(在根包中)

@Named
public class TestService {
}

0 个答案:

没有答案