Why I am getting NoSuchBeanDefinitionException when I deploy my Spring application in Tomcat? - spring

I am working with Spring and Spring Data JPA. When deploying my application in Tomcat I'm getting the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'myController': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.service.MyService com.controller.MyController.myService; nested exception
is org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'MyService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.repository.MyRepository com.service.MyService.myRepository; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
[com.repository.MyRepository] found for dependency: expected at least 1 bean which qualifies
as autowire candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
The following are my code:
MyController.java
package com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.domain.MyEntity;
import com.service.MyService;
#RestController
#RequestMapping(MyController.ROOT_RESOURCE_PATH)
public class MyController{
public static final String ROOT_RESOURCE_PATH = "/test";
#Autowired
private MyService myService;
#RequestMapping(value="/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> getAll() {
return myService.getAll();
}
}
MyService.java
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.domain.MyEntity;
import com.repository.MyRepository;
#Service(value = "MyService")
public class MyService {
#Autowired
private MyRepository myRepository;
public List<MyEntity> getAll() {
return myRepository.findAll();
}
}
MyRepository.java
package com.repository;
import java.util.List;
import org.springframework.data.repository.Repository;
import com.domain.MyEntity;
public interface MyRepository extends Repository<MyEntity, Long> {
public List<MyEntity> findAll();
}
}
MyApplication-context.xml
<jpa:repositories base-package="com.repository" />
<context:component-scan base-package="com.service" />
<context:component-scan base-package="com.controller" />
<context:annotation-config />

I'm not seeing your repository annotated. That might be the reason why Spring couldn't create a bean for MyRepository during component scan. Annotate it with #Repository

Related

JUnit #DataJpaTest org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

