Tag: 春季

在JUnit中使用Springtesting服务时如何回滚数据库事务?

我没有问题testing我的DAO和服务,但是当我testingINSERT或UPDATE我想回滚事务,而不是影响我的数据库。 我在我的服务中使用@Transactional来pipe理事务。 我想知道,是否有可能知道一个事务是否可以正常工作,但是为了防止改变数据库而回滚它? 这是我的testing: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/META-INF/spring.cfg.xml") @TransactionConfiguration(defaultRollback=true) public class MyServiceTest extends AbstractJUnit38SpringContextTests { @Autowired private MyService myService; @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Test public void testInsert(){ long id = myService.addPerson( "JUNIT" ); assertNotNull( id ); if( id < 1 ){ […]

有没有更好的方法来检测一个Spring DB事务是否比使用TransactionSynchronizationManager.isActualTransactionActive()有效?

我有一些遗留的代码,我现在试图在Spring下重用。 这段代码深深地嵌套在其他代码中,所以重新devise并不实际,在许多情况下被调用,其中只有一些是通过Spring。 我想要做的是使用Spring交易,如果已经开始了; 否则,继续使用现有的(遗留)数据库连接机制。 我们的第一个想法是使我们的遗留类成为一个bean,并使用一个注入的TransactionPlatformManager ,但似乎没有任何方法与我们的情况密切相关。 一些研究表明,Spring有一个名为TransactionSynchronizationManager的类,它有一个静态方法isActualTransactionActive() 。 我的testing表明这种方法是检测Spring事务是否有效的可靠方法: 当通过没有@Transactional注解的Spring服务调用时,它返回false 当通过@Transactional通过Spring服务调用时,它返回true 在我的传统方法称为现有的方式,它返回false 我的问题:有没有更好的方法来检测交易是否有效?

获取错误org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“springSecurityFilterChain”的bean

