I am developing a simple CRUD application with spring-boot.
I have most of the project completed, although I get this error when I try to run the project.
Description:
Field userDBOP in com.application.crud.GreetingController required a
bean of type 'com.application.crud.myoperation.JdbcUserDAO' that could
not be found.
Action:
Consider defining a bean of type
'com.application.crud.myoperation.JdbcUserDAO' in your configuration.
In IntelliJ when I hover over the line that causes the error, the following message shows up
"Could not autowire. No beans of 'JdbcUserDAO' type found.
Even though I have in my 'Beans.xml' file (located below the 'src' directory:
<bean id="customerDAO" class="com.application.crud.myoperation.JdbcUserDAO">
<property name="dataSource" ref="dataSource" />
</bean>
Can anyone tell me how to fix this error?
This seems to be a problem with configuring a Spring Boot Application with an existing Spring Context. There is a section in the Spring documentation about this.
By default, you need to specify the location of your application context using the #ImportResource annotation. An example would be:
#SpringBootApplication
#ImportResource("applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}
Note that if the file is located elsewhere in the classpath, then you need to reference it properly for spring to pick it up (e.g #ImportResource({"classpath*:applicationContext.xml"}) )
Related
Good day,
I am doing a Spring Boot Application in my Eclipse IDE. When I right click on my SpringBoot Application file and run as Java application, I hitting error as follow:
APPLICATION FAILED TO START
Description:
Field tutorialRepository in com.utility.tool.ToolApplication required a bean of type 'com.utility.tool.repository.TutorialRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.utility.tool.repository.TutorialRepository' in your configuration.
Then I found that I forget to include the spring boot starter data jar. Hence, I add the following code in my build.gradle and it finally run correctly:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.5'
Then I right click my project and export jar as runnable jar, and then try to run it by java -jar my.jar, and it hit back the error.
I open the jar in JdGui, and found that the spring-boot-starter-data-jpa-2.7.5.jar is inside. May I know what is my mistake? My jar structure is something as follow:
The jar is in the list but is at bottom, thus not in my screen shot.
Check your SpringBoot annotations. You may be missing some #Service, #Repository, #Component annotations.
In my spring project I can defined bean in XML like this:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="weblogic.xml.saaj.MessageFactoryImpl" />
</property>
</bean>
even my project does not contain weblogic.xml.saaj.MessageFactoryImpl class. When I deploy my application to wls server everything works fine just STS is complaining that it doesn't know this class. How I can convert this code to spring-boot application and define this factory in some java class ?
In a configuration class (has #Configuration above class definition) create a method as a factory for your MessageFactoryImpl and use #Bean above it. Defining a bean in spring boot is like the old java configuration.
See This question , It will help
weblogic.xml.saaj.MessageFactoryImpl is an implementation class which exists already in WebLogic server library jars.
To do that using #Bean, use #ConditionOnClass
#Bean
#Primary
#ConditionalOnClass(weblogic.xml.saaj.MessageFactoryImpl.class)
public SoapMessageFactory messageFactory() {
return new weblogic.xml.saaj.MessageFactoryImpl();
}
I am trying to use Spring java config with XML based config but beans using spring profile feature are never getting created. Can you suggest what could be root cause. Spring 3.2 is used by my application.
My Java Config Class -
#Configuration
#ComponentScan({ "com.aexp.mars" })
#Profile("dev")
#PropertySource("classpath:messages.properties" )
#ImportResource("classpath:/spring/spring-profiles.xml")
public class SpringConfigDev{
private DriverManagerDataSource dataSource;
Spring XML config-
<beans profile="dev">
<bean id="marsURLConfig" class="com.aexp.mars.common.utils.MarsURLConfig">
<property name="solrBaseURL" value="http://localhost:8080/solr"/>
</bean>
</beans>
The Dev profile is activated using the code below inside the main method
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String env = "dev";
context.getEnvironment().setActiveProfiles(env);
context.register(SpringConfigDev.class);
context.refresh();
One of my Spring Components uses Autowiring with MarsURLConfig type and I get the exception below.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.aexp.mars.common.utils.MarsURLConfig] 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)}
If I remove profile="dev" from the xml, the application works correctly
this seems to be known limitation with Spring 3.2 Java based config approach. Spring 3.2 do not support bean level profile creation when we use Java config and since I was trying to load XML from Java based configuration class , it didn't worked for me .
I'm trying to use Spring annotations in Domino XPages Java classes. Spring works when I'm defining beans in the configuration file. However I fail to use the annotations.
To illustrate the problem, I have created two simple empty classes annotated with #Component annotation - com.geo168.a.B (#Component public class B {}) and com.geo168.a.C (#Component public class C {})
The first one I have created in Eclipse, packed and added to the application in a jar. The second one I add directly in Code/Java section.
I add a component-scan tag in the configuration file: <context:component-scan base-package="com.geo168.a"/> and try to instantiate the classes:
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG);
// class defined in the jar, works ok
com.geo168.a.B b = ctx.getBean(com.geo168.a.B.class);
// class defined in Code/Java, throws exception
com.geo168.a.C c = ctx.getBean(com.geo168.a.C.class);
I get an error: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.geo168.a.C] is defined
Spring has found the annotated class only in the JAR file. It works if I add the bean explicitly to the configuration file:
<bean class="com.geo168.a.C"/>
A similar post: Is possible to add annotation #ManagedBean in XPages? seems to address only the particular JSF annotations (that do not work, because they are not implemented).
In the Spring documentation: http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-scanning-autodetection I find a note: The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task.
Could this be somehow related? I do not know in what form Domino deploys the classes internally.
I am currently trying to start up a server side of an application, built with Maven; the clean install is succesful, but then tomcat7:run fails with the following Spring error message
04:21:19,059 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader -
Context initialization failed org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'properties' defined in class path resource [context.xml]:
Initialization of bean failed; nested exception is java.lang.NoSuchMethodError:
org.springframework.core.convert.Property.<init>(Ljava/lang/Class;Ljava/lang/reflect/Method;
Ljava/lang/reflect/Method;Ljava/lang/String;)V
The concerned bean definition in context.xml is as follows
<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:server.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Research shows that these kind of errors are usually related to incorrect JAR versions on the classpath. In this particular application, Spring version 3.2.4.RELEASE dependencies are defined in Maven pom.xml, and checking out the API of the concerned org.springframework.core.convert.Property class it seems that constructor Property(Class objectType, Method readMethod, Method writeMethod, String name) is actually available in this version. Any ideas are appreciated
Try to run your java with -verbose option.
It shows wich jar has been loaded class from.
Looks like there is a spring jar in the bootstrap classloader of your Tomcat - older version of Spring.