I´m setting a bean for a spring boot application and I get the following error, can you please help me? I am not sure whether MyDataSource class is correct and whether something is missing within the application.properties
Many thanks in advance.
2017-07-22 13:50:33.752 WARN 6872 --- [ost-startStop-1]
ationConfigEmbeddedWebApplicationContext : Exception encountered during
context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name
'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.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 (no profiles are currently active).
2017-07-22 13:50:33.780 ERROR 6872 --- [ost-startStop-1] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.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 (no profiles are currently active).
MyDataSource class:
public class MyDataSource {
private static final String driverClassName = "org.postgresql.Driver";
private static final String url = "jdbc:postgresql://mydbinstance3.ckvnevtnxxbs.us-west-2.rds.amazonaws.com:5432/postgres";
private static final String dbUsername = "OnFocus";
private static final String dbPassword = "G0nk0p0rr0";
private static DataSource dataSource;
public static void main(String[] args) throws Exception {
dataSource = getDataSource();
// JdbcTemplate template = new JdbcTemplate(dataSource); // constructor
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(dataSource);
System.out.println(dataSource.getClass());
}
public static DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
}
Build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
classpath 'io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
version = '1.0-SNAPSHOT'
buildDir = 'target'
jar {
baseName = rootProject.name
}
war {
baseName = 'myapp'
version = '1.0.0'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.2.5.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.postgresql:postgresql:9.4-1201-jdbc41")
compile("org.springframework:spring-jdbc:4.2.4.RELEASE")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.liquibase:liquibase-core:3.3.3")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile("junit:junit")
}
bootRepackage {
mainClass = 'theproject.src.main.java.tutorialDatabaseServer.Application'
}
Application.properties:
server.port=8081
spring.datasource.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://mydbinstance3.ckvnevtnxxbs.us-west-
2.rds.amazonaws.com:5432/postgres
spring.datasource.username=OnFocus
spring.datasource.password=G0nk0p0rr0
liquibase.change-log=classpath:/db/changelog-master.xml
liquibase.enabled=true
Application class:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
You are enabling the use of liquibase in your properties file
liquibase.enabled=true
Have you intentionally activated it ? coz i dont see the liquibase dependency and the change log file .
Try to comment or remove ( if are not using liquibase ) these lines
liquibase.change-log=classpath:/db/changelog-master.xml
liquibase.enabled=true
I guess you are note using liquibase, when you put a liquibase properties spring de is trying to instanciate a liquibase bean . so if you don't need it , don't use it .
Related
So I've been trying to find out solution to this from long ! Any insights would help !
I am getting following error
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name
'routerConnectionFactory' defined in class path resource
[com/CONFIDENTIAL/event/processor/configuration/EventsConfiguration.class]: Unsatisfied dependency expressed through method 'routerConnectionFactory' parameter 0; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named
'actionRouterConnectionFactory' is expected to be of type 'org.apache.activemq.ActiveMQConnectionFactory' but
was actually of type 'org.springframework.cloud.sleuth.instrument.messaging.LazyTopicConnectionFactory'
Code snippet
#Bean(name = "routerConnectionFactory")
#Primary
public CachingConnectionFactory routerConnectionFactory(ActiveMQConnectionFactory actionRouterConnectionFactory ){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(actionRouterConnectionFactory);
return cachingConnectionFactory;
}
#Bean
public ActiveMQConnectionFactory actionRouterConnectionFactory(
#Value("${confidential.gateway.message.broker.url}") String brokerURL,
#Value("${confidential.router.message.broker.user.name}") String userName,
#Value("${confidential.router.message.broker.user.password}") String password,
#Value("true") Boolean alwaysSyncSend, RedeliveryPolicy defaultEntry,
#Value("${shared.amq.keystore.path:#{null}}") String keyStorePath,
#Value("${shared.amq.keystore.password:#{null}}") String keyStorePassword) throws Exception {
ActiveMQSslConnectionFactory targetConnectionFactory= new ActiveMQSslConnectionFactory();
targetConnectionFactory.setBrokerURL(brokerURL);
targetConnectionFactory.setUserName(userName);
targetConnectionFactory.setPassword(password);
if(!StringUtils.isEmpty(keyStorePath) && !StringUtils.isEmpty(keyStorePassword)){
targetConnectionFactory.setTrustStore(keyStorePath);
targetConnectionFactory.setTrustStorePassword(keyStorePassword);
}
targetConnectionFactory.setAlwaysSyncSend(alwaysSyncSend);
targetConnectionFactory.setRedeliveryPolicy(defaultEntry);
return targetConnectionFactory;
}
spring-cloud-sleuth-core : 2.2.6.RELEASE
spring-cloud-sleuth-zipkin : 2.2.6.RELEASE
active-mq-broker, active-mq-camel, client, jms-pool , open-wire-legacy, pool, spring : 5.15.13
other spring boot and related dependencies : 2.2.6.RELEASE
https://edwin.baculsoft.com/2019/07/error-overriding-bean-of-same-name-declared-in-class-path-resource-when-integrating-spring-cloud-sleuth-and-activemq-library/
Referred multiple articles on this issue (also on StackoverFlow), also tried disabling sleuth but didn't help !
Any clue ?
Your method signatures are looking for 'ActiveMQConnectionFactory'-- that is tightly coupled to ActiveMQ. Most likely, the intetion is to couple to JMS API instead. Change code to use javax.jms.ConnectionFactory instead. (ActiveMQConnectionFactory implements javax.jms.ConnectionFactory)
I am migrating our application to openjdk11 and with this setup my application is throwing below error.
PLease help on this
Note : With Jdk 1.8 the same code and configurations are working fine .
Java version: openjdk 11
Springboot-hadoop : 2.4.0 RELEASE
application properties
spring.hadoop.fsshell.enabled=false
#hadoop security properties
hadoop.config.key=hadoop.security.authentication
hadoop.config.value=Kerberos
#Hive connection properties
hive.datasource.keytab=/config/security/sit.001.keytab
hive.datasource.drivername=org.apache.hive.jdbc.HiveDriver
hive.datasource.username=ssit.001
#hive.datasource.password=password
hive.truststore.file=/config/security/hivetrust.jks
hive.krb5.conf=/config/security/krb5.conf
hive.datasource.url=url
hive.krb5.conf.debug.prop=sun.security.krb5.debug
hive.krb5.conf.isdebug=true
Java changes
#Value("${hive.datasource.drivername}")
private String driverName;
#Value("${hive.datasource.url}")
private String jdbcUrl;
#Value("${hive.datasource.username}")
private String userId;
#Value("${hive.datasource.keytab}")
private String keytab;
#Value("${hive.krb5.conf}")
private String kerberosConf;
#Value("${hadoop.config.key}")
public String hadoopConfigKey;
#Value("${hadoop.config.value}")
public String hadoopConfigValue;
#Bean(name = "hiveDS")
public DataSource configureHiveDataSource() throws IOException, ClassNotFoundException, SQLException {
Connection con = null;
// System.setProperty("hadoop.home.dir", hadoopHome);
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
System.setProperty("java.security.krb5.conf", kerberosConf);
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set(hadoopConfigKey, hadoopConfigValue);
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(userId, keytab);
Class.forName(driverName);
con = DriverManager.getConnection(jdbcUrl);
LOGGER.info("Hive Db Connected");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(jdbcUrl);
return dataSource;
}
#Bean(name = "hiveJdbc")
public JdbcTemplate getHiveJdbcTemplate(#Qualifier("hiveDS") DataSource hiveDS) {
return new JdbcTemplate(hiveDS);
}
#Bean(name = "hiveNamedJdbc")
public NamedParameterJdbcTemplate getHiveNamedJdbcTemplate(#Qualifier("hiveDS") DataSource hiveNamedDS) {
return new NamedParameterJdbcTemplate(hiveNamedDS);
}
}
2021-04-28T21:18:18.829+0530 [main] ERROR o.s.d.h.c.c.a.AbstractConfiguredAnnotationBuilder - Failed to perform build. Returning null
java.lang.IllegalArgumentException: Bean name must not be null
at org.springframework.util.Assert.notNull(Assert.java:201)
Error creating bean with name 'hadoopConfiguration' defined in class path resource [org/springframework/data/hadoop/config/annotation/configuration/SpringHadoopConfiguration.class]: Bean instantiation via factory method failed; nested exception is **org.springframework.beans.BeanInstantiationException: Failed to instantiate **[org.apache.hadoop.conf.Configuration]: Factory method 'configuration' threw exception; nested exception is java.lang.NullPointerException
I have a spring cloud function project for running the code in AWS Lambda.
The spring boot application runs fine locally. But gives the below error when deployed in AWS Lambda.
Error:-
{
"errorMessage": "Error creating bean with name 'associationService' defined in file [/var/task/org/qmetech/service/AssociationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available",
"errorType": "org.springframework.beans.factory.UnsatisfiedDependencyException",
"stackTrace": [
The project has a root project which has a subproject 'GetAssociationService'
rootproject - build.gradle
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
bootJar { enabled = false }
jar { enabled = true }
subprojects {
group = 'org.qmetech'
version = '1.0.0'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
}
repositories { mavenCentral ( ) }
ext { set ( 'springCloudVersion' , "Hoxton.SR3" ) }
dependencies {
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
}
ext.libraries = [
commonlibraries: ['org.springframework.boot:spring-boot-starter:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-function-context' ,
'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE' ,
'com.amazonaws:aws-lambda-java-log4j:1.0.0' ,
'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.data:spring-data-mongodb:2.2.5.RELEASE'
] ,
]
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test { useJUnitPlatform ( ) }
ChildProject - build.gradle
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClassName = 'org.qmetech.GetAssociationService'
}
dependencies {
dependencies {
compile libraries.commonlibraries
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
}
UserRepository.java
package org.qmetech.repository;
import org.qmetech.domain.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends MongoRepository<User, Integer> { }
AssociationService.java
package org.qmetech.service;
import org.qmetech.domain.User;
import org.qmetech.repository.UserRepository;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Function;
#Component
public class AssociationService implements Function<String, List<User>> {
private final UserRepository userRepository;
public AssociationService(UserRepository userRepository) {
this.userRepository = userRepository;
}
#Override
public List<User> apply(String uppercaseRequest) {
List<User> users = userRepository.findAll();
return users;
}
}
The Complete Code can be found here - https://github.com/iftekharkhan09/Services
Can Anyone please tell me what am I doing wrong?
Thanks!
You need to create beans of MongoTemplate since there are none to be injected by default configuration.
#Bean
public MongoClient mongoClient() {
return new MongoClient("localhost", 27017);
}
#Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) throws Exception {
return new MongoTemplate(mongoClient, "databaseName");
}
Also, don't forget to use com.mongodb.client.MongoClient which replaces reprecated com.mongodb.MongoClient as of Spring Boot 2.2.
I am facing a strange issue when I build the jar through Jenkins .
The WAR file created through Jenkins is unable to load the "Application.properties" file .
Meaning it is unable to inject anything in the "applicationProperties" field in the "ISellGenevaCacheClient.java" file - LINE 4
This issue is only happening when I build the WAR though Jenkins .
If I build the WAR normally in Eclipse (by maven clean install) , it is able to inject value into "ApplicationProperties" in "ISellGenevaCacheClient.java" file .
My "Application.properties" file is present under src/main/resources
Below are the java files being used .
My ApplicationProperties.java
#Component
#ConfigurationProperties(locations ="classpath:application.properties", ignoreUnknownFields = false , prefix="prop")
public class ApplicationProperties {
private String algorithm;
public String getAlgorithm() {
return algorithm;
}
void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
}
EncryptionUtilitiesImpl.java
public class EncryptionUtilitiesImpl {
private ApplicationProperties applicationProperties;
public EncryptionUtilitiesImpl(ApplicationProperties appProp) { // Constructor
applicationProperties = appProp;
}
public String decryptString(final String cipherTextHex) {
byte[] key = formEncryptionKeyArray(applicationProperties.getKeyProp()); // ---> Line 75 : Here , we are getting the applicationProperties as NULL
String decryptString = decryptStringFromHexWithMac(cipherTextHex, key);
return decryptString;
}
}
Utility.java
public class Utility {
public String decryptValue(String encryptedValue){
EncryptionUtilitiesImpl encryptionUtil = new EncryptionUtilitiesImpl(applicationProperties);
return encryptionUtil.decryptString(encryptedValue); // ---> Line 199
}
}
CacheClient.java
public class CacheClient {
#Inject
ApplicationProperties applicationProperties; //. --->. LINE 4
#Inject
Utility utility;
public void init(){
// -- Some code .. . . ..
Utility.decryptValue(applicationProperties.getAlgorithm())) // ---> Line 66
// --- Some code ... . . .
}}
I am getting the below NULL Pointer Exception because it is unable to inject any runtime object to 'applicationProperties' field in the "CacheClient.java" file -- LINE 4 .
Below is the Stack Trace :
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CacheServiceImpl': Unsatisfied dependency expressed through field 'genevaInstance': Error creating bean with name 'ISellGenevaCacheClient': Invocation of init method failed; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CacheClient': Invocation of init method failed; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CacheClient': Invocation of init method failed; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
at com.test.util.EncryptionUtilitiesImpl.decryptString(EncryptionUtilitiesImpl.java:75)
at com.test.util.Utility.decryptValue(Utility.java:199)
at com.test.salesinterface.service.integration.geneva.cache.CacheClient.init(CacheClient.java:66)
I am really stuck here . Please Help .
How to make Java ServiceActivator visible in JUnit Test?
I have started writing a test which imports some spring integration xmls via some Java Config files (ie, files set in the #ContextConfiguration). One of these xml files references a channel called pollerErrorChannel this is the input-channel to a ServiceActivator declared in a Java Class. When the test cranks up I get the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sftpInboundAdapterBusiness': Cannot create inner bean '(inner bean)#1fe8d51b' of type [org.springframework.integration.scheduling.PollerMetadata] while setting bean property 'pollerMetadata'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1fe8d51b': Cannot create inner bean '(inner bean)#324dcd31' of type [org.springframework.integration.channel.MessagePublishingErrorHandler] while setting bean property 'errorHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#324dcd31': Cannot resolve reference to bean 'pollerErrorChannel' while setting bean property 'defaultErrorChannel'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pollerErrorChannel' available
Below in my test
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = {PropertySourcesPlaceholderConfigurer.class,
SFTPSampleReceiver.class,
SampleIngestBusinessConfig.class,
SampleIngestConfig.class,
SessionFactoryConfig.class},
initializers = ConfigFileApplicationContextInitializer.class)
#TestPropertySource(locations={"/application.yml"})
public class BusinessSampleRecieverTests {
#Test
public void test() {
}
}
Segment from sample-ingest-business.xml which specifies pollerErrorChannel as a channel
<int-sftp:inbound-channel-adapter id="sftpInboundAdapterBusiness"
channel="sftpInboundBusiness"
session-factory="sftpSessionFactory"
local-directory="${sftp.localdirectory}/business-local"
filter="businessCompositeFilter"
remote-file-separator="/"
remote-directory="${sftp.directory}/business-sftp">
<int:poller cron="${sftp.cron}" max-messages-per-poll="1" error-channel="pollerErrorChannel"/>
</int-sftp:inbound-channel-adapter>
Here is The Java Class which specifies pollerErrorChannel as the InputChannel to a #ServiceActivator
#Slf4j
#MessageEndpoint
#Component
public class SFTPSampleReceiver {
#ServiceActivator(inputChannel = "pollerErrorChannel", outputChannel = "errorUploadChannel")
public Message<String> processInvalidSample(GenericMessage errorMessage) {
String error = ((Exception) errorMessage.getPayload()).getCause().toString();
String fileName = ((MessagingException) errorMessage.getPayload()).getFailedMessage().getHeaders()
.get("file_name").toString();
String directory = ((MessagingException) errorMessage.getPayload()).getFailedMessage().getHeaders()
.get("sample_type").toString() + "-sftp";
String shortFileName = fileName.replace(".xml", "");
String errorFile = shortFileName + "_error.txt";
log.debug(fileName + " Was invalid and rejected.");
final Message<String> message = MessageBuilder.withPayload(error).setHeader("error_file_name",
errorFile).setHeader("file_name", fileName).setHeader("short_file_name",
shortFileName).setHeader("directory", directory).build();
return message;
}
}
thanks
You have to declare that pollerErrorChannel bean.
With just a #ServiceActivator(inputChannel = "pollerErrorChannel" it will be already to late to have that channel auto-created. The <poller> is parsed and populated as a bean a bit earlier.
We might review the PollerParser to use MessagePublishingErrorHandler.setDefaultErrorChannelName() isntead of errorHandler.addPropertyReference("defaultErrorChannel", errorChannel); to let to resolve the channel late on demand.
Feel free to raise a JIRA on the matter!
Garys comment about adding < int:annotation-config/> to your XML worked