SpringBeanAutowiringInterceptor throwing NoSuchBeanDefinitionException - java

I'm trying to autowire beans to EJBs with Spring using the #Interceptors(SpringBeanAutowiringInterceptor.class) annotation. The problem is that I keep on getting the NoSuchBeanDefinitionException for the autowired classes when the EJBs are instantiated. (The Filter class in my example).
I'm using Java EE 6 application (EJB 3.0), Spring 4.2.2 managed with Maven and running in a WebSphere 7 AS.
I have the following Spring dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
Implementation:
#Stateless
#Remote(ServiceRemote.class)
#Interceptors(SpringBeanAutowiringInterceptor.class)
public class Service implements ServiceRemote {
#Autowired
private Filter filter;
...
}
Class to be autowired
#Component
public class FilterImpl implements Filter { ... }
The beanRefContext.xml which SpringBeanAutowiringInterceptor will look for:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="businessBeanFactory"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:applicaion-context.xml" />
</bean>
</beans>
The application-context.xml with the bean definitions:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.FilterImpl" />
</beans>
Both XMLs are in the classpath, as they are in src/main/resources/.
I've tried some debugging with the following code, but couldn't find something useful.
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
BeanFactoryReference ref = factoryLocator.useBeanFactory("businessBeanFactory");
BeanFactory factory = ref.getFactory();
FilterImpl instance = factory.getBean(FilterImpl.class);
Both contexts appear to be loaded, as I get logs like the following, when the EJB is instantiated for the first time.
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#7730773; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from URL [file:/C:/project/service-module/target/classes/beanRefContext.xml]
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#5580558: root of context hierarchy
But after that Spring can't find the bean I defined in the application-context.xml.
In application-context.xml I've tried using <context:component-scan base-package="com"/> instead of the <bean class="com.FilterImpl" />, but I got the same result.
Am I missing some configuration?
Edits: Added suggestion by Steve C and logs of context loading.

Move your xml configuration files from src/main/java to src/main/resources.
Files in src/main/java are only processed by the maven-compiler-plugin.
Additional resource files are processed by the maven-resources-plugin which copies files from src/main/resources.

It seems that you are not adding <context:annotation-config/> in your spring xml file

I've just found the answer. In the <constructor-arg value="classpath*:applicaion-context.xml" /> there is an applicaion-context.xml when it should be application-context.xml. When I edited the question and posted the logs about the loading of the contexts, I noticed that only beanRefContext.xml had its definition loaded. There was no logs about some parsing error and I just ignored this possibility. Everything is working now.

Related

Spring jndi.xml placement holder using environment variable

I have a tomcat application with jndi.xml file that has placeholder. This file contains placeholder ${...} and I want it to be replaced from environment variable.
jndi.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<bean id="widgetsRestBaseUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsRestBaseUrl}"/>
</bean>
</beans>
pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.13.RELEASE</version>
</dependency>
From my understanding, PropertySourcesPlaceholderConfigurer resolves ${...} in bean definition , so I expected it to work, but it doesn't. Am I missing anything from here? I do see PropertySourcesPlaceholderConfigurer class in my project from spring. And I also do set environment variable, checked using printenv.
EDIT: I found that I need to have some <context:property-placeholder />, but not sure what it means.
Exposing jndi variables as property place holder
After some research, I found that adding <context:property-placeholder ignore-unresolvable="true"/> in my context.xml file did the trick. From Exposing jndi variables as property place holder

How can I pass spring application.yaml file to spring context created in Java project using spring boot project as dependency?

I am using spring boot project as a dependency in another non spring java project. The problem is I am getting error when spring processes #Value() annotation.
According to this article
In order to get instances of the beans managed in spring I created ClassPathXmlApplicationContext and created applicationcontext.xml,
but I am getting error from #Value("${proper}") anotation that
the resource cannot be resolved to URL because it does not exist
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-application.xml"});
context.getBean("mybean");
applicationcontext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example" />
</beans>
The application.yaml is located outside the project in /opt/myapp/config/application.yaml and contains:
proper: file:/path/to/some/file/that/exists
I am expecting that spring will initialize the value of proper in component using the default application.yaml. since I can run spring boot project stand alone and it works fine.
Any Ideas?

Spring Java config specified Beans won't initialize

