No candidate found for Autowired bean - java-8

At a customer, I'm working on an application that requires auditing. These auditing records should be viewable in a web page and I'm modifying the currently existing data to do just that.
Maven compiles the code without errors, but when calling the webpage to display the audit records, I receive the following exception
04-Jan-2017 15:22:25.133 WARNING [http-nio-7443-exec-7] org.glassfish.jersey.server.spring.AutowiredInjectResolver.getBeanFromSpringContext No qualifying bean of type 'my.company.app.audit.service.IAuditService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
04-Jan-2017 15:22:25.167 WARNING [http-nio-7443-exec-7] org.glassfish.jersey.internal.Errors.logErrors The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.company.app.audit.service.IAuditService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of my.company.app.audit.rest.resources.AuditResource errors were found
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on my.company.app.audit.rest.resources.AuditResource
The application class
package my.company.app.audit;
public class AuditApplication extends ResourceConfig {
public AuditApplication() {
register(AuditResource.class);
}
}
The Resource class
package my.company.app.audit.rest.resources;
#Path(AUDIT_RESOURCE_LINK)
#Produces({ MediaType.APPLICATION_JSON })
public class AuditResource extends AbstractResource {
#Autowired
private IAuditService auditService; // Commenting out this line results in the exception no longer popping up
}
The Service class
package my.company.app.audit.service;
import org.springframework.stereotype.Service;
#Service
public class AuditService implements IAuditService {
}
Audit application specific config: audit-config.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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<context:component-scan base-package="my.company.app.audit"/>
<jpa:repositories base-package="my.company.app.audit" transaction-manager-ref="txManager"/>
<aop:aspectj-autoproxy/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
<display-name>foo web_audit</display-name>
<!-- SESSION -->
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<name>WEB_AUDIT</name>
<path>/</path>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
<!-- SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml,
classpath:foo-json-serialization-beans.xml,
classpath:foo-dao-jpa-beans.xml,
classpath:foo-dao-jpa-vendor-adapter-beans.xml,
classpath:spring/foo-jpa-services-common.xml,
classpath:audit-config.xml,
classpath:iam-config.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- FILTER -->
<filter>
<filter-name>securityHeadersFilter</filter-name>
<filter-class>my.company.fooshared.web.filter.SecurityHeadersFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>securityHeadersFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SSOFilter</filter-name>
<filter-class>my.company.fooshared.webaccess.filter.WebAccessFilter</filter-class>
<init-param>
<param-name>acsUrl</param-name>
<param-value>/acs</param-value>
</init-param>
<init-param>
<param-name>logoutUrl</param-name>
<param-value>/logout</param-value>
</init-param>
<init-param>
<param-name>authRequestUrl</param-name>
<param-value>/authRequest</param-value>
</init-param>
<init-param>
<param-name>spProviderId</param-name>
<param-value>screeningutility-ci.browse.companynet.sipn.company.com</param-value>
</init-param>
<init-param>
<param-name>idProviderSSOUrl</param-name>
<param-value>https://idp.companynet.sipn.company.com/idp/profile/SAML2/POST/SSO</param-value>
</init-param>
<init-param>
<param-name>cacertPath</param-name>
<param-value>/var/lib/tomcat/cacert/</param-value>
</init-param>
<init-param>
<param-name>keystoreFile</param-name>
<param-value>/var/lib/tomcat/jks/suchannel.jks</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SSOFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- JERSEY -->
<servlet>
<servlet-name>Jersey2Dispatcher</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>my.company.app.audit.AuditApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey2Dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<error-page>
<location>/error.html</location>
</error-page>
</web-app>
I took a resembling module as example to build my code on. That other module works fine, so I must be missing something...
Any suggestion is welcome.
When adding default constructors and setting breakpoints inside them, I notice that the constructor of the AuditService class is never called.

Autowiring the class itself instead of the implemented interface solved this issue. I have no explanation of why the interface didn't work.
Edit
I have probably found the cause of the issue.
While modifying the code, I introduced some new modules in maven. Those new modules are child-modules of the module that initially contained the code.
Old situation: New siuation:
audit-module audit-module
|- src |- persistence
|- target | |- src
|- pom.xml | |- target
| |- pom.xml
|- service
| |- src
| |- target
| |- pom.xml
|- web
| |- src
| |- target
| |- pom.xml
|- pom.xml
But other modules that were relying on the old audit-module were not updated and thus were refering to the parent of the new modules. This is likely the cause that the AuditService class was not loaded properly by Spring

