Tag: spring

事务标记为仅回滚:如何find原因

我在使用@Transactional方法提交事务时遇到问题: methodA() { methodB() } @Transactional methodB() { … em.persist(); … em.flush(); log("OK"); } 当我从methodA()调用methodB()时,方法成功通过,我可以在日志中看到“OK”。 但是,然后我得到 Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) […]

Spring确实存在时,找不到bean xmlconfiguration文件

我正在尝试在Spring中创build第一个bean,但加载上下文时遇到了问题。 我在src / main / resources中有一个bean的configurationXML文件。 我收到以下IOException: exception在线程“主”org.springframework.beans.factory.BeanDefinitionStoreException:IOException从类path资源parsingXML文档[src / main / resources / beans.xml]; 嵌套的exception是 java.io.FileNotFoundException:无法打开类path资源[src / main / resources / beans.xml],因为它不存在 但我不明白,因为我做了下面的代码testing: File f = new File("src/main/resources/beans.xml"); System.out.println("Exist test: " + f.exists()); 这让我真实! resources在类path中。 怎么了?

你如何在Eclipse中创build一个Spring MVC项目?

我正在尝试遵循Spring MVC的基本教程,但在Eclipse中创build一个新项目时却迷失了方向。 在我看来,大多数教程假定你知道如何在Eclipse中创build一个Spring项目。 任何build议从哪里开始或教程解释如何设置Eclipse将不胜感激。

@Autowired – 没有符合性的beantypes

我通过使用Spring和Hibernate创build实体,服务和JUnittesting来开始我的项目。 所有这一切都很好。 然后我已经添加了spring-mvc来使这个Web应用程序使用许多不同的分步教程,但是当我尝试使用@Autowired注释来制作Controller时,我在部署期间从Glassfish中收到错误。 我想,由于某种原因,spring没有看到我的服务,但经过多次尝试,我仍然无法处理它。 testing与服务 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:/beans.xml"}) 和 @Autowired MailManager mailManager; 正常工作。 没有@Autowired控制器,我可以打开我的项目在Web浏览器没有任何困难。 /src/main/resources/beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"> <context:property-placeholder location="jdbc.properties" /> <context:component-scan base-package="pl.com.radzikowski.webmail"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!–<context:component-scan base-package="pl.com.radzikowski.webmail.service" />–> <bean […]

纯Javaconfiguration的Spring 3.2 @value注解不起作用,但是Environment.getProperty起作用

我一直在这一个上打破我的头。 不知道我缺less什么。 我无法获得@Value注释在纯javaconfiguration的spring应用程序中工作(非web) @Configuration @PropertySource("classpath:app.properties") public class Config { @Value("${my.prop}") String name; @Autowired Environment env; @Bean(name = "myBean", initMethod = "print") public MyBean getMyBean(){ MyBean myBean = new MyBean(); myBean.setName(name); System.out.println(env.getProperty("my.prop")); return myBean; } } 属性文件只包含my.prop=avalue这个bean如下: public class MyBean { String name; public void print() { System.out.println("Name: " + name); } public String getName() { […]

我可以将null设置为Spring中@Value的默认值吗?

我目前正在使用像这样的@Value Spring 3.1.x注释: @Value("${stuff.value:}") private String value; 如果该属性不存在,则会将一个空string放入该variables中。 我想有null作为默认,而不是一个空的string。 当然,我也想避免在没有设置属性stuff.value时出错。

spring的数据:“删除”支持?

我正在使用Spring JPA进行数据库访问。 我能find例如findByName和countByName,为此我不必写任何方法的实现。 我希望find一些基于某些条件删除一组logging的例子。 Spring JPA是否支持deleteByName-like删除? 任何指针表示赞赏。 问候和感谢。

使用Springconfiguration文件设置系统属性

configuration : Spring 2.5,Junit 4,Log4j log4j文件位置是从系统属性指定的 ${log.location} 在运行时,使用-D java选项设置系统属性。 一切都很好。 问题/我需要什么: 在unit testing时间,系统属性未设置,文件位置未解决。 应用程序使用Spring,想简单地configurationSpring来设置系统属性。 更多信息: 要求仅用于configuration。 无法引入新的Java代码或条目到IDE中。 理想情况下,Spring的属性configuration实现可以处理这一点 – 我只是无法find正确的组合。 这个想法很接近,但需要添加Java代码: Spring的SystemPropertyInitializingBean 有什么帮助吗? 任何想法都表示赞赏。

java.lang.IllegalArgumentException:ServletContext需要configuration默认的servlet处理

我有以下testing类: @ActiveProfiles({ "DataTC", "test" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class }) public class RegularDayToTimeSlotsTest { … 这个问题似乎来自BaseTestConfiguration类: @Configuration @ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class), @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) }) public class BaseTestConfiguration { } 我系统地得到这个exception: Caused by: […]

Spring HandlerInterceptor与Servletfilter

Spring中的HandlerInterceptor现在可以被configuration为仅在特定的URL上使用<mvc:interceptors>来调用。 Servletfilter可以实现相同的function(日志logging,安全等)。 那么应该使用哪一个? 我认为使用拦截器,可以使用ModelAndView对象来处理模型,所以它具有更多的优势。 任何人都可以画出filter或拦截器相比其他优势的情况吗?