I've read Spring In Action, and prefer Java config over XML config. So I used Java config to write my app, but our deployment environment requires me to use XML config. So I wrote an XML config, and it's only function is to import the root Java config.
The Java config code looks like this:
package com.somegroup.app;
#Configuration
#ComponentScan(basePackages = "com.tianchengsys.crawlers.cqs")
public class AppCtxConfig {
#Bean
public SomeType aSomeType() {
return new SomeType()
}
}
and the XML config looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:annotation-config />
<bean class="com.somegroup.app.AppCtxConfig" lazy-init="false" />
</beans>
When I created a ClasspathXmlContext("classpath:spring-context.xml") in Eclipse, the SomeType bean defined in Java config is initialized, and registered to springs ApplicationContext. But when I deployed this app (all dependencies are in a lib directory), the AppCtxConfig bean defined in the XML config was just treated an ordinary bean (not configuration).
It was created, but the beans defined in it were not initialized. Spring some times warned the someType method in the Java config should be static. I did changed it to static, it also didn't work.
This is because you are creating AppCtxConfig as a regular bean, which it isn't.
As the commentor suggested, add the component-scan on and set base-package to the package where your config class is located in:
<!-- Scan the JavaConfig -->
<context:component-scan base-package="com.somegroup.app" />
If your app package is the root package, with all subpackages inside, add a new config package and move the AppCtxConfig inside it.
So add:
<!-- Scan the config package with AppCtxConfig inside it -->
<context:component-scan base-package="com.somegroup.app.config" />

Spring not autowiring using annotation a bean declared on the XML

I'm trying to inject a bean that was defined on a XML into an annotated, It is only annotated and not declared on XML, I think that is just something that I'm missing here is my *context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
...
<bean id="userBusiness" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName" value="java:global/app-common/app-common-core/UserBusinessImpl" />
<property name="businessInterface" value="com.app.common.business.UserBusiness" />
</bean>
...
<context:annotation-config/>
<context:component-scan base-package="com.app.common.jsf" />
</beans>
Here's the component:
#Component
public class AppAuthorization {
#Autowired
private UserBusiness userBusiness;
#Autowired
private AppLogin sabiusLogin;
...
#Local
public interface UserBusiness {
User listUserByFilter(User user) throws UserBusinessException;
...
#Stateless
#Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
#Interceptors({SpringBeanAutowiringInterceptor.class})
public class UserBusinessImpl implements UserBusiness {
#Autowired
private ProgramasUsuariosDao programasUsuariosDao;
...
When I try to access the AppAuthorization Spring says that:
Could not autowire field: private com.app.common.business.UserBusiness com.app.common.jsf.AppAuthorization.userBusiness"
Seems that the annotated bean can't see the declared one, but search and seems that I only needed to add the annotation-config to the XML, is this right? Hope some can help me.
EDIT
I think that this is the most important part of the stack trace:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.common.business.UserBusiness] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503)
... 201 more
Following the steps on the context creation I see no bean registered tha can be seen by annotations just when springs creates the context based on the xml is that I can see all the beans that wre created.
EDIT 2
This is the beanRefContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="contexts" class="com.app.common.spring.ClassPathXmlApplicationContext" />
</beans>
This is the class that loads the XML context files:
public class ClassPathXmlApplicationContext extends org.springframework.context.support.ClassPathXmlApplicationContext {
public ClassPathXmlApplicationContext() {
super((System.getProperty("module.context.file").split(";")));
}
}
As I said, the annotated beans cannot see the XML ones so, spring cannot autowire them.
EDIT 3
I don't have a #Configuration bean (I'm not using AnnotationConfigApplicationContext), all the config is in the XML and if I try to create one the server doesn't start, it's a legacy system with spring 3.2 and EJB 3.0 and I can't change this aspect now.
Thanks in advance!
I think you missed to specify #Component annotation for the UserBusiness class
EDIT:
Can you make the component-scan config to com.app.common instead of com.app.common.jsf
What seems to work was create a #Configuration import the xml that have the bean declaration (with #ImportResource) and don't scan it's package in XML.
For some reason if I declare the file in the XML the server don't start, it's strange because I have no definition anywhere that I'm using an annotation configuration.

Autowired spring bean null for cacheManager using ehcache spring annotations

I'm still new to spring and I'm trying to get ehcache spring annotations setup correctly. I'm using Spring 3.2.3 ehCache 2.4 and ehcache-spring-annotations-1.2.
When I try to access the reference to the cacheManager, it is always null. All the jars are on the build path, ehcache.xml is in the classpath and there are no xml errors. I've tried also including the classes in the component scan and using #Resource instead of Autowired. I'm stuck!
Application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
ehcache-spring-1.1.xsd">
<context:component-scan base-package="org.springframework.cache.ehcache.EhCacheManagerFactoryBean,com .defaultPackage,net.sf.ehcache.CacheManager" />
<!-- ehCache Annotation settings -->
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true"/>
</bean>
Wrapper
#Component
public final class MyCache implements Serializable {
#Autowired
private CacheManager ehCacheManager;
private getCacheManager() {
return ehCacheManger; // this is always null
}...}
It seems you are trying to use the EhCacheManagerFactoryBean as your cache manager.
Looking at Spring caching documentation, you need to declare another bean to be your CacheManager created from the factory.

Categories

Resources