Related

Spring singleton #autowired service and shared state

I have a controller and a filter in which I inject a particular service.
In that service I have a Hashmap where I try to store certain information. The issue that I am running into is that although it appears that that a single instance of that service is created and injected into my controller and my filter it seems that there an two instances of the Map. I'm at a loss as to why. No matter how I tried to instantiate the map (or inject it) the behavior is still the same.
It turns out the issue is that two instances of the service are created and injected one in the controller and one in the filter. It's not clear to me why this is happening and how to resolve it.
Following is an extract of the code:
#Controller
public MyController {
#Autowired
private MyService myService;
someEndpoint() {
....
myService.putData(key, value);
.....
}
}
public class MyFilter extends GenericFilterBean {
#Autowired
private MyService myService;
public void doFilter(...) {
//this is where I have a problem.
// the reference myService.myMap seems to be pointing to a different instance
// than the service.myMap in the controller which doesn't make any sense to me
// the filter obviously intercepts all requests so I would expect that after that particular
// endpoint is accessed the data will be there for subsequent requests
myService.getData(..);
}
.....
}
#Service
public class MyService {
private Map <String,String> myMap = new HashMap <String,String> ();
public String getData(String key) {
return myMap.get(key);
}
public void putData(String key, String value){
myMap.put(key,value);
}
}
Here is an extract of the app-config.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:security="http://www.springframework.org/schema/security"
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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:component-scan base-package="com.mycompany.myPackage"/>
<context:annotation-config />
.......
<security:http
........
........
<security:custom-filter ref="myFilter" position="FORM_LOGIN_FILTER" />
....
...........
<bean class="com.mycompany.filters.MyFilter" id="myFilter"/>
and the web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher Servlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Any help is greatly appreciated.
In your web.xml you set:
<servlet>
<servlet-name>Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
which creates one web application context initiated by your servlet.
Then you also have:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</context-param>
which also creates a parent root web application context, in your case a duplicate one.
This scheme, properly used, favors the cases where you may have more servlets, each one defining its own isolated context but inherit bean definitions from a common root context (services, datasources etc). It also gives a good practice roadmap for creating layered contexts, i.e. prevent your service beans to have dependencies on your mvc layer.
Unless you have more than one configuration files, you should assign an empty value to the contextConfigLocation of your servlet configuration as :
<servlet>
<servlet-name>Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Be careful. Do not omit the parameter. Spring will infer some default configuration file name based on your servlet and will complain if it does not exist.
GenericFilterBean is not in an application context. I would expect java.lang.InstantiationException in the code above. You can get your bean via the filter's ServletContext. Any other instantiation trick will cause map duplication.
Are you sure that there is only one instance of MyService class created? The simplest way to check this is to provide default constructor implementation and print some text in it. Please check this and let me know, because I have some suspicions.
If this is true, and there are two instances of MyService class, there is possibility that when you put some data to map, you use instance A of MyService and when you get data from the map you use instance B of MyService... I will wait for your response to continue/stop this thinking.

Spring Mobile inside AuthenticationFailureHandler not found

I am development success and failure handlers in Spring Security.
Depends device type I must show one html view or send one json response. To this purpose I use Spring Mobile, but when I create Device object with HtttpServletRequest not found. Some idea?
web.xml
<filter>
<filter-name>deviceResolverRequestFilter</filter-name>
<filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>
ApplicationContext.xml
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
Class
public class AuthFailureHandler implements AuthenticationFailureHandler{
#Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ae) throws IOException, ServletException {
Device device = DeviceUtils.getCurrentDevice(request);
if(device.isNormal()){
response.sendRedirect(response.encodeRedirectURL("./userNoAuth"));
} else {
response.sendRedirect(response.encodeRedirectURL("./rest/userNoAuth"));
}
}
}
Error
java.lang.NullPointerException at com.myapp.security.handler.AuthFailureHandler.onAuthenticationFailure(AuthFailureHandler.java:19)
UPDATE:
I am change .getCurrentDevice(HttpServletRequest) method to getRequiredCurrentDevice(HttpServletRequest).
Now I get this error.
java.lang.IllegalStateException: No currenet device is set in this request and one is required - have you configured a DeviceResolvingHandlerInterceptor?
Verify your web.xml contains a filter-mapping for the deviceResolverRequestFilter. The following is a working example from the Spring Mobile Samples repository. Hope that helps!
https://github.com/SpringSource/spring-mobile-samples/tree/master/lite-device-resolver-xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Use the DeviceResolverRequestFilter OR the DeviceResolverHandlerInterceptor in the servlet-context.xml -->
<filter>
<filter-name>deviceResolverRequestFilter</filter-name>
<filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>deviceResolverRequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Issue with Spring profiles and my properties files

