获取空指针异常,因为无法创建bean

时间:2017-03-01 11:31:15

标签: java spring wildfly



<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	default-autowire="byName"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<bean id="userRoleDAOTarget"
		class="com.revguru.crs.security.dao.UserRoleDAOImpl"
		scope="prototype">
		<property name="sessionFactory" ref="hsdSessionFactory" />
	</bean>
	<bean id="userAuthDAOTarget" class="com.revguru.crs.security.dao.AuthenticationDAOImpl" scope="prototype">
		<property name="sessionFactory" ref="hsdSessionFactory" />
	</bean>
	<bean id="hotelUserDAOTarget" class="com.revguru.crs.security.dao.HotelUserDAOImpl" scope="prototype">
		<property name="sessionFactory" ref="hsdSessionFactory" />
	</bean>
	<bean id="userRoleDataObjprocessor" class="com.revguru.crs.security.service.UserRoleProcessor" scope="prototype">
		<property name="userRoleDAO" ref="hsdSecurityObjectDAO" />
	</bean>
	<bean id="userAuthDataObjprocessor" class="com.revguru.crs.security.service.AuthenticationProcessor" scope="prototype">
		<property name="authenticationDAO" ref="hsdSecurityAuthObjectDAO" />
		<property name="messages" ref="userSectionMessageSource"/>	
	</bean>
	<bean id="hotelUserDataObjProcessor" class="com.revguru.crs.security.service.HotelUserProcessor" scope="prototype">
		<property name="hotelUserDAO" ref="hsdHotelUserDAO" />
		<property name="securitySectionLogger" ref="generalServiceLogger"/>
	</bean>
	<bean id="SECURITY_SERVICE" class="com.revguru.crs.core.service.sc.SecurityService" scope="prototype">
		<property name="userRoleProcessor" ref="userRoleDataObjprocessor" />
		<property name="userAuthProcessor" ref="userAuthDataObjprocessor" />
		<property name="hotelUserProcessor" ref="hotelUserDataObjProcessor" />
	</bean>
	<bean id="hsdSecurityObjectDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.revguru.crs.security.dao.UserRoleDAO
			</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>hibernateInterceptor</value>
				<value>userRoleDAOTarget</value>
			</list>
		</property>
	</bean>

	<bean id="hsdSecurityAuthObjectDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.revguru.crs.security.dao.AuthenticationDAO
			</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>hibernateInterceptor</value>
				<value>userAuthDAOTarget</value>
			</list>
		</property>
	</bean>
	
	<bean id="hsdHotelUserDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.revguru.crs.security.dao.HotelUserDAO
			</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>hibernateInterceptor</value>
				<value>hotelUserDAOTarget</value>
			</list>
		</property>
	</bean>
	<bean id="userSectionMessageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource" scope="prototype">
		<property name="alwaysUseMessageFormat" value="true"/>
		<property name="basenames">
		 <list>
				<value>userauthResources/hsd_user_msg</value>				
			</list>
		</property>
	</bean>	
</beans>
&#13;
&#13;
&#13;

这是我的bean文件,它正在引用hsdsessionFsctory,其定义如下:

&#13;
&#13;
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/revguru"/>
		<property name="username" value="root" />
		<property name="password" value="decoder" />
	</bean>
	
	<bean id="hsdSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
&#13;
&#13;
&#13;

当我运行我的项目时,我得到空指针异常

&#13;
&#13;
public class UserLoginController {
	
