spring 匹配的通配符是严格的,但找不到元素 'tx:annotation-driven' 的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6058037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:30:19  来源:igfitidea点击:

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

springtransactionsspring-transactions

提问by cascadox

I am trying to configure JSF+Spring+hibernate and I'm tying to run a test but when I use this "tx:annotation-driven" on my application-context.xml file, I get this error:

我正在尝试配置 JSF+Spring+hibernate 并且我正准备运行一个测试但是当我在我的 application-context.xml 文件上使用这个“tx:annotation-driven”时,我收到这个错误:

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

匹配的通配符是严格的,但找不到元素 'tx:annotation-driven' 的声明

Here is my application-context.xml:

这是我的 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>
       </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>

and here is my CourseServiceImplTest. I have still not implemented the tests:

这是我的 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.");
    }

and here is the CourseServiceImpl:

这是 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);
    }

}

回答by abalogh

You have some errors in your appcontext.xml:

您的 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 in tx:annotation-drivenand context:component-scan(. instead of -)

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:component-scan base-package="com.mmycompany" />
    
  • 使用 *-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" />
    

回答by dillip pattnaik

This is for others (like me :) ). Don't forget to add the spring tx jar/maven dependency. Also correct configuration in appctx is:

这是给其他人的(比如我:))。不要忘记添加 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"

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

, by mistake wrong configuration which others may have

, 错误的配置,其他人可能有

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

i.e., extra "/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"

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

in other words what is there in xmlns(namespace) should have proper mapping in schemaLocation (namespace vs schema).

namespace here is : http://www.springframework.org/schema/tx

schema Doc Of namespace is : http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

this schema of namespace later is mapped in jar to locate the path of actual xsd located in org.springframework.transaction.config

换句话说,xmlns(namespace) 中的内容应该在 schemaLocation (namespace vs schema) 中有正确的映射。

这里 的命名空间是:http: //www.springframework.org/schema/tx

schema Doc 的命名空间是:http: //www.springframework.org/schema/tx/spring-tx-3.1.xsd

这个命名空间的架构后来是在 jar 中映射以定位位于 org.springframework.transaction.config 中的实际 xsd 的路径

回答by Roshan Khandelwal

For me the thing that worked was the order in which the namespaces were defined in the xsi:schemaLocation tag : [ since the version was all good and also it was transaction-manager already ]

对我来说,起作用的是在 xsi:schemaLocation 标签中定义命名空间的顺序:[因为版本都很好,而且它已经是事务管理器]

The error was with :

错误是:

 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"

AND RESOLVED WITH :

并通过以下方式解决:

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"

回答by capnmojo

In my case this was actually a symptom of the server, hosted on AWS, lacking an IP for the external network. It would attempt to download namespaces from springframework.org and fail to make a connection.

就我而言,这实际上是托管在 AWS 上的服务器缺少外部网络 IP 的症状。它会尝试从 springframework.org 下载命名空间并且无法建立连接。

回答by Skandy

One extra forward slash (/) in front of tx and the *.xml file troubled me for 8 hours!!

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

My mistake:

我的错:

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

Correction:

更正:

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

Indeed one character less/more manages to keep programmers busy for hours!

事实上,少/多一个字符就能让程序员忙上几个小时!

回答by Shafqat Shafi

I'm learning from udemy. I followed every step that my instructor show me to do. In spring mvc crud section while setting up the devlopment environment i had the same error for:

我正在向 udemy 学习。我按照老师教我做的每一步。在 spring mvc crud 部分,在设置 devlopment 环境时,我遇到了同样的错误:

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

then i just replaced

然后我就换了

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

with

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

and

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

with

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

actually i visited these two sites http://www.springframework.org/schema/mvc/and http://www.springframework.org/schema/tx/and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and spring-tx-4.2.xsd

实际上我访问了这两个站点 http://www.springframework.org/schema/mvc/http://www.springframework.org/schema/tx/并且刚刚添加了最新版本的 spring-mvc 和 spring-tx 即, spring-mvc-4.2.xsd 和 spring-tx-4.2.xsd

So, i suggest to try this. Hope this helps. Thank you.

所以,我建议试试这个。希望这可以帮助。谢谢你。

回答by Saravana Kumaran

Any one can help for me!!!!!!!!!
<?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:jee="http://www.springframework.org/schema/jee"
        xmlns:lang="http://www.springframework.org/schema/lang"
        xmlns:p="http://www.springframework.org/schema/p"
        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.xsd
            http://www.springframework.org/schema/aop/ http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee/ http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/lang/ http://www.springframework.org/schema/lang/spring-lang.xsd
            http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
        <context:annotation-config />(ERROR OCCUR)
        <context:component-scan base-package="hiberrSpring" /> (ERROR OCCUR)
        <bean id="jspViewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/WEB-INF/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basename" value="classpath:messages"></property>
            <property name="defaultEncoding" value="UTF-8"></property>
        </bean>
        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
            p:location="/WEB-INF/jdbc.properties"></bean>
        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
            p:driverClassName="${com.mysql.jdbc.Driver}"
            p:url="${jdbc:mysql://localhost/}" p:username="${root}"
            p:password="${rajini}"></bean>
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
            <property name="configurationClass">
                <value>org.hibernate.cfg.AnnotationConfiguration</value>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${org.hibernate.dialect.MySQLDialect}</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="employeeDAO" class="hiberrSpring.EmployeeDaoImpl"></bean>
        <bean id="employeeManager" class="hiberrSpring.EmployeeManagerImpl"></bean>
        <tx:annotation-driven /> (ERROR OCCUR)
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    </beans>

回答by Return_Of_The_Archons

FWIW I had this same issue. Turned out my xsi:schemaLocation entries were incorrect, so I went to the official docs and pasted theirs into mine:

FWIW 我有同样的问题。结果我的 xsi:schemaLocation 条目不正确,所以我去了官方文档并将他们的粘贴到我的:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.htmlsection 16.5.6

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html16.5.6 节

I had to add a couple more but that was ok. Next up is to find out whythis fixed the problem...

我不得不再添加几个,但没关系。接下来是找出为什么这解决了问题......

回答by Divy Kumar

Make sure that Spring version and xsd version both are same.In my case I am using Spring 4.1.1 so my all xsd should be version *-4.1.xsd

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