2011-05-19 111 views
37

我正在尝试配置JSF + Spring + hibernate和绑定来运行测试,但是当我使用这个“tx :注解驱动的”我的应用程序context.xml文件,我得到这个错误:匹配的通配符是严格的,但是对于元素'tx:annotation-driven'没有发现任何声明

The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'

这里是我的应用程序的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> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
      </props> 
     </property> 

    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <tx:annotation-driven transaction.manager="transactionManager"/> 

    <context:annotation-config/> 
    <context:component-scan base.package="com.mmycompany"/> 
</beans> 

,这里是我的CourseServiceImplTest。我仍然没有实施的测试:

public class CourseServiceImplTest { 

    private static ClassPathXmlApplicationContext context; 
    private static CourseService courseService; 
    public CourseServiceImplTest() { 
    } 

    @BeforeClass 
    public static void setUpClass() throws Exception { 
     context=new ClassPathXmlApplicationContext("application-context.xml"); 
     courseService=(CourseService) context.getBean("courseService"); 
    } 

    @AfterClass 
    public static void tearDownClass() throws Exception { 
     context.close(); 
    } 

    @Before 
    public void setUp() { 
    } 

    @After 
    public void tearDown() { 
    } 

    /** 
    * Test of getAllCourses method, of class CourseServiceImpl. 
    */ 
    @Test 
    public void testGetAllCourses() { 
     System.out.println("getAllCourses"); 
     CourseServiceImpl instance = new CourseServiceImpl(); 
     List expResult = null; 
     List result = instance.getAllCourses(); 
     assertEquals(expResult, result); 
     // TODO review the generated test code and remove the default call to fail. 
     fail("The test case is a prototype."); 
    } 

    /** 
    * Test of getCourse method, of class CourseServiceImpl. 
    */ 
    @Test 
    public void testGetCourse() { 
     System.out.println("getCourse"); 
     Integer id = null; 
     CourseServiceImpl instance = new CourseServiceImpl(); 
     Course expResult = null; 
     Course result = instance.getCourse(id); 
     assertEquals(expResult, result); 
     // TODO review the generated test code and remove the default call to fail. 
     fail("The test case is a prototype."); 
    } 

这里是CourseServiceImpl:

@Service("courseService") 
@Transactional 
public class CourseServiceImpl implements CourseService{ 

    @Autowired 
    private SessionFactory sessionFactory; 
    @Override 
    public List<Course> getAllCourses() { 
     return sessionFactory.getCurrentSession().createQuery("from Course").list();  
    } 

    @Override 
    public Course getCourse(Integer id) { 
     return (Course) sessionFactory.getCurrentSession().get(Course.class, id); 
    } 

    @Override 
    public void save(Course course) { 
     sessionFactory.getCurrentSession().saveOrUpdate(course); 
    } 

} 

回答

46

您的appcontext.xml一些错误:

  • 使用* -2.5。 xsd

    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.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" 
    
  • 错别字tx:annotation-drivencontext:component-scan(。而不是 - )

    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <context:component-scan base-package="com.mmycompany" /> 
    
+0

非常感谢您的回答,它已完成 – cascadox 2011-05-20 08:27:42

11

这是给别人(像我:))。不要忘记添加spring tx jar/maven依赖项。在appctx 也是正确的配置是:

xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"

,误配置错误可能使他人有

xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" 

即额外的 “/spring-tx-3.1.xsd”

xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"

换句话说,xmlns(namespace)中有什么应该在 schemaLocation(命名空间vs模式)中有适当的映射。这里

命名空间为:http://www.springframework.org/schema/tx

架构文件的命名空间是:http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

这种模式命名空间的更高罐中映射找到位于org.springframework.transaction.config

实际XSD的路径
+0

添加了一些弹簧依赖关系为我解决了它。谢谢! – rogerdpack 2013-04-12 20:57:08

+0

写这个答案的方式让人很难理解 – 2016-11-23 06:49:56

9

对我来说,工作的东西是在xsi:schemaLocation标记中定义名称空间的顺序:[因为版本一切都很好,并且它已经是事务管理器]

错误是用:

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 

及与解决:

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
+1

总是要确保你按顺序配对。例如,一个接一个地添加http://www.springframework.org/schema/tx和 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd – jprism 2015-01-09 19:56:14

0

请确保Spring版本和xsd版本都一样。在我的情况下,我使用的是Spring 4.1.1,所以我所有的xsd都应该是version * -4.1.xsd

0

tx和* .xml文件前面的一个额外正斜杠(/)困扰了我8个小时!!

我的错误:

http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 

更正:

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 

事实上一个字符以下/以上设法保持程序员忙碌了几个小时!

0

我正在从udemy学习。我跟着我的老师指示我做的每一步。 在Spring MVC的CRUD节而设置的商发展环境我有同样的错误:

<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> 

然后我刚刚更换

http://www.springframework.org/schema/mvc/spring-mvc.xsd 

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 

http://www.springframework.org/schema/tx/spring-tx.xsd 

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 

其实我访问了这两个网站 http://www.springframework.org/schema/mvc/http://www.springframework.org/schema/tx/ 和刚添加的最新版本的弹簧MVC和弹簧TX即弹簧MVC-4.2.xsd和弹簧TX-4.2。 xsd

所以,我建议尝试一下。 希望这有助于。 谢谢。

相关问题