I'm trying to do a test on the repository using #DataJPATest but #Autowired is not working
I already tried looking for examples of junit5 with #DataJpaTest, but I didn't find it
I tried adding other dependencies, I used #SpringTest and it worked, but I wanted to use #DataJpaTest
package com.projetoSpring.catalog.repositories;
import com.projetoSpring.catalog.model.Product;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.Optional;
#DataJpaTest
public class ProductRepositoryTests {
#Autowired
private ProductRepository repositorys;
#Test
public void deleteShouldDeleteObjectWhenIdExists() {
long exintingId = 1L;
repositorys.deleteById(exintingId);
Optional<Product> result = repositorys.findById(1L);
Assertions.assertFalse(result.isPresent());
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.projetoSpring.catalog.repositories.ProductRepositoryTests': Unsatisfied dependency expressed through field 'repositorys'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.projetoSpring.catalog.repositories.ProductRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I managed to solve it, I updated spring to version 2.7.5 and I verified that the test dependency was not downloading correctly, I redid the pom.xml and it worked.

Component Scan with both including package and excluding a class is not working SpringBoot

I am trying to scan a package com.training and want to exclude a specific class(SDIAuthorizerConfig.java) from scanning in componenet scan but it doesnt seems like working
package com.training.execution.tom.action;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import com.training.foundation.core.application.SDIServiceApplication;
import com.training.foundation.core.authorizer.config.SDIAuthorizerConfig;
import org.springframework.context.annotation.FilterType;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
#SpringBootApplication
#ComponentScan(basePackages = "com.training",excludeFilters = #Filter(type= FilterType.ASSIGNABLE_TYPE, value=SDIAuthorizerConfig.class))
#EnableGlobalMethodSecurity(securedEnabled = true)
#EnableEurekaClient
#EnableAutoConfiguration
public class TOMActionServiceApplication extends SDIServiceApplication{
public static void main(String[] args) {
SpringApplication.run(TOMActionServiceApplication.class, args);
}
}
And getting this below error while starting the application in tomcat
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'SDIAuthorizerFilter': Unsatisfied dependency expressed through field 'authorizer'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SDILDAPAuthorizer': Unsatisfied dependency expressed through field 'populator';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SDIAuthorizerConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'sdi.ldap.url' in value "${sdi.ldap.url}"
the above property is being referenced inside SDIAuthorizerConfig class which I don't want my component scan to include so I have used the below but it still being scanned.
#ComponentScan(basePackages = "com.training",excludeFilters = #Filter(type= FilterType.ASSIGNABLE_TYPE, value=SDIAuthorizerConfig.class))

Error creating bean with name 'projectController': Unsatisfied dependency expressed through field 'service';

I use the following configuration and code to create a spring boot application.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>jeecg-boot-parent</artifactId>
<groupId>org.jeecgframework.boot</groupId>
<version>2.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-pm-project</artifactId>
<dependencies>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-system-cloud-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-base-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
Controller:
package cn.my.pm.project.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import cn.my.pm.project.entity.Project;
import cn.my.pm.project.service.IProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
#RestController
#RequestMapping("/project/project")
#Slf4j
public class ProjectController extends JeecgController<Project, IProjectService> {
#Autowired
private IProjectService projectService;
#GetMapping(value = "/list")
public Result<?> queryPageList(Project project,
#RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
#RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
QueryWrapper<Project> queryWrapper = QueryGenerator.initQueryWrapper(project, req.getParameterMap());
Page<Project> page = new Page<Project>(pageNo, pageSize);
IPage<Project> pageList = projectService.page(page, queryWrapper);
return Result.OK(pageList);
}
#PostMapping(value = "/add")
public Result<?> add(#RequestBody Project project) {
projectService.save(project);
return Result.OK("add!");
}
#PutMapping(value = "/edit")
public Result<?> edit(#RequestBody Project project) {
projectService.updateById(project);
return Result.OK("edit!");
}
#DeleteMapping(value = "/delete")
public Result<?> delete(#RequestParam(name="id",required=true) String id) {
projectService.removeById(id);
return Result.OK("delete!");
}
#DeleteMapping(value = "/deleteBatch")
public Result<?> deleteBatch(#RequestParam(name="ids",required=true) String ids) {
this.projectService.removeByIds(Arrays.asList(ids.split(",")));
return Result.OK("batch delete!");
}
#GetMapping(value = "/queryById")
public Result<?> queryById(#RequestParam(name="id",required=true) String id) {
Project project = projectService.getById(id);
if(project==null) {
return Result.error("not found");
}
return Result.OK(project);
}
}
Service:
package cn.my.pm.project.service;
import cn.my.pm.project.entity.Project;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IProjectService extends IService<Project> {
}
ServiceImpl:
package cn.my.pm.project.service.impl;
import cn.my.pm.project.entity.Project;
import cn.my.pm.project.mapper.ProjectMapper;
import cn.my.pm.project.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
#Service
#Slf4j
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
}
Mapper:
package cn.my.pm.project.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.my.pm.project.entity.Project;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Service;
public interface ProjectMapper extends BaseMapper<Project> {
}
ProjectMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.my.pm.project.mapper.ProjectMapper">
</mapper>
Entity:
package cn.my.pm.project.entity;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecg.common.aspect.annotation.Dict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
#Data
#TableName("my_info")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
private java.lang.String id;
private java.lang.String myName;
private java.lang.String myType;
private java.lang.String myType;
private java.lang.String myManager;
private java.lang.String myInfo;
private java.lang.String myDetails;
private java.lang.String myImage;
}
Application:
package cn.my;
import org.apache.shiro.spring.boot.autoconfigure.ShiroAnnotationProcessorAutoConfiguration;
import org.apache.shiro.spring.boot.autoconfigure.ShiroAutoConfiguration;
import org.apache.shiro.spring.boot.autoconfigure.ShiroBeanAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
#SpringBootApplication(exclude = {ShiroAnnotationProcessorAutoConfiguration.class, ShiroAutoConfiguration.class, ShiroBeanAutoConfiguration.class})
#EnableDiscoveryClient
#EnableFeignClients
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
But when I run the application,the error message is:
ERROR org.springframework.boot.SpringApplication:858 - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.my.pm.project.mapper.ProjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at cn.my.ProjectApplication.main(ProjectApplication.java:18)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.my.pm.project.mapper.ProjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 19 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.my.pm.project.mapper.ProjectMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 32 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:64977', transport: 'socket'
How to fix it?
Spring's complaint is that you haven't defined a Bean of type ProjectMapper, which it is saying that it needs to satisfy an Autowire request. Looking at your code, I don't see a definition for such a Bean in what you've shown us here.
The exact point of the problem is hidden, I'm guessing, because you aren't showing us the definition of the ServiceImpl class. My guess is that that's where the actual reference is that Spring is trying to wire up. Apparently, the field name is baseMapper.
Spring doesn't seem to find Mapper.
try
#Mapper
public interface ProjectMapper extends BaseMapper<Project> {
}
Or
#SpringBootApplication(exclude = {ShiroAnnotationProcessorAutoConfiguration.class,
ShiroAutoConfiguration.class, ShiroBeanAutoConfiguration.class})
#EnableDiscoveryClient
#EnableFeignClients
#MapperScan(basePackage="cn.my.pm.project.mapper.*")
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
The reason is that the package is not added with #MapperScan.
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
#MapperScan(value={"org.abc.modules.**.mapper*", "cn.my.pm.**.mapper*"})
public class MybatisPlusConfig {
……
}
Then there is no error.

Why #MockBean ConversionService causes an error

