标签: spring-3

在Spring 3中创建单一测试

我开始测试应用程序,我想创建几个测试来学习Spring中的Mockito.我一直在阅读一些信息,但我有一些普遍的疑虑,我想问一下.

  1. 我已经看到了Mockito测试,他们使用:@RunWith(MockitoJUnitRunner.class)注释类的测试,而在Spring文档中,它使用@RunWith(SpringJUnit4ClassRunner.class).我不知道它们之间有什么区别,我应该使用哪个应用程序来测试使用Mockito的Spring应用程序.
  2. 由于我还没有看到任何有测试的实际应用程序,我想知道开发人员会做的典型测试.例如,在用户的典型CRUD应用程序(用户可以创建,更新......)中,任何人都可以进行常规测试.

谢谢.

java spring unit-testing mockito spring-3

7
推荐指数
1
解决办法
3507
查看次数

Spring 3:找不到AuthenticationProcessingFilter

我无法找到AuthenticationProcessingFilter从何时迁移Spring 2Spring 3(使用spring-core)

spring-security spring-3

7
推荐指数
1
解决办法
5805
查看次数

在启用HiddenHttpMethodFilter之后使用Spring MVC 3.0.2上载多个文件

重要提示:对于高于3.0.4的任何Spring版本,此问题完全无用,因为此版本中讨论的问题很久以前已在该版本中修复,并且在后续版本的Spring中不再可重现.


我使用的是Spring 3.0.2版.我需要使用multiple="multiple"文件浏览器的属性上传多个文件,例如,

<input type="file" id="myFile" name="myFile" multiple="multiple"/>
Run Code Online (Sandbox Code Playgroud)

(并没有使用多个文件浏览器,就像这个答案所说的那样,它确实有效,我试过).

虽然没有版本的Internet Explorer支持这种方法,除非使用适当的jQuery插件/小部件,我现在不关心它(因为大多数其他浏览器都支持这个).

这适用于commons fileupload,但除了使用RequestMethod.POSTRequestMethod.GET方法之外,我还想使用Spring支持和建议的其他请求方法,RequestMethod.PUT并且RequestMethod.DELETE在他们自己的适当位置.为此,我已经配置了Spring,HiddenHttpMethodFilter正如这个问题所示.

但即使选择了文件浏览器中的多个文件,它也可以一次只上传一个文件.在Spring控制器类中,方法映射如下.

