jsf-spring-integration - moving to java config

This commit is contained in:
Slavisa Baeldung 2016-05-30 17:23:48 +02:00
parent b36e9a3a5c
commit 794e084f10
9 changed files with 79 additions and 89 deletions

View File

@ -48,6 +48,17 @@
<artifactId>spring-messaging</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
<version>${javax.servlet.version}</version>
</dependency>
<!-- logging -->
@ -98,5 +109,8 @@
<!-- logging -->
<org.slf4j.version>1.7.13</org.slf4j.version>
<logback.version>1.1.3</logback.version>
<!--Tomcat-->
<javax.servlet.version>3.1.0</javax.servlet.version>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.springintegration.config;
import com.sun.faces.config.FacesInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;
public class MainWebAppInitializer extends FacesInitializer implements WebApplicationInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(MainWebAppInitializer.class);
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
super.onStartup(classes, servletContext);
}
/**
* Register and configure all Servlet container components necessary to power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
LOGGER.info("MainWebAppInitializer.onStartup()");
sc.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true");
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.register(SpringCoreConfig.class);
sc.addListener(new ContextLoaderListener(root));
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.springintegration.config;
import com.baeldung.springintegration.dao.UserManagementDAO;
import com.baeldung.springintegration.dao.UserManagementDAOImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringCoreConfig {
@Bean
public UserManagementDAO userManagementDAO() {
return new UserManagementDAOImpl();
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.springintegration.controllers;
import com.baeldung.springintegration.dao.IUserManagementDAO;
import com.baeldung.springintegration.dao.UserManagementDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,7 +16,7 @@ public class RegistrationBean implements Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class);
@ManagedProperty(value = "#{userManagementDAO}")
transient private IUserManagementDAO userDao;
transient private UserManagementDAO userDao;
private String userName;
private String operationMessage;
@ -44,11 +44,11 @@ public class RegistrationBean implements Serializable {
this.userName = userName;
}
public void setUserDao(IUserManagementDAO userDao) {
public void setUserDao(UserManagementDAO userDao) {
this.userDao = userDao;
}
public IUserManagementDAO getUserDao() {
public UserManagementDAO getUserDao() {
return this.userDao;
}

View File

@ -1,18 +0,0 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.springintegration.dao;
/**
* @author Tayo
*/
public abstract class IUserManagementDAO implements UserManagementDAO {
@Override
public abstract boolean createUser(String userName);
}

View File

@ -1,16 +1,7 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.springintegration.dao;
/**
* @author Tayo
*/
public interface UserManagementDAO {
public boolean createUser(String newUserData);
boolean createUser(String newUserData);
}

View File

@ -1,12 +1,17 @@
package com.baeldung.springintegration.dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
public class UserManagementDAOImpl extends IUserManagementDAO {
@Repository
public class UserManagementDAOImpl implements UserManagementDAO {
private static final Logger LOGGER = LoggerFactory.getLogger(UserManagementDAOImpl.class);
private List<String> users;
@ -19,7 +24,7 @@ public class UserManagementDAOImpl extends IUserManagementDAO {
public boolean createUser(String newUserData) {
if (newUserData != null) {
users.add(newUserData);
Logger.getAnonymousLogger().log(Level.INFO, "User {0} successfully created", newUserData);
LOGGER.info("User {} successfully created", newUserData);
return true;
} else {
return false;

View File

@ -1,15 +0,0 @@
<?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/spring-context-2.5.xsd"
xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-3.1.1.RELEASE.xsd
http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd
">
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="com.baeldung.springintegration.dao.UserManagementDAOImpl" id="userManagementDAO"/>
</beans>

View File

@ -1,38 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_MODE</param-name>
<param-value>SERVER</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/index.jsf</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>