I am trying to activate one set of properties files for one Spring profile and another set for another Spring profile as follows:
<beans profile="cloud">
<context:property-placeholder location="classpath*:META-INF/spring/cloud/*.properties" />
</beans>
<beans profile="default">
<context:property-placeholder location="classpath*:META-INF/spring/default/*.properties" />
</beans>
I have the corresponding and appropriate directory structure in my src/main/resources folder.
I have a simple #Value("${application.url}") in one of my services and I systematically get the following error:
Error creating bean with name 'mailerServiceImpl': Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.Be anCreationException: Could not
autowire field: private java.lang.String
com.kadjoukor.service.MailerServiceImpl.websiteContext; nested
exception is java.lang.IllegalArgumentException: Could not resolve
placeholder 'application.url' in string value "${application.url}"
Note that I have tried adding a spring.profiles.active init-param to my web.xml. It doesn't make any difference...
I am not sure what I am getting wrong. Can anyone please provide advice?
EDIT 1: Could it matter that the above snippets of configuration are located at the bottom of the configuration file?
EDIT 2: Here is the output of my web.xml:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true">
<display-name>kadjoukor</display-name>
<description>Roo generated kadjoukor application</description>
<!-- Enable escaping of form submission contents -->
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml classpath:META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml</param-value></context-param>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
<!-- Handles Spring requests -->
<servlet>
<servlet-name>kadjoukor</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml classpath:META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>kadjoukor</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout><!-- TODO -->
</session-config>
<context-param><param-name>contextInitializerClasses</param-name><param-value>org.cloudfoundry.reconfiguration.spring.CloudApplicationContextInitializer</param-value></context-param></web-app>
and here is the contents of the WEB-INF/libs directory:
activation-1.1.1.jar 67.8K
antlr-2.7.6.jar 433.0K
aopalliance-1.0.jar 4.4K
asm-3.3.1.jar 42.6K
aspectjrt-1.7.0.RC1.jar 113.5K
aspectjweaver-1.7.0.RC1.jar 1.7M
auto-reconfiguration-0.6.5.jar 693.3K
cglib-2.2.2.jar 280.5K
cglib-nodep-2.2.2.jar 319.3K
commons-beanutils-1.8.3.jar 226.6K
commons-codec-1.5.jar 71.4K
commons-collections-3.2.1.jar 561.9K
commons-dbcp-1.3.jar 145.3K
commons-digester-2.1.jar 192.2K
commons-fileupload-1.2.2.jar 58.2K
commons-io-2.1.jar 159.3K
commons-lang3-3.1.jar 308.4K
commons-logging-1.0.4.jar 37.1K
commons-pool-1.5.6.jar 98.1K
dom4j-1.6.1.jar 306.5K
ehcache-core-2.6.0.jar 1.3M
flexjson-2.1.jar 79.2K
guava-11.0.2.jar 1.6M
hamcrest-core-1.1.jar 74.8K
hibernate-commons-annotations-3.2.0.Final.jar 69.6K
hibernate-core-3.6.9.Final.jar 3.0M
hibernate-entitymanager-3.6.9.Final.jar 416.3K
hibernate-jpa-2.0-api-1.0.1.Final.jar 100.3K
hibernate-validator-4.2.0.Final.jar 358.0K
httpclient-4.1.2.jar 344.0K
httpcore-4.1.2.jar 177.0K
imgscalr-lib-4.2.jar 27.2K
jackson-core-asl-1.9.9.jar 226.7K
jackson-mapper-asl-1.9.9.jar 762.0K
java-xmlbuilder-0.4.jar 18.1K
javassist-3.12.0.GA.jar 618.5K
javassist-3.16.1-GA.jar 643.9K
javax.inject-1.jar 2.4K
jcl-over-slf4j-1.6.4.jar 16.9K
jets3t-0.9.0.jar 527.1K
jmimemagic-0.1.2.jar 44.5K
joda-time-2.1.jar 557.1K
jsr305-1.3.9.jar 32.2K
jstl-api-1.2.jar 29.8K
jstl-impl-1.2.jar 382.8K
jta-1.1.jar 14.7K
junit-dep-4.8.2.jar 213.2K
log4j-1.2.16.jar 470.2K
mail-1.4.3.jar 451.3K
mysema-commons-lang-0.2.4.jar 11.8K
mysql-connector-java-5.1.18.jar 771.4K
ognl-3.0.5.jar 222.5K
oro-2.0.8.jar 63.7K
prettytime-1.0.8.Final.jar 65.4K
querydsl-core-2.9.0.jar 367.3K
querydsl-jpa-2.9.0.jar 93.3K
slf4j-api-1.6.4.jar 25.4K
slf4j-log4j12-1.6.4.jar 9.5K
spring-aop-3.2.0.RELEASE.jar 327.0K
spring-aspects-3.2.0.RELEASE.jar 68.2K
spring-beans-3.2.0.RELEASE.jar 590.6K
spring-context-3.2.0.RELEASE.jar 834.0K
spring-context-support-3.2.0.RELEASE.jar 124.1K
spring-core-3.2.0.RELEASE.jar 842.8K
spring-data-commons-core-1.3.0.RELEASE.jar 215.0K
spring-data-jpa-1.1.0.RELEASE.jar 129.8K
spring-expression-3.2.0.RELEASE.jar 189.2K
spring-jdbc-3.2.0.RELEASE.jar 391.6K
spring-js-resources-2.2.1.RELEASE.jar 4.3M
spring-orm-3.2.0.RELEASE.jar 383.0K
spring-security-acl-3.1.2.RELEASE.jar 77.7K
spring-security-config-3.1.2.RELEASE.jar 198.9K
spring-security-core-3.1.2.RELEASE.jar 332.1K
spring-security-taglibs-3.1.2.RELEASE.jar 20.3K
spring-security-web-3.1.2.RELEASE.jar 245.2K
spring-social-core-1.0.2.RELEASE.jar 113.7K
spring-social-facebook-1.0.2.RELEASE.jar 117.2K
spring-social-web-1.0.2.RELEASE.jar 18.0K
spring-tx-3.2.0.RELEASE.jar 235.3K
spring-web-3.2.0.RELEASE.jar 609.7K
spring-webmvc-3.2.0.RELEASE.jar 621.0K
thymeleaf-2.0.14.jar 677.9K
thymeleaf-extras-tiles2-1.0.0-beta3-SNAPSHOT.jar 46.7K
thymeleaf-spring3-2.0.14.jar 161.9K
tiles-api-2.2.2.jar 35.1K
tiles-core-2.2.2.jar 157.2K
tiles-jsp-2.2.2.jar 49.6K
tiles-servlet-2.2.2.jar 58.3K
tiles-template-2.2.2.jar 23.9K
validation-api-1.0.0.GA.jar 46.3K
xercesImpl-2.7.1.jar 1.1M
xml-apis-1.0.b2.jar 106.8K
xmlParserAPIs-2.0.2.jar 76.6K
Base on the https://cloudfoundry.atlassian.net/browse/CF-132 bug, in order to have the cloud profile work, you would need a ContextLoaderInitializer. So I would suggest you to create a context file, even leave it empty if you have to, to make the cloud profile work.

