Integration of JSF 2 and Spring 4 (no Spring MVC) - spring

I've been struggling for 2-3 weeks now with transforming my project (single module) into a multi module project where the modules are representing the layers of a multilayer architecture (i.e. persistence, business, domain...)
When the project was still a single module, everything worked together. I had my ManagedBean classes managed by JSF and enabled #Autowired for Spring with a workaround, so I could inject my Spring-managed classes into the jsf-managed beans. Now, this won't seem to work anymore, so I'm trying to clean my code up and orientate myself at some examples I've seen (one is the Maven archetype for jsf2 + spring 4 integration).
It seems, there are two ways suggested to do the Dependency Injection:
Do it per #ManagedProperty (But I don't want to do this all per JSF)
Do it per #Autowired and let Spring manage the Backing/Managed Beans (That's what I try to do)
Though many sources (even the Maven archetype, which is working) suggest to just annotate my Backing Bean class with Spring annotations (#Component, #Scope("session")), I always get the error
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'administration' resolved to null
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/WEB-INF/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
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" >
<context:component-scan
base-package="my.base.projectpackage" />
<context:annotation-config />
<context:spring-configured />
<!-- import the other context files for Spring -->
<import resource="classpath*:META-INF/*-springContext.xml"/>
faces-config.xml
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2" metadata-complete="false">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I've already tried to:
Enable Autowiring into a JSF managed bean (per [...].autowire(this) -> not working, NPE for the Service class)
Try it with #ManagedProperty on JSF annotated Service classes (not working, NPE for the Service class)
Try it with pure Spring classes (like mentioned above, not working identifier resolved to null for the managed bean)
I can compile and build the modules and the whole project without errors, and I've also not forgotten to put intermodular dependencies (like business module is depending on persistence module) into the pom.xml. Every module has packaging "jar" (except ofc the webapp module, which is war). Did I forget to mention something else?
I run the project on Tomcat 8 (though this should not be the source of the error).
Maybe someone of you has got a hint, idea or solution for me.
Thanks in advance.
Edit
I got my project to work again and split it into multiple modules. But the only way to do this was to really copy + paste (argh! really..) my classes into the Maven archetype project and add some minor, not important fixes (like cleaning out my POM files). When it worked there, I was curious, copied the project files back into my old project and tried it there again. Surprisingly, it still didn't work, even though there was no difference anymore which could have caused it not to work. So, the mystery is still unsolved, but after all, I could continue with my work.
Thank you for your help (or for trying to help me, since this was a lost cause) #Tiny.

Related

Spring Session and Spring Security (3.2.X RELEASE)

We have a web application built upon Spring Framework 3.2.x (3.2.12-RELEASE at moment) and Spring Security 3.2.x (i.e 3.2.5-RELEASE)
Security is implemented with classical Spring approach. In web.xml we load ApplicationContext and springSecurityFilterChain
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/applicationContext*.xml
</param-value>
</context-param>
...
<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>
As of a change in requirements, we are trying to investigate on how to permit different HttpSessions per tab (or window) within the same browser: in this scenario the same physical user can log in the application using two separate app-users.
I can understand that this is not possibile using JSESSIONID cookie approach. I'm trying using Spring Session (Spring Session and Spring Security
and Spring Session Multiple Session) but i can't manage doing it, having difficulties mixing my XML configuration with the JavaConfig approach shown in links before, Tomcat does not even start with a lot of errors.
I'm new with mixing XML and JavaConfig approch, so..can anyone give me an hint in how to proceed with Spring Session?
Are there other ways to fulfill my requirement?
Spring Session project actually contains a few sample projects that demonstrate usage with Spring XML config. Look for the samples with -xml suffix in the sample name.
Generally speaking, what you need to do is manually register appropriate #Configuration class as a #Bean. For example, if you want to use Spring Session backed by Redis, register RedisHttpSessionConfiguration. You'll find the correct #Configuration class to use by looking at appropriate #Enable*HttpSession annotation (in this case #EnableRedisHttpSession) and determining which class is imported via #Import annotation.

