在ServletContext资源中定义名为'userDao'的bean时出错

时间:2016-08-10 14:06:20

标签: xml hibernate spring-mvc spring-security

我的问题与其他问题一样,但我无法找到解决问题的方法。

的web.xml

<web-app 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"
version="3.0">

<display-name>Spring Security Application</display-name>

<!-- Spring MVC -->
<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>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-database.xml,
        /WEB-INF/spring-hibernate.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<!-- Spring 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>
</web-app>

调度-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

<context:annotation-config />
<context:component-scan base-package="az.nikoil.*" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<import resource="spring-database.xml" />
<import resource="spring-hibernate.xml" />
<import resource="spring-security.xml" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

弹簧database.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="jdbc:mysql://localhost:3306/equipdb" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>

弹簧hibernate.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="sessionFactoryBean"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.charSet">UTF-8</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>az.nikoil.model</value>
        </list>
    </property>
</bean>

<bean id="userDao" class="az.nikoil.dao.impl.UserDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="myUserDetailService" class="az.nikoil.service.MyUserDetailsService">
    <property name="userDao" ref="userDao" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="find*" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="userServicePointCut"
        expression="execution(* az.nikoil.service.*Service.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut" />
</aop:config>
</beans>

弹簧security.xml文件

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />

    <!-- access denied page -->
    <access-denied-handler error-page="/403" />
    <form-login login-page="/login" default-target-url="/welcome"
        authentication-failure-url="/login?error" username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" />
    <!-- enable csrf protection -->
    <csrf />
</http>

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailsService">
        <password-encoder hash="bcrypt" />
    </authentication-provider>
</authentication-manager>

</beans:beans>

MyUserDetailsS​​ervice.java

@Service
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserDAO userDAO;

@Override
@Transactional
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {

    // Programmatic transaction management
    /*
     * return transactionTemplate.execute(new
     * TransactionCallback<UserDetails>() {
     * 
     * public UserDetails doInTransaction(TransactionStatus status) {
     * com.mkyong.users.model.User user = userDao.findByUserName(username);
     * List<GrantedAuthority> authorities =
     * buildUserAuthority(user.getUserRole());
     * 
     * return buildUserForAuthentication(user, authorities); }
     * 
     * });
     */

    az.nikoil.model.User user = userDAO.findByUserName(username);
    List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRole());

    return buildUserForAuthentication(user, authorities);

}

// Converts az.nikoil.model.User user to
// org.springframework.security.core.userdetails.User
private User buildUserForAuthentication(az.nikoil.model.User user, List<GrantedAuthority> authorities) {
    return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities);
}

private List<GrantedAuthority> buildUserAuthority(Set<UserRole> userRoles) {

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

    // Build user's authorities
    for (UserRole userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
    }

    List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

    return Result;
}

public UserDAO getUserDAO() {
    return userDAO;
}

public void setUserDao(UserDAO userDAO) {
    this.userDAO = userDAO;
}

}

异常日志:

1)

Error creating bean with name 'userDao' defined in ServletContext resource [/WEB-INF/spring-hibernate.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined

2)

No bean named 'sessionFactory' is defined

1 个答案:

答案 0 :(得分:0)

请在spring-hibernate.xml中将sessionfactory id名称从sessionFactoryBean更改为sessionfactory以解决第二个错误(“没有定义名为'sessionFactory'的bean')。