@RequestMapping(method={RequestMethod.POST}, value={"admin_side/Temp"})
public String onSubmit(@RequestParam("myFile") List<MultipartFile> files, @ModelAttribute("tempBean") TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException {
    for (MultipartFile file : files) {
        System.out.println(file.getOriginalFilename());
    }
}
Run Code Online (Sandbox Code Playgroud)

即使请求参数@RequestParam("myFile") List<MultipartFile> files是一个List类型MultipartFile(它一次只能有一个文件).


我可以找到一个可能与 …

spring file-upload spring-mvc spring-3

7
推荐指数
1
解决办法
8916
查看次数

在Spring中设置Hibernate会话的刷新模式

我正在编写集成测试,在一种测试方法中,我想将一些数据写入DB然后读取它.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@TransactionConfiguration()
@Transactional
public class SimpleIntegrationTest {

    @Resource
    private DummyDAO dummyDAO;

    /**
     * Tries to store {@link com.example.server.entity.DummyEntity}.
     */
    @Test
    public void testPersistTestEntity() {
        int countBefore = dummyDAO.findAll().size();
        DummyEntity dummyEntity = new DummyEntity();
        dummyDAO.makePersistent(dummyEntity);

        //HERE SHOULD COME SESSION.FLUSH()

        int countAfter = dummyDAO.findAll().size();

        assertEquals(countBefore + 1, countAfter);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你可以存储和读取数据之间看到,会议应被刷新,因为默认FushModeAUTO因此没有数据可以实际存储在数据库中.

问题:FlushMode可以ALWAYS在会话工厂或其他地方设置如何设置以避免重复session.flush()呼叫?

DAO中的所有数据库调用都与HibernateTemplate实例有关.

提前致谢.

spring hibernate spring-3

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

什么时候在Spring中使用Hibernate的事务?

升级我的项目我在这里考虑交易.那么事情是我不太确定我何时应该在Spring中使用Hibernate查询的事务.并不是说我完全不明白什么是交易,我想我这样做,但是我是否需要使用交易进行get*类型查询而只是设置read-only属性?

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <tx:method name="get*" read-only="true" />
        <!-- other methods use the default transaction settings -->
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>
Run Code Online (Sandbox Code Playgroud)

这对get*查询有效吗?因为,据我所知,使用事务应该像更新,插入,删除和此类查询一样.我在这里错过了什么吗?

java hibernate3 spring-transactions spring-3

6
推荐指数
1
解决办法
9374
查看次数

使用Spring 3 MVC在FreeMarker中链接/包含css

我目前正在尝试在我的FreeMarker*.ftl中包含一个css文件.我还在servlet config xml文件中配置了一个资源文件夹.

<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

但是如何从FreeMarker模板访问我的css文件?

我只是尝试了以下但没有成功.

<link href="/resources/css/style.css" rel="stylesheet"  type="text/css" />
Run Code Online (Sandbox Code Playgroud)

资源文件夹位于我的spring MVC 3.0应用程序的根目录中.

/web
  /resources
    /img
    /css
  /WEB-INF
    /templates
Run Code Online (Sandbox Code Playgroud)

我的Servlet根目录定义为:

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/web/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

我的FreeMarker文件位于templates文件夹中.

freemarker spring-mvc spring-3

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

如何覆盖在应用程序上下文中定义的单个bean

我有一个访问外部Web服务的Web应用程序.我正在为Web应用程序编写自动验收测试套件.我不想调用外部Web服务,因为它有严重的开销,我想模拟这个Web服务.如何在不改变Web应用程序的应用程序上下文的情况下实现这一目标?我们最近迁移到Spring 3.1,所以我很想使用新的环境功能.这些新功能是否可以帮助我覆盖这个单一的Web服务并保留应用程序上下文的原样?

spring spring-mvc spring-3

6
推荐指数
2
解决办法
5390
查看次数

有没有办法在spring mvc3中的xml中有if/else条件?

我有一个在我的xml中定义的属性文件:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/db.properties"></property>    
</bean>
Run Code Online (Sandbox Code Playgroud)

我在文件中有一个属性:

someprop = one
Run Code Online (Sandbox Code Playgroud)

在我的XML中,我想在bean定义中添加/删除属性.例如:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.internal.url}" />
    <!--I want to add/remove the line below based on value in property file-->
    <property name="username" value="${jdbc.internal.username}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-3

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

在版本3.0.x中禁用Spring方法安全性

我有一个配置了spring security的Web应用程序,用于限制对URL和方法的访问.我想在默认情况下完全禁用它,并允许我的客户在需要时轻松打开它们(他们只能访问"spring-security.xml").

我设法关闭URL拦截,但我的方法安全性仍然启用...

任何线索?

(我不想让客户更改我的web.xml,所以不幸的是每次修改"global-method-security"设置都不是一个选项......)

这是我更新的spring-security.xml配置:

<http auto-config='true' use-expressions="true">
    <intercept-url pattern="/**" access="permitAll" />
    <http-basic />
    <anonymous />
</http>
Run Code Online (Sandbox Code Playgroud)

我已经覆盖了DelegatingFilterProxy.doFilter方法,如下所示:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
    final String springSecured = System.getProperty("springSecured");

    if (StringUtils.isNotBlank(springSecured) && springSecured.equalsIgnoreCase("true")) {
        // Call the delegate
        super.doFilter(request, response, filterChain);
    } else {
        // Ignore the DelegatingProxyFilter delegate
        filterChain.doFilter(request, response);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我拥有的方法安全性的一个例子:

@RequestMapping(
        value = "applications/{applicationName}/timeout/{timeout}",
        method = RequestMethod.POST)
public
@ResponseBody
@PreAuthorize("isFullyAuthenticated() and hasPermission(#authGroups, 'deploy')")
Object deployApplication() {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

security spring spring-security spring-3

6
推荐指数
1
解决办法
703
查看次数

如何在Spring中池对象?

我正在按照本教程关于如何在Spring中池化对象.我按照教程中的说明进行操作,但是当我运行我的应用程序时,它总是会生成一个新的对象实例.我期待,因为我正在汇集对象,现有的对象将被重用.因此,不应创建新实例.此外,当我访问bean的getter方法时,会再次创建bean的新实例.

我怎么可能做错了?我是否误解了Spring中汇集的概念?

以下是我的代码:

应用程序上下文:(这只是我的应用程序上下文的主体.)

<bean id="simpleBeanTarget" class="com.bean.SimpleBean" scope="prototype">

</bean>

<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
    <property name="targetBeanName" value="simpleBeanTarget" />
    <property name="maxSize" value="2" />
</bean>

<bean id="simpleBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="poolTargetSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)

控制器:(这只是我方法的主体)

@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response)
{
    String message = "Hello World, Spring 3.";
    try
    {
        System.out.println("Accessing Application Context");
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("Getting Bean");
        SimpleBean simpleBean = (SimpleBean) context.getBean("simpleBean");
        //A new SimpleBean... is printed here.

        System.out.println("Displaying Hello World: " + simpleBean.getRandomNum());
        //After this …
Run Code Online (Sandbox Code Playgroud)

java spring pooling spring-aop spring-3

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