Spring: Is it possible to inject external indexed property? - java

I have next application.properties file:
cassandra.connection.hosts[0]=host1.lab.org
cassandra.connection.hosts[1]=host2.lab.org
And I'm looking for a way to inject it as a list into a Spring bean - something like this:
#Value("${cassandra.connection.hosts}")
private List<String> hosts;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List ru.croc.rosstat.csod.store.migrations.cli.Application.contactPoints; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'cassandra.connection.nodes' in string value "${cassandra.connection.nodes}"
I know that it's possible to specify hosts in one comma separated line but it's not what I want. Another option is to use #ConfigurationProperties, it works. But is there a way to inject the list just by an annotation?

Related

Error creating bean; Unsatisfied dependency expressed through field

I have a central library for certain functions and now I have trouble integrating that library.
The library is written in spring boot and contains a class: com.common.Security.
It is defined like this:
package com.common;
....
#Service
#EnableConfigurationProperties(SecurityProperties.class)
#Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Security {
....
}
I try to use this in another class:
package org.special;
import com.common.Security;
#Configuration
public class WebServiceConfig {
#Autowired
private Security security;
....
}
But I get some errors:
Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'WebServiceclient';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webserviceClient':
Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'webServiceTemplate' defined in class path resource [org/special/WebServiceConfig.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.ws.client.core.WebServiceTemplate]: Factory method 'webServiceTemplate' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
nested exception is org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.Security': Scope 'request' is not active for the current thread;
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found:
What can I do to fix this?
Removing the Scope was quite helpfull.
I tried this before but some of my tests failed after that. I didn't see, that this was because of missing settings in application.yaml for the tests.
They were not neccessary when scope is request.

Spring boot project on Heroku unable to use application.yml

As the title states I'm trying to deploy a spring boot application to heroku.
Under /resources I have an application.yml file from which I inject properties using #Value annotation.
During boot I get a stacktrace but I believe the relevant part is the below
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dk.fitfit.remotetexting.util.GoogleAuth dk.fitfit.remotetexting.api.controller.MessageController.googleAuth; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'googleAuth': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dk.fitfit.remotetexting.business.service.ConfigurationService dk.fitfit.remotetexting.util.GoogleAuth.configurationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String dk.fitfit.remotetexting.business.service.ConfigurationService.clientId; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'security.oauth2.client.clientId' in string value "${security.oauth2.client.clientId}"
Basically it says it can't create my ConfigurationService because it can't resolve my placeholder 'security.oauth2.client.clientId'.
#Service
public class ConfigurationService {
#Value("${security.oauth2.client.clientId}")
private String clientId;
#Value("${fcm.auth_key}")
private String AUTH_KEY_FCM;
#Value("${fcm.api_url}")
private String API_URL_FCM;
...
}
Any clues about how to make heroku use my application.yml file?
I forgot to include application.yml file in my repository... doh!

NotWritablePropertyException when deploying the application

Im getting the below exception when i deploy the application, Does anyone know the work around?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateUtil' defined in class path resource [spring-hibernate-cfg.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.library.database.HibernateUtil]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1493)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
add setSessionFactory(); method to com.go.database.HibernateUtil,
you have defined property sessionFactory and injected into HibernateUtil, Spring will use setter injection and you don't have setter method for this property so it is failing

Unclosed Parenthesis exception on CREATE in spring-data-neo4j

I'm running Spring Data Neo4j version 2.3.3. When I try to execute a CREATE statement through my GraphRepository, I'm getting the following exception:
org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement ... nested exception is Unclosed parenthesis
"CREATE (:line{id:1})-[:ROOT]->(:point{id:10})-[:NEXT]->(:point{id:11})-[:NEXT]->(:point{id:12})-[:NEXT]->(p:point{id:13})"
^
The ... merely hides the same thing printed below it, and the ^ appears in the correct place. As you can see if you look at my query, there is no unclosed parenthesis. What's going on here?
EDIT: For testing purposes, I tried reducing the statement to: CREATE (:line{id:1}), and I get the same result.
EDIT: I tried upgrading my pom to use version 3.0.0, and now am getting the following exception:
Error creating bean with name 'controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.example.model.LineRepo org.example.controller.Controller.lineRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineRepo': Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Cannot resolve reference to bean 'graphDatabaseService' while setting bean property 'graphDatabaseService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphDatabaseService' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.neo4j.kernel.EmbeddedGraphDatabase]: Constructor threw exception; nested exception is java.lang.RuntimeException: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.extension.KernelExtensions#61634a3b' failed to initialize. Please see attached cause exception.
The deepest root cause exception given is:
java.lang.NoSuchMethodError: org.neo4j.kernel.impl.nioneo.store.FileSystemAbstraction.getOrCreateThirdPartyFileSystem(Ljava/lang/Class;Lorg/neo4j/helpers/Function;)Lorg/neo4j/kernel/impl/nioneo/store/FileSystemAbstraction$ThirdPartyFileSystem;
Nothing was changed other than upgrading the dependency. The 'controller' bean it is referring to is the controller I have #Autowired with my GraphRepository
Spring Data Neo4j 2.3.3 is for Neo4j 1.9, which doesn't support labels (the syntax you're using in Cypher). You need to use 3.0.0 in order to get Neo4j 2.0 support.

Why doesn't Autowiring work for this Spring MVC String bean property?

#Autowired
private String defaultLanguage;
When I try to #Autowire the defaultLanguage field of the CountryBean class as follows:
<beans:bean id="countryBean" class="geoapp.CountryBean">
<beans:property name="defaultLanguage" value="English" />
</beans:bean>
I get this error:
Error creating bean with name 'CountryBean':
Injection of autowired dependencies failed;
nested exception is
org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private java.lang.String geoapp.CountryBean.defaultLanguage;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [java.lang.String] 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)}:
When it says No matching bean of type [java.lang.String] found for dependency, I wonder if there's something else I need to say to let it know that the value English is a String?
Since you are explicitly specifying the property value via xml, there is no need to have the AutoWired annotation.
You are not using the autowire feature properly.
You can either:
Use the <beans:property ... /> in the XML and add a setter for the property in the class
Use the #Autowired annotation that automatically looks in the Spring context for an existing bean that has your property class (in your case, String)
The other answers are correct, but to clarify things, those beans defined in the *.xml files are processed before any # type annotations get processed. So for your example, the bean "CountryBean" is processed before the property "defaultLanguage" is processed, leaving the dependency null.
Yet another 'the other answers are correct', but here's another tidbit for you -- Spring will automatically set enums for you, so if you had an enum of languages and your bean definition had something like ", you'd get a type safe enum in there, instead of a String.
If you want autowiring your property, you need to make a new bean for that defaultLanguage String. Then equate the bean's name and field's name.

Categories

Resources