标签: spring-annotations

在单元测试中覆盖自动装配的Bean

有一种简单的方法可以在特定的单元测试中轻松覆盖自动装配的bean吗?在编译类中只有一个类型的bean,因此在这种情况下自动装配不是问题.测试类将包含额外的模拟.在运行单元测试时,我只想指定一个额外的配置,基本上说,运行此使用测试时使用此模拟而不是标准bean.

对于我需要的内容,配置文件看起来有点矫枉过正,我不确定使用Primary注释可以实现这一点,因为不同的单元测试可能有不同的模拟.

spring spring-annotations spring-boot

49
推荐指数
4
解决办法
6万
查看次数

Spring MVC:请解释@RequestParam和@ModelAttribute之间的区别

我是Spring MVC的新手.请帮我解开文档.

文档

Spring MVC文档声明(强调我的):

  • @ModelAttribute在method参数上指示应从模型中检索参数.如果模型中不存在,则应首先实例化参数,然后将其添加到模型中.一旦出现在模型中,参数的字段应该从具有匹配名称的所有请求参数中填充.WebDataBinder类将请求参数名称(包括查询字符串参数和表单字段)与名称模型属性字段进行匹配.

  • @RequestParam 将请求参数绑定到控制器中的方法参数.

免责声明/澄清器

我知道@ModelAttribute并且@RequestParam不是相同的东西,不是相互排斥的,不执行相同的角色,并且可以同时使用,因为在这个问题中 - 实际上,@RequestParam可以用于填充字段@ModelAttribute.我的问题更多地是针对其内部运作之间的差异.

题:

@ModelAttribute(用于方法参数,而不是方法)和@RequestParam?之间的区别是什么?特别:

  • 来源:待办事项@RequestParam@ModelAttribute具有信息/人口,在URL即请求参数相同的源,其可以被提供作为被窗体/模型的元素POST编?
  • 用法:检索到的变量@RequestParam被丢弃是否正确(除非传递给模型),而检索@ModelAttribute到的变量会自动输入要返回的模型中?

或者在非常基本的编码示例中,这两个示例之间真正的工作区别是什么?

例1 @RequestParam::

// foo and bar are thrown away, and are just used (e.g.) to control flow?
@RequestMapping(method = RequestMethod.POST)
public String testFooBar(@RequestParam("foo") String foo,
@RequestParam("bar") String bar, ModelMap model) { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-annotations

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

@EntityScan和@ComponentScan之间的区别

我试图了解这里的区别.我看到一个类已经使用相同的包示例注释了它们:

@Configuration
@EntityScan("some.known.persistence")
@ComponentScan({ "some.known.persistence"})
public class ApiConfig {

}
Run Code Online (Sandbox Code Playgroud)

我理解与API文档的不同之处,但希望详细了解.这也意味着扫描的任何东西@ComponentScan都具有更广泛的可见性和Spring背景,而@EntityScan不是.如果这样使用某些属性@ComponentScan应该已经足够需要在JPA上下文中绑定,不是吗?

spring-annotations spring-boot spring-4 java-annotations

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

Spring注释之间的区别

问题:

1)@Component@Configuration?之间的区别?

我已经读过,两者都删除了将代码放入XML中的必要性,但没有区分它们.

2)之间有什么区别@Autowired,@Inject@Resource
- 什么时候使用?
- 每个的优点/缺点是什么?

java spring spring-annotations

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

Struts2 + Spring Security 2.06:尝试在Action方法上使用@Secured时,Valuestack为null

在我开始之前,我要说的是我找到的最接近的答案就在这里,但说实话,我并不真正理解那里发生了什么.

我正在使用Struts2 + Spring Security 2.06与自定义身份验证提供程序和访问决策管理器来消除对"ROLE_"前缀的需求.

我的applicationContext-security.xml看起来像这样:

    <beans:bean id="customAuthenticationProvider"
                class="com.test.testconsole.security.CustomAuthenticationProvider">
        <beans:property name="userManagementService" ref="userManagementService"/>
        <custom-authentication-provider/>
    </beans:bean>

    <!--Customize Spring Security's access decision manager to remove need for "ROLE_" prefix-->
    <beans:bean
            id="accessDecisionManager"
            class="org.springframework.security.vote.AffirmativeBased">
        <beans:property name="decisionVoters">
            <beans:list>
                <beans:ref bean="roleVoter"/>
                <beans:ref bean="authenticatedVoter"/>
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean
            id="roleVoter"
            class="org.springframework.security.vote.RoleVoter">
        <beans:property name="rolePrefix" value=""/>
    </beans:bean>

    <beans:bean
            id="authenticatedVoter"
            class="org.springframework.security.vote.AuthenticatedVoter">
    </beans:bean>

