当前位置: 首页 > 知识库问答 >
问题:

获取bean类[]的错误无效属性“packagesToScan”..].Bean属性“packagesToScan”不可写或具有无效的setter方法

太叔超英
2023-03-14

我得到以下错误

原因:org.springframework.beans.NotWritablePropertyException:bean类的无效属性'packagesToScan'[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]:Bean属性'packagesToScan'不可写或具有无效的setter方法。setter的参数类型是否与getter的返回类型匹配?\

我用的是spring3.2,依赖项是用MAven添加的。一切似乎都没问题。为什么我会得到这个错误。这是我的文件

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                    http://www.springframework.org/schema/data/jpa 
                    http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

....
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>        
    <property name="packagesToScan" value="pk.training.basitMahmood.domain"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
            <prop key="hibernate.show_sql">true</prop>              
        </props>        
    </property>
</bean>    

<context:annotation-config/> 

.....

</beans>

谢谢

共有3个答案

微生智刚
2023-03-14

查看Spring 3.2.3源代码,我注意到实际上在组织.Spring框架.orm.jpa.本地容器实体管理器工厂豆方法中没有获取包ToScan(),所以,豆类实用程序是正确的启动这个例外!

殳俊晤
2023-03-14

我已经解决了这个问题。事实上,我认为我的POM是不正确的。我正在做spring MVC项目,首先我的POM如下。我认为使用这个POM我有太多的jar冲突,因为maven使用传递方法来下载jar。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pk.training.basitMahmood</groupId>
<artifactId>ch17_ThemingAndTemplating</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<name>ch17_ThemingAndTemplating Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework-version>3.2.2.RELEASE</org.springframework-version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jpa</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.5.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.0.Beta2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.0.Beta2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Draft-16</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.171</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-hibernate</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-jsptags</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>
</dependencies>

<build>
    <finalName>ch17_ThemingAndTemplating</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
                <source>1.6</source>
                <target>1.6</target> 
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

但当我把POM改为

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pk.training.basitMahmood</groupId>
    <artifactId>ch17_WebApplicationWithSpring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <name>ch17_WebApplicationWithSpring Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java-version>1.6</java-version>
    <org.springframework-version>3.2.3.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>

    <!-- Hibernate entity manager with JPA 2 support. -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.0.Beta2</version>
    </dependency>

    <!-- Hibernate’s implementation of JSR-303. -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
    </dependency>

    <!-- The JSR-303 Bean Validation API library. -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>

    <!-- H2 database for embedded JDBC database. -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.171</version>
    </dependency>

    <!-- Joda-time (joda-time.sourceforge.net/) is a date-time API that Spring Data JPA uses. We will 
    use it in our domain objects too. -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.2</version>
    </dependency>

    <!-- Joda-time library for integration with Hibernate for date-time data persistence. For hibernate 3
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-hibernate</artifactId>
        <version>1.3</version>
    </dependency>
    -->
    <dependency>
        <groupId>org.jadira.usertype</groupId>   <!-- Hibernate 4  -->
        <artifactId>usertype.core</artifactId>
        <version>3.1.0.CR6</version>
    </dependency>

    <!-- Contains useful helper classes. -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

    <!-- Logging 
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
<finalName>WebApplicationWithSpring</finalName>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <encoding>${project.build.sourceEncoding}</encoding>
            <source>1.6</source>
            <target>1.6</target>
            <!-- specifiy which directory within the project hierarchy will be considered as the root directory from the 
                 generated war file 
            <warSourceDirectory>WebContent</warSourceDirectory>
            -->
        </configuration>

    </plugin>
</plugins>
</build>
</project>

然后事情开始运转。错误消失了。希望这也能帮助其他人。谢谢

郎仰岳
2023-03-14

不知道你的包裹ToScan?希望您使用注释,如@Table、@Entity、@column…,您不需要扫描任何包。当您尝试对一个实体进行crud时,hibernate将读取注释我的答案是删除该行

更新config.dao.xml

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml" />
    <property name="dataSource" ref="dataSource" />

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">10</prop>
        </props>
    </property>
</bean>

persitence.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

    </persistence-unit>

</persistence>
 类似资料:
  • 这是我的实用程序java文件: Spring-servlet.xml: 当我运行时,我得到以下错误: org.springframework.beans。NotWritablePropertyException:bean类[com.chrysler.Utilities.HibernateUtil]的属性“sessionFactory”无效:bean属性“sesionFactory'不可写或具有无效

  • 我知道这个问题已经被问了很多次,但在我看来一切都是正确的。我还从Eclipse中删除了我的代码,让IDE创建getter/setter,但没有用。 这是我在网络逻辑中的错误: 原因:豆类属性“gpsDataAllStopsSql”无效:bean 类的属性“gpsDataAllStopsSql”无效:bean 属性“gpsDataAllSTopsSql”无效,或者具有无效的 setter 方法。二极

  • 我是Spring的新手,我有个例外。 我有一个jsp,有很多选择。它们都工作正常,但有一个显示了这个例外: 我正在以与其他工作正常的人相同的方式实施selct。下面是jsp代码: 这里是控制器: 这里,实体Expediente: 最后是TipoExpedinte实体 我认为这可能发生,因为PharmantePadre属性是同一实体Pharmante的外键,但我不知道如何解决它。。。 要帮忙吗?谢谢

  • 我是新来的Spring靴。我在基于javax的验证上遇到了麻烦。控制器期望从POSTendpoint获得json obj的列表。在JSON请求json数组中,如果或为空,我得到正确的400错误。但是,当或为空时,它会抛出500个带有错误消息的异常: 控制器。JAVA 汽车JAVA Engine.java CollectionValidator。JAVA 验证者Advice.java 请求/响应:U

  • 在“项目创建”视图中,我需要一个选择框,该框显示所有拥有类似Chef Projet的Rolename的用户 以下是Projet、Usilizateur最重要的摘录: 用户: 当提交表格时,被选中的用户将被分配到项目中。 Projets。jsp: 管理员控制器: 这是堆栈跟踪:

  • 我收到以下错误“bean类[java.util.ArrayList]的无效属性'username]:bean属性'username'不可读或具有无效的getter方法”。 虽然我有正确的表单bean,名称为username,但是我仍然收到了这个错误,你能帮我解决这个错误吗。。? 我的jsp页面在下面 下面提供了我的控制器