1

すべての構成に XML を使用する Spring MVC プロジェクトがありましたが、すべての XML を削除して JavaConfig (Spring Security 以外のすべて) にしました。Spring Security を機能させようとすると、プロジェクトが WEB.INF で applicationContext.xml を探して爆発していることがわかりました。私はそれを指しているものを何も持っていないので、誰がそれを必要としますか?

私のsecuirty.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.1.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security-3.1.xsd">


    <global-method-security pre-post-annotations="enabled" />

    <http use-expressions="true">
        <intercept-url access="hasRole('ROLE_VERIFIED_MEMBER')" pattern="/mrequest**" />
        <intercept-url pattern='/*' access='permitAll' />
        <form-login default-target-url="/visit" />

        <logout logout-success-url="/" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="cpilling04@aol.com.dev" password="testing" authorities="ROLE_VERIFIED_MEMBER" />
            </user-service>

        </authentication-provider>
    </authentication-manager>
</beans:beans>

これが私のwebconfigです:

@Configuration
@EnableWebMvc
@Import(DatabaseConfig.class)
@ImportResource("/WEB-INF/spring/secuirty.xml")
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";

    private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);



    @Bean
    public  ViewResolver resolver() {
        UrlBasedViewResolver url = new UrlBasedViewResolver();
        url.setPrefix("/WEB-INF/view/");
        url.setViewClass(JstlView.class);
        url.setSuffix(".jsp");
        return url;
    }


    @Bean(name = "messageSource")
    public MessageSource configureMessageSource() {
        logger.debug("setting up message source");
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(MESSAGE_SOURCE);
        messageSource.setCacheSeconds(5);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver lr = new SessionLocaleResolver();
        lr.setDefaultLocale(Locale.ENGLISH);
        return lr;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        logger.debug("setting up resource handlers");
        registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        logger.debug("configureDefaultServletHandling");
        configurer.enable();
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleChangeInterceptor());
    }

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
        mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        b.setExceptionMappings(mappings);
        return b;
    }

    @Bean
    public RequestTrackerConfig requestTrackerConfig()
    {
        RequestTrackerConfig tr = new RequestTrackerConfig();
        tr.setPassword("Waiting#$");
        tr.setUrl("https://uftwfrt01-dev.uftmasterad.org/REST/1.0");
        tr.setUser("root");

        return tr;
    }


}

これが私のDatabaseConfigです:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages= "org.uftwf")
@PropertySource(value = "classpath:application.properties")
public class DatabaseConfig  {


    private static final Logger logger = LoggerFactory.getLogger(DatabaseConfig.class);


    @Value("${jdbc.driverClassName}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Value("${hibernate.dialect}")
    private String hibernateDialect;

    @Value("${hibernate.show_sql}")
    private String hibernateShowSql;

    @Value("${hibernate.hbm2ddl.auto}")
    private String hibernateHbm2ddlAuto;

    @Bean
    public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
    {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocation(new ClassPathResource("application.properties"));
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }



    @Bean
     public DataSource dataSource()  {

        try {
            Context ctx = new InitialContext();
            return (DataSource) ctx.lookup("java:jboss/datasources/mySQLDB");
        }
        catch (Exception e)
        {

        }

        return null;
     }

    @Bean
    public SessionFactory sessionFactory()
    {

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setHibernateProperties(getHibernateProperties());
        factoryBean.setPackagesToScan("org.uftwf.inquiry.model");

        try {
            factoryBean.afterPropertiesSet();
        } catch (IOException e) {
            logger.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return factoryBean.getObject();
    }

    @Bean
    public Properties getHibernateProperties()
    {
        Properties hibernateProperties = new Properties();

        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");

        hibernateProperties.setProperty("hibernate.format_sql", "true");
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");

        hibernateProperties.setProperty("javax.persistence.validation.mode", "none");

        //Audit History flags
        hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", "true");
        hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", "true");

        return hibernateProperties;
    }



    @Bean
    public HibernateTransactionManager hibernateTransactionManager()
    {
        HibernateTransactionManager htm = new HibernateTransactionManager();
        htm.setSessionFactory(sessionFactory());
        htm.afterPropertiesSet();
        return htm;
    }


}

そして私の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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">


    <display-name>Inquiry</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>org.uftwf.inquiry.config, org.uftwf.inquiry.controller</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>





 <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>

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

<!--


 <security-constraint>
     <web-resource-collection>
         <web-resource-name>securedapp</web-resource-name>
         <url-pattern>/*</url-pattern>
     </web-resource-collection>

     <user-data-constraint>
         <transport-guarantee>CONFIDENTIAL</transport-guarantee>
     </user-data-constraint>
 </security-constraint>
-->
</web-app>

したがって、applicationContext.xmlを探しているものは何も表示されません....追加する必要がある理由を教えてください。

4

3 に答える 3

4

web.xml で構成しましたが、 context-paramContextLoaderListenerを指定していません。contextConfigLocationこの場合の動作は、そのクラスの javadoc で説明されています。

「contextConfigLocation」context-param を処理します [...] 明示的に指定されていない場合、コンテキスト実装はデフォルトの場所を使用することになっています (XmlWebApplicationContext: 「/WEB-INF/applicationContext.xml」を使用)。

したがって、ContextLoaderListenerが必要なのはapplicationContext.xmlです。

于 2013-03-18T14:13:49.393 に答える
4

Spring アプリケーションのコンテキストは階層的です。Web アプリでの典型的な配置は、コンテキスト ローダー リスナーが AC をブートストラップし、それらを「グローバルに」利用できるようにすることです。その後、個々の DispatcherServlet は、すべての Bean (通常はサービス、データ ソース、など) コンテキストローダーリスナーの AC から。いずれの場合でも、ContextLoaderListener または DispatcherServlet を指定する場合、Spring は自動的に (規則に基づいて) XML アプリケーション コンテキストを探し、それをロードしようとします。通常、これを無効にするには、空の contextConfigLocation パラメータ ("") を指定するか、代わりに Java 構成クラス (contextClass 属性) を期待するように指示します。ところで、複数の DispatcherServlets を持つことは可能です。たとえば、Spring Integration のインバウンド HTTP アダプターを 1 つ、Spring Web Services エンドポイントを別のもの、Spring MVC アプリを別のもの、Spring HTTP インボーカー エンドポイントを別のもので使用すると、それらはすべて DispatcherServlet を介して公開されます。理論的には、それらすべてを同じ DispatcherServlet で機能させることもできますが、分離することで物事が整理され、DataSource などのグローバルで高価な Bean の同じ単一インスタンスをすべて共有できます。

于 2013-03-19T18:07:34.200 に答える
0

アプリが applicationContext.xml を使用していないことを確認するには、これと同様のことを行うことができます。ここで、これらすべてがどのように連携するかを確認できます

public class MyInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) {

    //Clear out reference to applicationContext.xml
    servletContext.setInitParameter("contextConfigLocation", "");

    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext =
            new AnnotationConfigWebApplicationContext();
    rootContext.register(MySpringRootConfiguration.class);

    // Manage the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));

    //Add jersey or any other servlets
    ServletContainer jerseyServlet = new ServletContainer(new RestApplication());
    Dynamic servlet = servletContext.addServlet("jersey-servlet", jerseyServlet);
    servlet.addMapping("/api/*");
    servlet.setLoadOnStartup(1);
}
}
于 2016-12-01T01:42:27.003 に答える