<global-method-security secured-annotations="enabled" access-decision-manager-ref="accessDecisionManager"/>
Run Code Online (Sandbox Code Playgroud)

我的web.xml的相关部分:

    <!--Spring Security-->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>

<!--Has to be placed _Before_ the struts2 filter-mapping-->
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern> …
Run Code Online (Sandbox Code Playgroud)

java spring struts2 spring-security spring-annotations

39
推荐指数
1
解决办法
2454
查看次数

为什么我们必须在Data Jpa中对查询使用@Modifying注释

例如,我的CRUD界面中有一个方法可以从数据库中删除用户:

public interface CrudUserRepository extends JpaRepository<User, Integer> {

    @Transactional
    @Modifying
    @Query("DELETE FROM User u WHERE u.id=:id")
    int delete(@Param("id") int id, @Param("userId") int userId);
}
Run Code Online (Sandbox Code Playgroud)

此方法仅适用于注释@Modifying.但是这里的注释需要什么?为什么不能分析查询并理解它是一个修改查询?

java spring spring-annotations spring-data-jpa

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

什么是春天的"刻板印象"?

在SpringSource博客条目中,以下句子引用了构造型.

因为@Controller是Spring的@Component Stereotype注释的特化,所以Spring容器会自动检测该类作为容器组件扫描过程的一部分,创建bean定义并允许实例像任何其他Spring管理的组件一样被依赖注入.

这个单词刻板印象的用法是什么?这是一个技术性的春季术语吗?或者只是在一般意义上使用的刻板印象?

spring stereotype spring-annotations

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

JavaConfig优于Spring中的XML配置?

之前的配置曾经在代码中进行了硬编码,之后将其外部化为.property文件(为了避免硬编码值,避免为了更改配置而改变代码等等),然后将其移至XML(for为了更加标准化,没有错误..等等)

现在,在阅读Spring 3中的@Configuration时,看起来我们又回到了最初的方法.

为什么我们要在代码中硬编码配置而不是将其外部化?

spring annotations spring-annotations spring-3

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

Spring @Value以默认值转义冒号(:)

我有以下使用@Value注释的属性.我有一个使用默认分隔符':'定义的默认值

@Value("${prop.url:http://myurl.com}")
Run Code Online (Sandbox Code Playgroud)

有没有办法逃避':' http://myurl.com或我必须在我的配置中定义一个不同的分隔符值.

spring spring-annotations spring-properties

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

Spring Boot中的多个DataSource和JdbcTemplate(> 1.1.0)

我想JdbcTemplate在Spring Boot项目中注入一个特定的.我尝试按照此示例进行多种DataSource配置:http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-available-now

我的代码编译并运行,但只考虑带有@Primary注释的DataSource ,无论我@QualifierSqlService类中放置什么.我的相关代码如下:

DatabaseConfig.java:

@Configuration
public class DatabaseConfig {

    @Bean(name = "dsSlave")
    @ConfigurationProperties(prefix="spring.mysql_slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dsMaster")
    @Primary
    @ConfigurationProperties(prefix="spring.mysql_master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jdbcSlave")
    @Autowired
    @Qualifier("dsSlave")
    public JdbcTemplate slaveJdbcTemplate(DataSource dsSlave) {
        return new JdbcTemplate(dsSlave);
    }

    @Bean(name = "jdbcMaster")
    @Autowired
    @Qualifier("dsMaster")
    public JdbcTemplate masterJdbcTemplate(DataSource dsMaster) {
        return new JdbcTemplate(dsMaster);
    }

}
Run Code Online (Sandbox Code Playgroud)

我做了一个快速的服务尝试:

SqlService.java:

@Component …
Run Code Online (Sandbox Code Playgroud)

java spring jdbctemplate spring-annotations spring-boot

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