Hi I have a spring boot project. The main class package is like this :
com.som.demo
I have used an external jar as a maven dependency in my project. In that jar there is a service class annotated with #Service as below :
#Service
public class SomeServiceImpl implements SomeService {
#Autowired
private TreeCacheWrapper ffCoreCache;
#Autowired(required = false)
private ZookeeperProperties zookeeperProperties;
public SomeServiceImpl(TreeCacheWrapper ffCoreCache, ZookeeperProperties zookeeperProperties) {
this.ffCoreCache = ffCoreCache;
this.zookeeperProperties = zookeeperProperties;
}
}
And the main package where this SomeServiceImpl class is as below :
com.som.test
Now in my project when I am autowiring the class I get the bean creation error while running the spring boot app. This is how I am doing :
#Autowired
private SomeService someService;
Error :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someServiceImpl'
Unsatisfied dependency expressed through field 'ffCoreCache';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'ffCoreCache' defined in class path resource [com/som/test/config/ZooKeeperConfig.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.som.test.cache.TreeCacheWrapper]:
Factory method 'ffCoreCache' threw exception;
nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'ffCoreCache' defined in class path resource [com/som/test/config/ZooKeeperConfig.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.som.test.cache.TreeCacheWrapper]:
Factory method 'ffCoreCache' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.som.test.cache.TreeCacheWrapper]:
Factory method 'ffCoreCache' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
Caused by: java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry
Caused by: java.lang.ClassNotFoundException: org.apache.curator.retry.ExponentialBackoffRetry
When spring boot application starts, it scans for beans in the same package as your main class, and all packages beneath it.
Since your main class resides in the package com.som.demo spring boot will find any beans in this package, or in packages like com.som.demo.sample1, com.som.demo.a.b.c etc
However it won't scan com.som.test package because its a "peer" to the main package.
In general you can configure spring boot to scan the packages of your choice with the help of #ComponentScan annotation that accepts a list of base packages to scan. You can put this annotation right on your main class (next to #SpringBootApplication).
However this is kind of against the conventions of spring boot.
Read this tutorial for more technical details and examples.
Annotate Service interface with #Service, not the Implementation class and try to annotate ServiceImplementation class with #Component.
Spring boot by default scan to beans in your main package. you can configure it using
#SpringBootApplication(scanBasePackages={"com.som.demo" , "com.som.test"})
Related
I have written a Restful application in springboot.
So for that i have a controller class with #RestController and a service class with #Service annotation.
In my Rest controller, i #Autowire the service like below.
#Controller
public class TestController {
#Autowire
TestService testService;
}
#Service
public class TestService {
}
Note : My main springboot class at top level of all the packages.
The strange thing is that, when i start my Spring boot application, it start perfectly and service bean initalized correctly. I also tested the same. But when i do maven build it gets failed with error :
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I'm unable to solve this. I have a spring boot application.
Main class annotated with
#SpringBootApplication.
Test class annotated with:
#RunWith(SpringRunner.class)
#SpringBootTest
Everything is fine until i add a filter-configurer class in the application:
#Configuration
public class YadaYadaFilterConfigurer
{
#Bean
public FilterRegistrationBean<YadaYadaFilter> createYadaYadaFilter()
{...}
...
}
Boom!
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]:
...
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:...org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
My dependencies:
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Some clearification.
Stripped so that I have only this test-class:
#RunWith(SpringRunner.class)
#SpringBootTest
public class JsonvalidationApplicationTests {
#Test
public void contextLoads() {
}
}
If comment out the #SpringBootTest then everything works. Seems like this annotation introduces a need for more dependencies?
Some more stacktrace:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'createyadayadaFilter' threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
... 67 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
Sorry, after some more investigation i found that the problem is in my FilterRegistrationBean.
the exeption is thrown here.
ClassPathScanningCandidateComponentProvider provider = createComponentScanner();
provider.findCandidateComponents("").stream()...
I will answer this thread and create a new.
I am trying to integrate PowerMockito with my spring boot unit test cases suite.
I am using consul to fetch my app configuration from the cloud.I am getting following exception while running junit testcases :
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.consul.config.ConsulConfigBootstrapConfiguration$ConsulPropertySourceConfiguration': Unsatisfied dependency expressed through field 'consul': Error creating bean with name 'consulClient' defined in org.springframework.cloud.consul.ConsulAutoConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ecwid.consul.v1.ConsulClient]: Factory method 'consulClient' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.ecwid.consul.v1.ConsulRawClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consulClient' defined in org.springframework.cloud.consul.ConsulAutoConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ecwid.consul.v1.ConsulClient]: Factory method 'consulClient' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.ecwid.consul.v1.ConsulRawClient
Following is my code:
#SpringBootApplication
#EnableRetry
#EnableDiscoveryClient
#EnableScheduling
#ConfigurationProperties(prefix = "scheduler")
class TestApplication{
}
#ActiveProfiles("abc")
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
#PrepareForTest(KafkaClientUtil.class)
#SpringApplicationConfiguration(classes=TestApplication.class)
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProcessorTest {
#Autowired
private TestClient Client;
#Test
public void test1() throws JsonProcessingException {
}
}
I'm just learning Spring and I've been struggling for the past couple of days on how to configure the Spring JdbcTemplate to use my PostgreSQL database. I don't know how to do this. I keep reading the documentation, (such as http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html) but it seems like I'm going around in circles.
From the errors thrown it seems that it can't instantiate the RelationalDatabase Class that I wrote as a bean. I'm not sure what it would take to instantiate the class properly.
How do I move from something like the guides (Like https://spring.io/guides/gs/relational-data-access/) which totally work to a more complex solution?
Relational Database Class
#Repository
public class RelationalDatabase
{
private JdbcTemplate jdbcTemplate;
public RelationalDatabase(){}
public RelationalDatabase(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
#Autowired
public void setDataSource(javax.sql.DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
\src\main\resources\application-dev.properties
spring.datasource.url=jdbc:postgresql://192.168.56.102:5432/scibase
spring.datasource.type=org.postgresql.ds.PGPoolingDataSource
spring.datasource.username=lemon
spring.datasource.password=XXXXXX
spring.datasource.platform=postgres
spring.datasource.max-active=100
spring.datasource.name=lime
spring.database.driverClassName=org.postgresql.Drive
Stack Trace (Summary)
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'relationalDatabase': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire method: public void
com.scientifi.papers.db.relational.RelationalDatabase.setDataSource(javax.sql.DataSource);
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw
exception; nested exception is
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException:
Cannot determine embedded database driver class for database type
NONE. If you want an embedded database please put a supported one on
the classpath. If you have database settings to be loaded from a
particular profile you may need to active it (the profiles "dev "
are currently active).
Thanks!
Did you forget the "r" in Driver? (Cannot determine embedded database driver class for database type NONE)
spring.database.driverClassName=org.postgresql.Driver
instead of
spring.database.driverClassName=org.postgresql.Drive
I Got the same Exception when i was testing my Spring Boot Application from Junit.
I resolved it using the #SpringApplicationConfiguration(classes = Application.class) above the Junit Test class. The Spring Boot application was not getting initialized properly
I've got an ejb
#Stateless
#LocalBinding(jndiBinding = DmsExportExecutor.DEFAULT)
public class InjectedBean implements BeanInterface {
}
that implements interface
#Local
public interface BeanInterface {
public static final String DEFAULT = "package.InjectedBean";
}
And i'm trying to inject this into a bean, that's in exactly the same project and package. by #jndiinject annotation
public class AnotherBean {
#JndiInject(jndiName = BeanInterface.DEFAULT)
BeanInterface bean;
}
If i do that, a runtime exception is thrown.
java.lang.RuntimeException: Unable to inject jndi dependency: env/package.InjectedBean/bean into property package.InjectedBean. AnotherBean.bean: package.InjectedBean not bound
Caused by: javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: package.InjectedBean not bound]
Caused by: javax.naming.NameNotFoundException: package.InjectedBean not bound
After refactoring my InjectedBean into another package that is even located inside of the same maven project, it starts working.
Could anyone explain why this happens?
Have you tried using the standard #EJB annotation ?
#EJB
BeanInterface bean;
This should do the work for you by bean type.
You should see if (and how) your injected bean is loaded by the EJB container at container startup
or using the admin gui.
You can always do a basic lookup:
InitialContext().lookup("the.bean.jndi.name").
Y.Lev