	private FacesContext facesContext = FacesContext.getCurrentInstance();
	HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private SecurityService securityService;
	
	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}
	
	public SecurityService getSecurityService() {
		return this.securityService;
	}
	
	/**
	 * Authenticate the User
	 * @return 
	 */
	public String authenticate() {
		if(session.getAttribute("menus") != null) {
			session.removeAttribute("menus");
		}
		
		if (session.getAttribute("CommonGlobalBean") != null) {
			session.removeAttribute("CommonGlobalBean");
		}
		
		CommonGlobalBean commonGlobalBean = new CommonGlobalBean();
		commonGlobalBean.setTimeZone(TimeZone.getDefault().getID());

		UserLogin userLogin =(UserLogin)session.getAttribute("UserLogin");
		
		String username = userLogin.getUserName();
		String password = userLogin.getPassword();
		
		UserInformation userInformation = null;	
		System.out.println("security serv ice  is ");
		if(securityService==null){
			System.out.println("security serv ice  is null:");
			securityService= new SecurityService();
		}
		if (userLogin != null) {
			System.out.println("username is:"+username);
			System.out.println("password is:"+password);
			
			if (securityService.authenticate(username,password)) {
				Util.log("Get User Object by Email");
				UserDataObject userDataObject = securityService.getUserObjectByEmail(userLogin.getUserName());
				Util.log("User name : "+userDataObject.getUserFirstName());
				userInformation = (UserInformation)session.getAttribute("UserObject");
				if (userInformation != null) {
					session.removeAttribute("UserObject");
				}
				userInformation = getUserInformation(userDataObject);
				session.setAttribute("UserObject", userInformation);
						
				if(!checkUserAsHotelRole(userInformation)) {
					Util.log("User does not have hotel role.");
					Util.reportError(facesContext, "login_user_not_valid_password", null);
				}

				HotelDataObject hotelDataObject = getAssignedHotel(userInformation);
				session.setAttribute("hotelDataObjectInSession", hotelDataObject);
				
				List<Menu> menus = new ArrayList<Menu>();
				for (FeatureDataObject feature : userInformation.getFeatureDataObjects()) {
					if (feature.getParentFeatureId() == null && feature.getFeatureTypeId().getId().intValue() == HSDServiceConstants.MENU && feature.getShowFeature() == 1) {
						List<MenuItem> menuItems = getMenuItems(feature.getId(), userInformation.getFeatureDataObjects());
						menus.add(new Menu(feature.getId(), feature.getFeatureName(), "", menuItems));
					}
				}
	
				String languageCode = userLogin.getLanguageCode();  
						
				if (languageCode == null) {
					userLogin.setLanguageCode("en");
					facesContext.getViewRoot().setLocale(Locale.ENGLISH);
				} else {
					if(languageCode.equals("fr")) {
						facesContext.getViewRoot().setLocale(Locale.FRENCH);
					} else {
						facesContext.getViewRoot().setLocale(Locale.ENGLISH);
					}
				}
				
				Util.log(":: languageCode :: " + userLogin.getLanguageCode());
				
				session.setAttribute("menus", menus);
				session.setAttribute("CommonGlobalBean", commonGlobalBean);
				/**
				 * get hotel general info details
				 */
				GeneralHotelController generalHotelController = new GeneralHotelController();
				generalHotelController.loadGeneralInformation();
				/**
				 * return to hotel general info page
				 */
				return "generalInfo";
			} else {
				Util.log("Invalid User.");
				Util.reportError(facesContext, "login_user_not_valid_password", null);
			}
		} 
		return "";
	} 
&#13;
&#13;
&#13;

将另一个方法调用为类,如下所示

&#13;
&#13;
public boolean authenticate(String email, String password) {		
		log.info("Authenticating : " + email);
		UserDataObject user = getUserObjectByEmail(email);
		if (user == null) {
			log.info("User does not exist for email id :" + email);
			return false;
		}
		return checkPassword(password, user.getPassword());
	}
&#13;
&#13;
&#13;

当我在我的Spring explorer中看到它时,它显示Bean参考是未知的

这是我的堆栈跟踪

&#13;
&#13;
javax.servlet.ServletException: java.lang.NullPointerException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)
io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
&#13;
&#13;
&#13;

我的春季探险家是什么红色

enter image description here

而其他人没有它

1 个答案:

答案 0 :(得分:0)

UserLoginController未在您的XML中配置为bean(至少不是您发布的XML),因此Spring对此一无所知并且您没有注入。

尝试添加此功能(当然包含完整包):

<bean id="loginController" class="com.revguru.crs.security.dao.AuthenticationDAOImpl" scope="prototype">
        <property name="securityService" ref="SECURITY_SERVICE" />
</bean>