标签: spring-3

如何在JSP中使用PropertyPlaceholderConfigurer中指定的属性文件中的属性

在我的应用程序上下文中,我定义了属性文件

<context:property-placeholder  location="classpath:application.properties" />
Run Code Online (Sandbox Code Playgroud)

我想获得JSP页面上该文件中定义的属性的值.有没有办法做到这一点

${something.myProperty}?
Run Code Online (Sandbox Code Playgroud)

java spring spring-3

15
推荐指数
2
解决办法
3万
查看次数

Spring MVC 3:发现了模糊映射

我正在玩spring MVC 3.1并测试不同的功能.我想验证来自@ RequestMapping#value doc的以下声明

If you have a single default method (without explicit path mapping), then all requests without a more specific mapped method found will be dispatched to it. If you have multiple such default methods, then the method name will be taken into account for choosing between them
Run Code Online (Sandbox Code Playgroud)

所以我用多个默认处理程序方法创建了以下控制器

@Controller
@RequestMapping("/book")
public class BookController {

    @RequestMapping
    public @ResponseBody String greet() {
        return "Hi Book!";
    }

    @RequestMapping
    public @ResponseBody String meet() {
        return "Nice to meet …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-3

15
推荐指数
2
解决办法
5万
查看次数

Spring:将@Qualifer附加到Java配置的bean

在Spring中,您可以XML配置bean以具有限定符.如果通过Java注释配置bean,我似乎无法找到如何附加限定符.那是怎么回事?我只需使用普通的旧名称吗?

java spring spring-3

15
推荐指数
2
解决办法
4万
查看次数

"匹配的通配符是严格的,但是没有找到元素'http'的声明"错误

我正在尝试配置NTLM身份验证,但收到错误:

cvc-complex-type.2.4.c:匹配的通配符是strict,但是没有为元素'http'找到声明.

我读了很多类似错误的主题,但我找不到解决问题的方法.

导致错误的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.xsd
                            http://www.springframework.org/schema/security
                            http://www.springframework.org/schema/security/spring-security.xsd" >

    <http access-denied-page="forms/error403.jsp" entry-point-ref="ntlmEntryPoint" servlet-api-provision="false"> 
        <intercept-url pattern="forms/error403.jsp" filters="none"/>
        <intercept-url pattern="forms/**" access="ROLE_GUEST,ROLE_OPERATOR,ROLE_ADMIN" />
                <custom-filter position="PRE_AUTH_FILTER"/>
    </http>

    <authentication-manager alias="mainAuthenticationManager"/>  
    <authentication-provider user-service-ref='userDetailsService' />

    <beans:bean id="userDetailsService"
        class="service.UserInfoService">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="usersByUsernameQuery" value="select distinct name, ' ' as password, 1 as enabled from TMP_SPRING_USERS where name=?" />
        <beans:property name="authoritiesByUsernameQuery" value="select name, role from TMP_SPRING_USERS where name=?" />
    </beans:bean>

    <beans:bean id="ntlmEntryPoint"
        class="org.springframework.security.ui.ntlm.NtlmProcessingFilterEntryPoint">
        <beans:property name="authenticationFailureUrl" value="forms/error403.html" />
    </beans:bean>

    <beans:bean id="exceptionTranslationFilter"
        class="org.springframework.security.ui.ExceptionTranslationFilter">
        <beans:property name="authenticationEntryPoint" …
Run Code Online (Sandbox Code Playgroud)

java spring ntlm spring-security spring-3

15
推荐指数
1
解决办法
4万
查看次数

实体的RestTemplate帖子

我的帖子方法被调用,但我的个人资料是空的.这种方法有什么问题?我必须使用@Requestbody来使用RestTemplate吗?

Profile profile = new Profile();
profile.setEmail(email);        
String response = restTemplate.postForObject("http://localhost:8080/user/", profile, String.class);


@RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    Object postUser(@Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {

    //Profile is null
        return profile;
    }
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-3

15
推荐指数
1
解决办法
5万
查看次数

Spring转换服务 - 从List <A>到List <B>

我在Spring 3应用程序中注册了自定义转换服务.它适用于POJO,但它不适用于列表.

例如,我从转换StringRole,它工作正常,但不是List<String>List<Role>.

ClassCastExceptions在尝试注入列表时,无论它们包含什么,都可以在应用程序中飞行.转换服务调用转换为List<String>List<Role>所有.

如果你考虑一下,这是有道理的.类型擦除是罪魁祸首,转换服务实际上是看到ListList.

有没有办法告诉转换服务使用泛型?

我还有其他选择吗?

java spring type-conversion spring-3

14
推荐指数
4
解决办法
1万
查看次数

Spring 3中@Component和@Configuration之间的区别

我遇到了Spring 3提供的两个注释(@Component@Configuration)我对它们感到有些困惑.
这是我读到的关于@Component的内容

把这个"context:component"放在bean配置文件中,就意味着,在Spring中启用自动扫描功能.base-package指示存储组件的位置,Spring将扫描此文件夹并找出bean(使用@Component注释)并将其注册到Spring容器中.

所以我想知道@Configuration的用途是什么,如果@Controller将注册我的bean而不需要在spring配置xml文件中声明它们

java spring spring-3

14
推荐指数
3
解决办法
3万
查看次数

有没有办法覆盖组件扫描发现的bean?

我有一个直接提供fooBean的java配置类和通过组件扫描提供barBean.

@Configuration
@ComponentScan(basePackages = { "com.blah" })
public class Config {

    @Bean
    public FooBean fooBean {
        return new FooBean();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在测试用例中重用它,我需要用模拟替换bean:

@Configuration
@Import(Config.class)
public class TestConfig {

    @Bean
    public FooBean fooBean {
        return new FooBeanMock();
    }

    @Bean
    public BarBean barBean {
        return new BarBeanMock();
    }
}
Run Code Online (Sandbox Code Playgroud)

(这里重用Config没有多大意义,但在现实生活中我有1000豆,我只需要模拟一些)

这里fooBean被覆盖,但不是barBean.

Skipping loading bean definition for %s: a definition for bean " + "'%s' already exists. This is likely due to an override in XML.
Run Code Online (Sandbox Code Playgroud)

还有一个官方问题:https: //jira.springsource.org/browse/SPR-9682

有人知道覆盖组件扫描发现的bean的任何解决方法吗?

考虑到bean是遗留代码并且无法修改,并且其依赖项没有setter(私有属性+ @Resource).

spring dependency-injection spring-annotations spring-3

14
推荐指数
1
解决办法
1万
查看次数

在JUnit测试中解析Spring @Value表达式

这是一个Spring bean的片段:

@Component
public class Bean {

    @Value("${bean.timeout:60}")
    private Integer timeout;

    // ...
}
Run Code Online (Sandbox Code Playgroud)

现在我想用JUnit测试来测试这个bean.因此我使用SpringJUnit4ClassRunnerContextConfiguration注释.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BeanTest {

    @Autowired
    private Bean bean;

    // tests ...

    @Configuration
    public static class SpringConfiguration {
        @Bean
        public Bean bean() {
            return new Bean();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,SpringJUnit4ClassRunner无法解析@Value表达式,即使提供了默认值(抛出NumberFormatException).似乎跑步者甚至无法解析表达式.

我的测试中缺少什么?

java junit spring junit4 spring-3

14
推荐指数
1
解决办法
1万
查看次数

MVC Java Config - HandlerInterceptor不排除路径

我有一个MVC Java配置,但HandlerInterceptor不排除某些模式.

在标有xxx的行上,如果

1)我同时添加addPatterns("/**")excludePathPatterns("*.ecxld")HandlerInterceptorInterceptorRegistration时,HandlerInterceptor.preHanlde()是不是在所有调用.例如.addPathPatterns("/**").excludePathPatterns("*.ecxld")

2)我只添加excludePathPatterns("*.ecxld")HandlerInterceptor's InterceptorRegistration,HandlerInterceptor.preHanlde()仍然执行.

(其他拦截器被调用很好).

任何指针赞赏.

谢谢

@Configuration
public class MyMVCConfigurerAdapter extends WebMvcConfigurerAdapter {

 @Override
 public void addInterceptors(final InterceptorRegistry registry) {

     registry.addInterceptor(getInterceptorOne());

     registry.addInterceptor(getMyHandlerInterceptor())
                 .excludePathPatterns("*.ecxld");  // **xxx**

     registry.addInterceptor(getInterceptorTwo()
     );

 }
Run Code Online (Sandbox Code Playgroud)

java spring-mvc spring-3

13
推荐指数
2
解决办法
1万
查看次数