Spring Boot and PrettyFaces: PrettyFaces Not Loading

Summary:
When I turn on my Spring Boot application. (Running on the embedded Tomcat 8 server) I never receive the:
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] RewriteFilter starting up...
...
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] Loaded [] org.ocpsoft.rewrite.config.ConfigurationProvider [org.ocpsoft.rewrite.prettyfaces.PrettyFacesRewriteConfigurationProvider<1>]
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] RewriteFilter initialized.
Log notifications. For some reason PrettyFaces isn't starting up, and I don't know why.
Technologies: Spring Boot 1.2.0.RELEASE, Java 8, Maven for dependency management. Embedded Tomcat 8.0.15 Server.
Focusing on Java Configuration as much as possible. Previously I tried to use Rewrite, but it gave me an equal amount of gruff. Feel like I'm missing something obvious.
Here's a link to my current code base. (It's pretty small, just working on the foundation for a new project, nothing major implemented yet.)
https://github.com/MeisterGit/FoundationServer
Maven Dependency:
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>`
Other Maven Dependency Tried:
<!-- PrettyFaces -->
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>2.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-config-prettyfaces</artifactId>
<version>2.0.12.Final</version>
</dependency>
Both version yield the same result. No startup messages.
I'm trying to keep XML to an absolute minimum. I have faces-config set up with:
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<!-- Allow Spring Beans to be accessible to JSF. -->
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>`
and my controller is topped by:
#Controller
#URLMapping(id = UserController.INDEX,
pattern = "/",
viewId = "/content/index.xhtml") // Home page.`
Here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="foundation-server"
version="3.1">
<!-- PrettyFaces: Specify which package to scan for #UrlMapping annotations -->
<context-param>
<param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>
<param-value>foundation</param-value>
</context-param>
<!-- No Pretty Filter required, servlet 3.0+ automatically registers the filter. -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Any help on what I'm doing wrong? The server turns on, I can hit http://localhost:8080/content/index.xhtml just fine, and the JSF template loads. The Spring Bean backs it up. . . But no URL mapping is functioning. If I hit http://localhost:8080/ I just get an error.
When you're using Spring Boot with an embedded container web.xml and web-fragment.xml are ignored. You need to register the PrettyFaces filter in your application's Java configuration:
#Bean
public FilterRegistrationBean prettyFilter() {
FilterRegistrationBean prettyFilter = new FilterRegistrationBean(new PrettyFilter());
prettyFilter.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST,
DispatcherType.ASYNC, DispatcherType.ERROR);
prettyFilter.addUrlPatterns("/*");
return prettyFilter;
}
Spring Boot could be improved to auto-configure this filter for you if PrettyFaces is on the classpath. If you'd like to see such an enhancement, please open an issue.

trying to use spring 3 but auto wired objects are null (on tomcat)

I am trying to do my first steps using spring, but I don't manage to get it working, I would appreciate your assistance.
I am using tomcat and I have added to tomcat lib directory a file: jdbc-service.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:spring-configured />
<context:component-scan base-package="com.myproject"/>
<context:property-placeholder location="database.properties"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
I have a database.properties that defines several properties to be used to obtain DataSource object.
I also have a class that uses #Configuration and it takes the properties for the DB.
In addition i have another #Configuration class that inherits from the first one and added JdbcTemplate object each method is annotated #Bean.
I created an interface UserRepository and an implementation for this called UserRepositoryImpl - which has an annotated #Autowired member JdbcTemplate and implementation of only one method findById inherited from the interface UserRepository.
I have another interface called UserManager and it's implementation UserManagerImpl - which is annotated as #Service, it has a member UserRepository annotated with #Autowired.
it is implementing a method to findById and just tried to get the user from the DB using the UserRepository.
Last, I have a servlet that uses a member
#Autowired
private UserManager userManager;
the problem is that userManager is null. I think that the spring does not get initiated, but i don't know why. I am probably missing something important.
Please help me as I can't progress.
Thanks in advance.
Servlets are not Spring beans. They're instantiated by tomcat directly, and not by Spring. They are thus out of Spring's scope and can't be injected.
Consider using Spring MVC controllers instead of servlets.
Are you sure your UserManagerImpl is in package com.myproject which is specified as base package?
In your web.xml specify two context listeners but make sure the first listener is Spring's:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

