Spring #Autowired(required = true) is null [duplicate] - spring

This question already has answers here:
Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
(4 answers)
Closed 6 years ago.
I have a webmodule with JSF 2 end Spring 4.3. In a backing bean I use #Autowired for DI of a service of a JAR. In EAR module there are WAR, JAR with #Service Spring and JAR with Spring configuration file.
Below a web.xml snippet:
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>classpath:beanRefContext.xml</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>sharedContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
applicationContext.xml:
<context:annotation-config />
<context:spring-configured />
<!-- package of #Service class in jar module in EAR-- >
<context:component-scan base-package="com.ipdb.service" />
beanRefContext.xml:
<bean id="sharedContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg>
<list>
<value>spring-ctx.xml</value>
</list>
</constructor-arg> </bean>
When I Use #Autowired(required=null) in a Backing Bean the value is null (there is not any exception). My JSF bean
#Component
#ManagedBean
#ViewScoped
public class PortfolioController {
#Autowired(required = true)
private PortfolioService portfolioService;
...
Can you help me, please.

PortfolioController is considered a JSF context bean adding #Component to #ManagedBean is totally wrong you can't mark same class as bean in two different contexts (JSF and Spring ).
Two solutions either make PortfolioController a spring bean thus remove the #ManagedBean and #ViewScoped or inject PortfolioController via JSF injection annotation #ManagedProperty
#ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;

if the applicationContext.xml is in your jar dependency, then you need to add asterisk after classpath:
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
With the asterisk spring search files applicationContext.xml anywhere in the classpath not only the current project.

Related

Spring configuration file in web.xml

I'm loading my spring configuration file in web.xml file. The spring file contains many beans. How can I get the bean name in my class. Is their any way to get the bean name without providing the path name.
web.xml
<context-param>
<param-name>configDir</param-name>
<param-value>D:/akatte/config</param-value>
</context-param>
<context-param>
<param-name>springConfig</param-name>
<param-value>spring-config.xml</param-value>
</context-param>
spring-config.xml
<bean id="foNasShare" class="com.katte.galaxe.FoNas">
<property name="fname" value="arvind"/>
<property name="lname" value="katte"/>
</bean>
Pojo class
FoNas.java
class FoNas
{
//getter and setter
}
Now tell me how can I access the bean name (context.getBean("foNasShare")) in my class. spring-config.xml file is already loaded in web.xml, i don't want to load the xml file one more time. The problem is spring bean is loading but the value of the getter and setter are setting to null.

spring aop and jersey classes

so in spring xml config, i define the following pointcut:
<aop:config>
<aop:aspect ref="metricsAdviceInterceptor">
<aop:around method="invoke" pointcut="#annotation(com.mycom.MetricsAdvice)"/>
</aop:aspect>
</aop:config>
The idea is to collect metrics on methods that have the "MetricsAdvice" annotation:
class SomeClass {
#MetricsAdvice
public void someMethod(...) { ... }
}
So this all works fine when i explicitly declare the beans in my spring config:
<bean id="someBean" class="com.mycom.SomeClass" />
But i want to be able to use this annotation on jersey code, and it doesn't work. Now, in the jersey config, one adds the below to web.xml. The idea is that you're telling jersey in which packages to find various rest services. Ie, it looks for classes in the packages: com.mycom.restservices.* and instantiates them. Presumably the instantiation of these beans is being done "differently", and thus aren't getting proxied:
<servlet>
<servlet-name>JerseyWebApplication</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<display-name>Jersey Servlet</display-name>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
com.mycom.restservices.billing;
com.mycom.restservices.account;
org.codehaus.jackson.jaxrs
</param-value>
</init-param>
....
So what's "best practice" for trying to get these annotations to work on jersey beans?
Thx.
If all you need is to get rid of explicit bean declarations, you can use Spring classpath scanning feature instead of the Jersey one.
Just annotate your Jersey resources with #Component (or other similar annotations), and use <context:component-scan> to specify packages with these resources.
This way beans will be instantiated by Spring, and Spring AOP will work fine.
See also:
3.10 Classpath scanning and managed components

Beans injected into Apache Wink with Spring aren't registered

Following on from How do I inject a Spring bean into Apache Wink?
I'm now using wink-spring-support and I thought I had things set up correctly.
web.xml includes:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/wink/wink-core-context.xml
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>restServlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>restServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
META-INF/wink/wink-core-context.xml contains:
<bean class="org.apache.wink.spring.Registrar">
<property name="instances">
<set>
<ref bean="myservice" />
</set>
</property>
</bean>
<bean id="myservice" class="mystuff.ServiceImpl"/>
There's a #Autowired annotation in mystuff.ServiceImpl that injects other Spring stuff, and mystuff.ServiceImpl implements a JAX-RS annotated interface and itself includes a JAX-RS #Path("/services") annotation.
I can see Spring loading up this stuff just fine, including the myservice bean. However when I request my resources, I get a 404 not found. As Wink starts, I can see a couple of log entries that might indicate the problem:
applicationConfigLocation property was not defined
Using application classes null named in init-param applicationConfigLocation
Have I missed something somewhere? Any advice?
The problem was my misunderstanding the docs.
There is a Spring configuration META-INF/server/wink-core-context.xml provided with wink-spring-support. This registers the BeanPostProcessors that actually do the setup and must be referenced from contextConfigLocation.
I thought that I put my configuration in there, which explains why the application didn't get registered with Wink on startup.

Spring Component scanned bean not accessable

I have a UserDetailsService class annotated with #Service. I also have DAO classes annonated which are autowiring and working fine within my controllers.
The problem is when I want to wire up the UserDetailsService bean within my security-context.xml. Spring is unable to find the bean. Is it because my component-scan is in my controllers.xml file and out of the scope of my security configuration?
xml config file layout as so :
web.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/appServlet/security-context.xml
</param-value>
</context-param>
servlet-context.xml :
...
<beans:import resource="controllers.xml" />
...
Yes, you'll need to add the component scan to both contexts, it's not enough to do it in just one.

spring 3 scheduled task running 3 times

I have a very simple method scheduled to run every 10 seconds like this:
#Component
public class SimpleTask {
#Scheduled(fixedRate=10000)
public void first() {
System.out.println("Simple Task " + new Date());
}
}
Config:
<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />
My problem is that my method is being invoked 3 times every 10 seconds. It should be invoked just once. What am I doing wrong?
I use Spring Source ToolSuite with SpringSource tc Server 6.
I had this same problem. One of the causes is a bug in Spring 3.0.0. I upgraded to 3.0.5 and the repetition went down to only two.
The other cause was because my class that had the #Scheduled method was getting instantiated twice. This happened because the context config was getting loaded twice. In web.xml I was pointing my ContextLoaderListener and DispatcherServlet at the same context config file:
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
WEB-INF/applicationContext.xml is the default context config for the ContextLoaderListener. So make sure that your ContextLoaderListener and your ServletDispatcher are using different context files. I ended up creating a /WEB-INF/spring-servlet.xml without any bean definitions and it worked flawlessly.
you are mixing annotations with configuration and I dont believe you need both
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-namespace
From Documentation
Note
Make sure that you are not initializing multiple instances of the same #Scheduled annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use #Configurable on bean classes which are annotated with #Scheduled and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the #Configurable aspect, with the consequence of each #Scheduled method being invoked twice.
may be you load applicationContext multiple times ?

Resources