A small detail to note when using spring and SPRINGMVC to manage beans

Source: Internet
Author: User

have been doing the graduation design recently ... Shiro and Springmvc were used. Friends who have used Shiro know that Shiro need to write realm himself and then inject realm into SecurityManager. and SecurityManager is the class that comes with Shiro. I can not modify the source code, add annotations, so the configuration of this realm bean is the use of XML configuration method. And because the whole project is used to SPRINGMVC, the class I write myself is basically configured with annotations. And then there's the problem ... My question is basically the same as configuring and http://www.oschina.net/question/238156_142131 this article.

In a nutshell, I configured a realm bean in the shiro.xml and then injected the annotation configuration Xxxdao into the Realm class.

Shiro.xml is loaded via spring's contextloaderlistener.

  When I first started in the Shiro.xml,

<bean id= "Mysqlrealm" class= "Jiang.app.realm.MySQLRealm"/>

And then in this realm,

public class Mysqlrealm extends Authorizingrealm {private static Logger Logger = Logger.getlogger (authorizingrealm.class );    @ResourceRoleDao Roledao, @ResourceUserDao Userdao, ...}

The result is that the xxxdao that I get into realm is null ...


Then I searched the internet for the next ... But the feeling is not very reliable ... And then went to the csdn to ask the great gods.

http://bbs.csdn.net/topics/391014289


Get the revelation of 2L and then go online search the next information ... The discovery is indeed true.

The information is here:

Http://www.dewen.io/q/16682/spring%20mvc%E7%9A%84%E7%88%B6%E5%AD%90%E4%B8%8A%E4%B8%8B%E6%96%87 ( Webapplicationcontext)%e7%9a%84%e4%bd%9c%e7%94%a8%e8%8c%83%e5%9b%b4%ef%bc%8c%e5%8c%ba%e5%88%ab%e3%80%81%e5%bd% b1%e5%93%8d%e9%9d%a2%e6%8f%90%e7%8e%b0%e5%9c%a8%e5%93%aa%e9%87%8c%ef%bc%9f

  My Xxxdao is configured in the SPRINGMVC configuration (mvc-dispatcher-servlet.xml). The SPRINGMVC is a child context (configured through a servlet, specifying the configuration file location). Shiro is configured in the parent context (loaded by Contextloaderlistener). So I can't read the bean configured in Springmvc ....


When you know the problem, the solution is detailed. Configuring the Xxxdao in the parent environment will be OK .

Like what:

Web. xml

<context-param><param-name>contextconfiglocation</param-name><param-value>classpath: Spring.xmlclasspath:spring-hibernate.xml,classpath:shiro.xml</param-value></context-param>

Shiro.xml is configured as-is.

Configuration in Spring.xml

<context:component-scan base-package= "Jiang"/>

Such annotations read the Bean is in the parent environment (I originally Component-scan is configured in the Mvc-dispatcher-servlet.xml sub-environment) ...





A small detail to note when using spring and SPRINGMVC to manage beans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.