我正在运行使用Spring安全的NTLM,我收到以下错误 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“springSecurityFilterChain”的bean 我怎样才能解决这个错误? 我有以下在web.xml中定义 <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> 更新1 我解决了这个错误,现在我正在接受 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“filterSecurityInterceptor”的bean 我有以下几点 <bean id="springSecurityFilterChain" class="org.acegisecurity.util.FilterChainProxy"> <property name="filterInvocationDefinitionSource"> <value> CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON PATTERN_TYPE_APACHE_ANT /**=httpSessionContextIntegrationFilter, exceptionTranslationFilter, ntlmFilter, filterSecurityInterceptor </value> </property> </bean>` 我改变了我的applicationContext.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:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.2.xsd"> <!–<authentication-manager alias="_authenticationManager"></authentication-manager>–> <security:authentication-provider> <security:user-service> <security:user name="testuser" password="PASSWORD" authorities="ROLE_USER, ROLE_ADMIN"/> <security:user […]

为什么不使用Spring的OpenEntityManagerInViewFilter

虽然在Spring的OpenSession / EntityManagerInViewFilter的主题上写了很多post,但是我找不到任何提及它的缺陷。 据我所知,并假设使用@Transactional服务层的典型分层Web应用程序体系结构,filter的工作原理如下: filter拦截一个servlet请求 filter打开一个EntityManager并将其绑定到当前线程 networking控制器被调用 Web控制器调用服务 事务拦截器开始一个新的事务,检索线程绑定的EntityManager并将其绑定到事务 服务被调用,用EntityManager做一些东西,然后返回 事务拦截器刷新EntityManager然后提交事务 networking控制器准备视图,然后返回 视图被build立 filterclosuresEntityManager并从当前线程中解除绑定 在步骤8和9中,由线程的EntityManager加载的对象仍然被pipe理。 因此,如果在这些步骤中使用了惰性关联,那么将使用仍然打开的EntityManager从数据库加载它们。 据我所知,每个这样的访问都要求数据库打开一个事务。 Spring的事务pipe理将不知道这一点,因此我称之为“隐式事务”。 我看到2个问题: 加载多个惰性关联会导致多个数据库事务,这可能会影响性能 根对象及其惰性关联会加载到不同的数据库事务中,因此数据可能会过时(例如,线程1加载的根,线程2更新的根关联,线程1加载的根关联) 一方面,这两个问题似乎足以拒绝使用这个filter(性能命中,数据不一致)。 另一方面,这个解决scheme非常方便,避免写几行代码,问题1可能不那么明显,问题2可能是纯粹的偏执狂。 你怎么看? 谢谢!

春季安全AuthenticationManager与AuthenticationProvider?

有人能告诉我在Spring Security中AuthenticationManager和AuthenticationProvider的区别吗? 他们如何使用,他们如何被称为。 我的理解是一个SecurityFilter会调用AuthenticationManager来authentication一个Authentication对象? 但是AuthenticationProvider在哪里起作用呢? 谢谢!

匹配的通配符是严格的,但是对元素“tx:annotation-driven”没有声明。

我正在尝试configurationJSF + Spring + hibernate,而我试图运行一个testing,但是当我在application-context.xml文件中使用这个“tx:annotation-driven”时,出现这个错误: 匹配的通配符是严格的,但是对元素“tx:annotation-driven”没有声明。 这是我的application-context.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.6.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.6.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.6.xsd " xmlns:tool="http://www.springframework.org/schema/tool"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@192.168.56.101:1521:Gpsi"/> <property name="username" value="omar"/> <property name="password" value="omar"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>om.mycompany.model.Course</value> <value>om.mycompany.model.Student</value> <value>om.mycompany.model.Teacher</value> </list> […]

在Spring MVC中redirect到dynamicURL

我想让我的Spring MVC应用程序redirect到一个dynamic的URL(由用户提交)。 所以如果我有这样的代码, @RequestMapping("/redirectToSite") protected ModelAndView redirect( @RequestParam("redir_url") String redirectUrl, HttpServletRequest request, HttpServletResponse response) { // redirect to redirectUrl here return ? } 我应该写什么来redirect到提交的url? 例如, http://mySpringMvcApp/redirectToSite?redir_url=http://www.google.com应redirect到Google。

JPA @OneToMany – >父 – 子参考(外键)

我有一个关于从子实体引用ParentEntities的问题如果我有这样的事情: Parent.java: @Entity(name ="Parent") public class Parent { @Id @Generate….. @Column private int id; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent") private Set<Child> children; simple … getter and setter … } 和Child.java: @Entity(name ="Child") public class Child{ @Id @Generate…. @Column private int id; @ManyToOne private Parent parent; … simple getter an setter } […]

如何触发基于服务器响应的jquery.ajax()错误callback,而不是HTTP 500?

通过使用jQuery的ajaxfunction,我可以做一些事情: $.ajax({ url: url, type: 'GET', async: true, dataType: 'json', data: data, success: function(data) { //Handle server response here }, error: function(xhr, status, error){ //Handle failure here } }); 根据上面的代码,我有两个问题要问: 什么时候会调用jquery.ajax() errorcallback? 如果服务器响应我一个string消息“ 有一个错误 ”的JSON对象。 这意味着请求仍然发送成功,但我得到了服务器响应{message: "There is an error"} 。 我想不pipe是什么string值服务器响应,如果客户端得到服务器的响应, jquery.ajax() successcallback将被触发无论如何。 我想问一下,如果服务器专门返回给我一个string值的JSON对象,如{message: 'There is an error'} ,服务器可以做一些事情,使这个响应可以在jquery.ajax() errorcallback处理successcallback?

您在Spring MVC应用程序中使用什么命名约定?

我坚持在弹簧应用程序中弄清楚服务层的一个好的命名约定。 对于服务层中的每个类,我首先编写它应该实现的接口,然后编写实际的类。 所以例如我有以下接口: public interface UserAccountManager{ public void registerUser(UserAccount newUserAccount); public void resetPassword(UserAccount userAccount); … } 然后是实现类… 在这里有什么错误是UserAccountManager是一个实现类的好名字,所以我不得不给它一个愚蠢的名字,如SimpleUserAccountManager或UserAccountDbManager。 到目前为止,您使用的一些惯例是什么? 把实现类放在一个不同的包中并给它们起一个和接口一样的名字是个好主意? 你还有什么想在以Service结尾的名字上使用以Manager结尾的名字?