HTTPSessionListener using Spring injections and Services => impossible to access my beans

I work on a web app using Spring annotations to inject my Controllers and Services. In the app, I have users working on projects but I need to make sure one project is only edited by one user at a time so I have an attribute in the table "Project" in the database to save if the project is open and by whom.
I need to add an HTTPSessionListener to be able to "close" the project when the user editing it disconnects. It means that at the "sessionDestroyed" event, I want to call my DAO service to update the project in the database.
The only problem is that this service is injected by Spring and I cannot get it...
I tried to use #Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (Spring – How to do dependency injection in your session listener ) but I get a nullPointerException for the WebApplicationContext...
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" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- jsp config => automatic inclusions in all jsp files -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>taglibs.jsp</include-prelude>
<include-prelude>setLanguage.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<display-name>MEANS</display-name>
<!--Spring dispatcher servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern><!-- detect all urls ending with ".do" -->
</servlet-mapping>
<!-- The welcome files -->
<welcome-file-list>
<welcome-file>connection.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout><!-- the session is automatically disconnected after 30 min of inactivity -->
</session-config>
<listener>
<listener-class>myPackages.listener.MySessionListener</listener-class>
</listener>
And the dispatcher-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring MVC Support for annotations (JSR-303) -->
<mvc:annotation-driven/>
<!-- Spring View resolver => find the jsps -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
<!-- Spring info : what packages to scan for detecting annotations -->
<context:component-scan base-package="myPackages"/>
So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?
Thanks in advance for your help!
One of the options (perhaps the best one from the design point of view) is to create a bean of scope session (don't forget to declare RequestContextListener) and put logic associated with the session lifecylce into it. Spring will automatically call its destruction method (annotated with #PreDestroy or configured as destroy-method) when session is to be destroyed.
Your approach with injection into session listener causes problems because you only have DispatcherServlet, not ContextLoaderListener, and WebApplicationContextUtils cannot obtain an application context associated with DispatcherServlet. If you choose to follow that apporach you should create a root application context and extract some of your beans into it, then you will be able to access it from session listener via WebApplicationContextUtils.

How to configure Spring 3.0 annotations based xml file?

I'm new to Spring, and searching how to configure it's XML file so all beans will be created by container? And how I can tell in application to load this file on server start-up?
Thanks in advance!
Sample available at https://anonsvn.springframework.org/svn/spring-samples/mvc-basic/. Look at the web.xml and the spring config in .../WEB-INF/spring/appServlet/servlet-context.xml.
You should specify the servlet in web.xml in following way for Spring.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
also you need to create the xml file called dispatcher-servlet.xml where you can specify the beans you would like the Spring Framework to create for you.
Hope that helps you.
Cheers.
If you're working on a Spring MVC application, follow the indication #Japan Trivedi gave you.
If case you're working on a standalone Spring application, here's an example :
<?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="package.containing.your.bean.classes" />
</beans>
Suppose this configuration file is named "spring-config.xml"
Simply put this file in the classpath of your application and in the code, initialize the Spring application context like this :
ApplicationContext context = new ClasspathXmlApplicationContext("spring-config.xml");
Then, from the context object, you'll be able to retrieve the beans, which have been automatically instantiated by Spring.
Note that this solution does not fully applied IOC concept as you actually explicitly when and what bean you retrieve.

Resources