2011-05-19 13 views
37

Estoy intentando configurar JSF + Spring + hibernate y estoy intentando ejecutar una prueba, pero cuando uso este "tx : anotación impulsada" en mi archivo de aplicación-context.xml, me sale este error:El comodín coincidente es estricto, pero no se puede encontrar ninguna declaración para el elemento 'tx: controlado por anotaciones'

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

aquí es mi aplicación 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> 

y aquí está mi CourseServiceImplTest. Todavía no se han puesto en práctica las pruebas:

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."); 
    } 

y aquí está la 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); 
    } 

} 

Respuesta

46

Usted tiene algunos errores en su appcontext.xml:

  • Use * -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" 
    
  • Typos en tx:annotation-driven y context:component-scan (. en lugar de -)

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

muchas gracias por su respuesta que se hace – cascadox

11

Esto es para otros (como yo :)). No olvides agregar la dependencia spring tx jar/maven. configuración También correcto en appctx es:

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"

, por la configuración incorrecta error que otros pueden tener

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

es decir, extra "/spring-tx-3.1.xsd"

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

en otras palabras, lo que hay en xmlns (namespace) debe tener una asignación adecuada en schemaLocation (espacio de nombres vs esquema).

espacio de nombres aquí es: http://www.springframework.org/schema/tx

esquema Doc de espacio de nombres es: http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

este esquema de espacio de nombres más adelante se mapea en el tarro para localizar la ruta de xsd real situado en org.springframework.transaction.config

+0

añadiendo algunas dependencias de primavera lo arregló para mí. ¡Gracias! – rogerdpack

+0

La forma en que se escribe esta respuesta hace que sea muy difícil de entender –

9

Para mí, lo que funcionó fue el orden en que se definieron los espacios de nombres en la etiqueta xsi: schemaLocation: [ya que la versión era buena y también era el administrador de transacciones]

El error fue con:

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" 

y se solucionaron con:

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

Siempre asegúrese de emparejarlos en el orden. Por ejemplo, agregue http://www.springframework.org/schema/tx y http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "uno después del otro – jprism

0

Asegúrese de que la versión de Spring y la versión de xsd sean las mismas.En mi caso, estoy usando Spring 4.1.1 para que mi xsd sea la versión * -4.1.xsd

0

Una barra inclinada (/) delante de tx y el archivo * .xml me preocuparon durante 8 horas.

Mi error:

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

Corrección:

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

De hecho uno de los personajes más/menos se las arregla para mantener a los programadores ocupados por horas!

0

Estoy aprendiendo de udemy. Seguí cada paso que mi instructor me enseñó a hacer. En la sección porquería MVC primavera, mientras que la configuración del entorno devlopment que tenía el mismo error para:

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

entonces me acaba de reemplazar

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

con

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

y

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

con

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

realidad yo visitamos estos dos sitios http://www.springframework.org/schema/mvc/ y http://www.springframework.org/schema/tx/ y se agregó la versión más reciente de la primavera-mvc y primavera-tx es decir, primavera-mvc-4.2.xsd y primavera-TX-4.2. xsd

Por lo tanto, sugiero probar esto. Espero que esto ayude. Gracias.

Cuestiones relacionadas