Setting up Vaadin and Spring 3 - Autowiring fails (annotations)

I'm having some trouble using the #Autowire annotation of spring 3 in a vaadin application.
I have made a tiny test project with just a button which executes a method in an autowired service class.
I'm getting a NullPointerException when I click the button, because the service is never injected.
I have annotated the service with #Service("calculationService")
This is my application class:
package vaadinBaas;
// imports
public class MyVaadinApplication extends Application {
private Window window;
private CalculationService calculationService;
#Autowired
public void setCalculationService(CalculationService calculationService) {
this.calculationService = calculationService;
}
#Override
public void init() {
window = new Window("Show me the magic");
setMainWindow(window);
Button button = new Button("Click Me");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
window.addComponent(new Label(String.valueOf(calculationService.multiply(10, 10))));
}
});
window.addComponent(button);
}
}
This is my spring 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:ct="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">
<ct:component-scan base-package="vaadinBaas" />
<ct:annotation-config />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
And finally my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Vaadin Web Application</display-name>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>vaadinBaas.MyVaadinApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>
Am I missing something obvious here?
Try annotating MyVaadinApplication with Component annotation. Since MyVaadinApplication is not considered as spring managed class the Autowire didnt work for you. Also you need to load the bean definition xml using ClassPathXmlApplicationContext and try calling getBean(MyVaadinApplication.class) so that all the autowiring happens automatically