Given: JUnit 5 and empty SpringBoot project created with Intellij IDEA Ultimate. I get the same results for projects with a controller that contains #Autowired ConversionService.
I need to use in a test the mocked version of ConversionService.
This is my approach:
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.convert.ConversionService;
#SpringBootTest
public class ConversionServiceTest {
#MockBean
ConversionService conversionService;
#Test
void test() {}
}
The above code causes the error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.format.support.FormattingConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Qualifier(value="mvcConversionService")}
What is the reason for this error and what is the solution?
The solution is to replace #MockBean by #SpyBean.

Autowiring with spring java config for beans in different packages not working

I am trying to implement a simple Spring AOP example.
Use case:
I have a compact disc (SgtPeppers) class with attributes such as artist, title. It has a method to play a specific track. I have created an aspect to count the number of times a track is being played. I have a test class to test the same.
Problem:
Compact disc class is in a different package from the aspect and the test class. Beans are configured using spring java config method. However, autowiring doesn't seem to work for compact disc class. It throws BeanCreationException.
Please find the code below:
Compact disc class (Here, it's configured as SgtPeppers)
package com.springinaction.soundsystem.autoconfig;
import java.util.List;
public class SgtPeppers implements CompactDisc {
private String title, artist;
public SgtPeppers(String title, String artist) {
super();
this.title = title;
this.artist = artist;
}
public SgtPeppers() {
// TODO Auto-generated constructor stub
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
#Override
public void play() {
System.out.println("Playing "+title+" by "+ artist);
}
public void playTrack(int tracknumber) {
System.out.println("Playing track "+ tracknumber);
}
}
TrackCounter Aspect:
package com.springinaction.aspects.concert.trackCounter;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
#Aspect
public class TrackCounter {
private Map<Integer,Integer> trackCounts = new HashMap<Integer, Integer>();
#Pointcut("execution(** com.springinaction.aspects.concert.trackCounter.SgtPeppers.playTrack(int)) && args(trackNumber)")
public void trackPlayed(int trackNumber){}
#Before("trackPlayed(trackNumber)")
public void countTrack(int trackNumber) {
int trackCount = getTrackCount(trackNumber);
trackCounts.put(trackNumber, trackCount+1);
}
public int getTrackCount(int trackNumber) {
return trackCounts.containsKey(trackNumber) ? trackCounts.get(trackNumber) : 0;
}
}
TrackCounterConfig:
package com.springinaction.aspects.concert.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.springinaction.aspects.concert.trackCounter.SgtPeppers;
import com.springinaction.aspects.concert.trackCounter.TrackCounter;
#Configuration
#EnableAspectJAutoProxy
public class TrackCounterConfig {
#Bean
public SgtPeppers sgtPeppers() {
SgtPeppers cd = new SgtPeppers("Sgt. Pepper's Lonely Hearts Club Band", "The Beatles");
return cd;
}
#Bean
public TrackCounter trackCounter() {
return new TrackCounter();
}
}
TrackCounterTest:
package com.springinaction.aspects.concert.trackCounter;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.springinaction.aspects.concert.config.TrackCounterConfig;
import com.springinaction.soundsystem.autoconfig.SgtPeppers;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=TrackCounterConfig.class)
public class TrackCounterTest {
#Autowired
private TrackCounter trackCounter;
#Autowired
private SgtPeppers sgtPeppers;
#Test
public void testTrackCounter() {
sgtPeppers.playTrack(1);
sgtPeppers.playTrack(1);
sgtPeppers.playTrack(2);
sgtPeppers.playTrack(2);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(3);
sgtPeppers.playTrack(7);
sgtPeppers.playTrack(7);
assertEquals(2, trackCounter.getTrackCount(1));
assertEquals(2, trackCounter.getTrackCount(2));
assertEquals(4, trackCounter.getTrackCount(3));
assertEquals(2, trackCounter.getTrackCount(7));
}
}
Here, SgtPeppers class is in com.springinaction.soundsystem.autoconfig package. TrackCounter and TrackCounterconfig are in com.springinaction.aspects.concert.config package. I get the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.springinaction.aspects.concert.trackCounter.TrackCounterTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springinaction.soundsystem.autoconfig.SgtPeppers com.springinaction.aspects.concert.trackCounter.TrackCounterTest.sgtPeppers; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springinaction.soundsystem.autoconfig.SgtPeppers] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springinaction.soundsystem.autoconfig.SgtPeppers com.springinaction.aspects.concert.trackCounter.TrackCounterTest.sgtPeppers; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springinaction.soundsystem.autoconfig.SgtPeppers] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springinaction.soundsystem.autoconfig.SgtPeppers] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
... 28 more
Note: I have tried ComponentScan with basePackages, basePackageClasses. But, it didn't work. I don't use spring boot configuration as it's for learning purpose.
I have created the same SgtPeppers bean inside com.springinaction.aspects.concert.config package. Autowiring worked. Why java config way of bean creation doesn't work for beans defined in different packages?
Any suggestion would be helpful.
Thanks.

Resources