Spring Java Config with XML config - java

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 .

Related

SpringBootTest loads unwanted beans which are not included in ContextConfiguration

I am writing a Spring Boot integration test. Here I want to exclude several beans. So I am trying to load only as many beans as are required for my tests. For that I am using #COntextConfiguration and specifying only those classes which I want.
Despite of doing this I can see Spring Boot is still loading all other beans. Like for e.g it is loading RetryTemplateConfig which is present in a different package altogether. I used #ComponentScan to scan only current package and not the one in which RetryTemplateConfig lies. Still I get bean configuration errors. Below is my top section of my test class
#SpringBootTest
#RunWith(SpringJUnit4ClassRunner.class)
#Import(BulkExceptionClosureTestConfig.class)
#ActiveProfiles("test")
#TestPropertySource(locations = "classpath:application-test.yml", properties = {"INSTANCE_ID=1","spring.profiles.active=test","ENV=test"})
#ContextConfiguration(classes = {BulkExceptionClosureApplication.class, ClosureRequestCsvFileProcessor.class,
MetadataFetcher.class, XmlProcessingService.class})
#ComponentScan(basePackages = "com.barclays.exceptionclosure")
public class BulkExceptionClosureTest {
And I get below error when starting this test.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'retryTemplate' defined in class path resource [com/xyz/validation/config/RetryTemplateConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException
I have not included this class in my ContextConfiguration, still it gets loaded. Also #ComponentScan doesn't seem to work, as RetryTemplateConfig is in com.xyz.validation package. And my test class is in com.xyz.exceptionclosure package
What can be done to definitely load only the required beans for my test? Please not I cannot add #Profile("!test") on classes which I don;t want in Spring Boot integration test. Those are from library I cannot modify.

Failed to read candidate component class in spring mvc & spring boot

i add spring boot project as dependency in spring mvc project , application can run successfully but when i call service i faced below error
Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to read candidate component class: nested exception is
org.springframework.core.annotation.AnnotationConfigurationException:
Attribute 'proxyBeanMethods' in annotation
[org.springframework.boot.SpringBootConfiguration] is declared as an
#AliasFor nonexistent attribute 'proxyBeanMethods' in annotation
[org.springframework.context.annotation.Configuration].; nested
exception is java.lang.NoSuchMethodException:
org.springframework.context.annotation.Configuration.proxyBeanMethods()
Attribute 'proxyBeanMethods' in annotation
[org.springframework.boot.SpringBootConfiguration] is declared as an
#AliasFor nonexistent attribute 'proxyBeanMethods' in annotation
[org.springframework.context.annotation.Configuration].; nested
exception is java.lang.NoSuchMethodException:
org.springframework.context.annotation.Configuration.proxyBeanMethods()
It mainly complains about your #Configuration does not have an attribute called proxyBeanMethods. Checking from the javadoc , this attribute is added since Spring 5.2.
Most probably it is because you are messing up the spring version with your spring-boot version. Your spring version is too old (before 5.2) such that the above attribute of the #Configuration is not defined.
You should use the spring framework which the version is defined by the spring-boot version that you use . Show me the pom.xml and I can help to check which <dependency> causes such version mismatch. Thanks.

How to use class which is not a dependency in my spring boot project

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();
}

Getting cyclic dependencies error in Spring boot

I am trying to migrate our spring application to Spring boot.
I am getting an cyclic dependencies error.
Bean A -> Bean B -> Bean C
In Bean C I am autowiring a Map which is defined in a XML
<util:map id="beanMap" map-class="java.util.HashMap">
<entry key="beanA" > <bean class="com.org.BeanA" /> </entry>
</util:map>
In Bean C I am autowiring
#Autowired
#Resource(name = "beanMap")
private Map<String, serviceInterface> beanMap;
This setup is working properly in Spring without boot.
But when I try to run this configuration in Spring Boot. I am facing cyclic dependencies error.
Any help is appreciated.
Try removing #Autowired in Bean C.

Spring boot CRUD application - Bean not found error

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"}) )

Categories