AspectJ Injection in Vaadin working only after I Generate the SerialVersionID

Hi I am using Spring 3 + Spring MVC (half of the site) + Vaadin + AspectJ + JPA2 + Spring Security
My problem is that Spring creates all my Repositories and I would like to share those with Vaadin using AspectJ injection with Spring Annotations, when vaadin is started (Admin part of the site)
I have managed to make it all working after a couple days, I can use the #Configurable Spring annotation in my Vaadin Controller so It can get auto injected with my Spring context repositories,
BTW I am using Compile-Time weaving with maven, codehaus plugins and AspectJ eclipse plugin so tomcat can get the necessary libs.
BUT...
It sometimes works sometimes doesn't...
I found that when I add serializable interface to my repos it works, but only If I ask to generate the serialId and then run the app (tomcat) right after it, if I make any changes and build it again, injection is gone.
My config and Classes...
part that I think matters of my applicationContext.xml
.
.
Other stuff
<context:spring-configured />
<context:component-scan base-package="br.com.gsc" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager"/>
.
.
Other stuff
here is the Vaadin Servlet
package br.com.gsc.vaadin;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.repository.objRepos.PersonRepository;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
#Configurable(preConstruction=true,autowire=Autowire.BY_TYPE)
public class VaadinOperatorServlet extends Application {
/**
*
*/
private static final long serialVersionUID = -1481084776783567319L;
#Autowired
private transient PersonRepository pRepo;
public void init() {
createWindow();
}
public void createWindow(){
Window window = new Window();
Panel p = new Panel();
Label l = new Label("Teste");
Label l2 = new Label("");
Label l3 = new Label("");
Person person = pRepo.findPersonByID("user");
l2 = new Label(person.getUsername());
p.addComponent(l);
p.addComponent(l2);
window.addComponent(p);
setMainWindow(window);
window.getContent().setSizeFull();
}
}
My Repo
package br.com.gsc.repository.objRepos;
import java.io.Serializable;
import java.util.List;
import org.springframework.stereotype.Repository;
import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.repository.AbsRepository;
import br.com.gsc.repository.objInterfaces.IPersonRepository;
#Repository
public class PersonRepository extends AbsRepository<Person> implements IPersonRepository,Serializable{
/**
*
*/
private static final long serialVersionUID = -8520715359024018210L;
#Override
public void addPerson(Person t) {
add(t);
}
Lot's of other stuff....
}
Web.xml with the servlet routings and other stuff.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>GSC</display-name>
<welcome-file-list>
<welcome-file>/intern.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Vaadin production mode -->
<context-param>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<!-- SERVLETS -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>vaadinServlet</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>br.com.gsc.vaadin.VaadinOperatorServlet</param-value>
</init-param>
<init-param>
<description>Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>br.com.gsc.vaadin.widgetset.GscWidgetset</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- SERVLET MAPPINGS -->
<servlet-mapping>
<servlet-name>vaadinServlet</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>vaadinServlet</servlet-name>
<url-pattern>/oper/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>vaadinServlet</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- Filter OpenSession -->
<filter>
<filter-name>openEntityManager</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntityManager</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Filter OpenSession -->
<!-- Filter Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Filter Security -->
<!-- Filter HTTP Methods -->
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>spring</servlet-name>
</filter-mapping>
<!-- Filter HTTP Methods -->
</web-app>
Sounds like a problem we had, where we had correctly configured maven to compile-time weave our #Configurable's, but Eclipse did not do this automatically.
This is a feature of the m2eclipse plugin in eclipse that's lacking I think.
Because you correctly define the spring-aspects jar in the aspects path in the pom file, but eclipse doesn't know to do this as well.
So we had to manually add the AOP facet to your project that contains #Configurables, and then configure it to add the spring-aspects.jar as the aspects path.
Then when eclipse compiles, the aspects are woven, and when you run your tomcat from Eclipse, your tomcat will use woven classes, instead of non-woven classes.
Well I could not fix it by any means...
I finally used Spring roo to build a maven web app and pasted all my codes and configs there, because I knew Roo worked out of the box with Aspects.
It's now working... but I have no clue why my last project had that issue with DI outside Spring Context.

Resources