java - Spring @Configuration 注解 @Autowired 服务为空
问题描述
我正在使用 Spring 配置和 Jersey 配置。
这是我的弹簧配置:
@Component
@Configuration
@EnableScheduling
@EnableAspectJAutoProxy
@EnableTransactionManagement
@ComponentScan(basePackages = { "my.packages" })
public class SpringConfig {
private static final String MESSAGE_SOURCE_BASE_NAME = "i18n/messages";
@Profile(Profiles.APPLICATION)
@Configuration
@PropertySource("classpath:application.properties")
static class ApplicationProperties {
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder(11);
}
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Bean
MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE_BASE_NAME);
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
这是我的服务类和接口:
public interface DAOService {
User register(User user)
}
@Service
public class DAOServiceImpl implements DAOService {
@Override
User register(User user){
return null;
}
}
这是我的球衣资源:
在 ResourceSecurityService 类中,我想将 @Autowire 用于 DAOService 类,但我得到空指针异常,daoService 始终为空。
@Component
@Path("/security")
@Produces({ MediaType.APPLICATION_JSON })
public class ResourceDAOService {
@Autowired
private DAOService daoService ; //<--- here daoService is null;
@POST
@Path("/register")
@Consumes({ MediaType.APPLICATION_JSON })
@Transactional
public Response register(User user, @Context HttpServletRequest request,
@Context HttpServletResponse response) throws AppException {
return Response.status(Response.Status.CREATED).entity(securityService.register(user)).build();
}
我正在使用带有下一个依赖项的 spring 版本 4.3.8.RELEASE:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
解决方案
如果您提供指向您的项目的链接可能会更好。根据我看到的信息:
- 确保你有
jersey-spring4
依赖 - 确保您的 Jersey 版本与集成依赖项兼容
- 检查您的项目配置
我建议您使用带有自动配置的 Spring Boot。
推荐阅读
- javascript - Dropzone 发送 $_FILES 为空
- php - updateOrCreate() >100k 记录
- pdf - PyPDF2:解压缩数据时出现错误 -5:流不完整或被截断
- apache-spark - 如何将流式查询的结果写入多个数据库表?
- javascript - 添加 div 元素的宽度
- java - JFrame 比它的窗口大
- spring-boot - 将 EhCache 3 配置为使用默认模板自动创建所需的缓存
- java - 如何减少 Java 中的 ImageIcon 双数组执行时间
- arm - ARM FRDM-KE02Z 如何从引脚读取输入
- php - 解析 SOAP 响应