Bean 属性 'persistenceManagerFactory' 不可写或具有无效的 setter 方法

Bean property 'persistenceManagerFactory' is not writable or has an invalid setter method

我是运行maven jdo项目。我想使用注释事务,所以我根据这个设置了我的调度程序,但是我在错误之后收到这个错误。

org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'myController': Injection of autowired dependencies 
failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.titas.dao.UserDAO 
com.titas.controller.MyController.userDAO; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'userDAO' defined in ServletContext resource [/WEB-
INF/dispatcher-servlet.xml]: Error setting property values; nested 
exception is org.springframework.beans.NotWritablePropertyException: 
Invalid property 'persistenceManagerFactory' of bean class 
[com.titas.dao.UserDAOImpl]: Bean property 'persistenceManagerFactory' 
is not writable or has an invalid setter method. Does the parameter type 
of the setter match the return type of the getter?

我的dispatcher.xml是:

<!-- declare mvc to be annotation driven -->
<mvc:annotation-driven/>
<!-- provide Your Base package to scan annotations for components -->
<context:component-scan base-package="com.titas.controller"></context:component-scan>
<mvc:resources location="/resources/" mapping="/resources/**"/>
 <!-- enable the configuration of transactional behavior based on annotations-->
<tx:annotation-driven transaction-manager="txManager"/>


<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--The index controller.-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

<bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver"
    p:url="jdbc:mysql://localhost:3306/login"
    p:username="root"
    p:password="" />

<!-- PMF Bean -->
<bean id="myPmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
    <property name="jdoPropertyMap">
            <props>
                <prop key="javax.jdo.PersistenceManagerFactoryClass">org.datanucleus.api.jdo.JDOPersistenceManagerFactory</prop>
                <prop key="javax.jdo.option.ConnectionDriverName">com.mysql.jdbc.Driver</prop>
                <prop key="javax.jdo.option.ConnectionURL">jdbc:mysql://localhost:3306/login</prop>
                <prop key="javax.jdo.option.ConnectionUserName">root</prop>
                <prop key="javax.jdo.option.NontransactionalRead">true</prop>
                <prop key="javax.jdo.option.RetainValues">false</prop>
                <prop key="javax.jdo.option.DetachAllOnCommit">true</prop>
                <prop key="javax.jdo.option.Multithreaded">true</prop>
                <prop key="datanucleus.appengine.ignorableMetaDataBehavior">NONE</prop>
            </props>
    </property>
</bean>

<bean id="pmfProxy" class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy">
<property name="targetPersistenceManagerFactory" ref="myPmf"/>
<property name="allowCreate" value="false"/>
</bean>   


<bean id="txManager" class="org.springframework.orm.jdo.JdoTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceManagerFactory" ref="pmfProxy"/>
</bean>

<bean id="userDAO" class="com.titas.dao.UserDAOImpl" >
    <property name="persistenceManagerFactory" ref="pmfProxy"/>
</bean>

UserDAOImpl.java

@Repository

public class UserDAOImpl 实现 UserDAO{ 静态记录器日志 = Logger.getLogger(UserDAOImpl.class.getName());

@Autowired
@Qualifier("dataSource")
private DataSource dataSource; 
@Autowired
@Qualifier("myPmf")
private PersistenceManagerFactory persistenceManagerFactory;

HttpServletRequest request;


@Override
@Transactional
public User getUser(String user_name, String user_password) {
    PersistenceManager pm = this.persistenceManagerFactory.getPersistenceManager();
    try {
        User user=new User();
        pm.makePersistent(user);
        Query query = pm.newQuery(User.class, "this.userName == userNameParam && this.userPassword==userPasswordParam");
        query.setFilter("this.userName == userNameParam && this.userPassword==userPasswordParam");
        query.declareParameters("String lastNameParam, String userPasswordParam");
        log.info("hereeee 5");
        log.info("hereeee again 5");
        user = (User)query.execute(user_name,user_password);
        log.info("hereeee 6");
        log.info("hereeee again 6");
        log.info(user.getUserEmail()+"..........."+user.getUserProfileName());
        return user;
    }
    finally {
        pm.close();
    } 
 }

MyController.java

@Controller
//@RequestMapping(value = "/test")
public class MyController{

  static Logger log = Logger.getLogger(MyController.class.getName());
  @Autowired
  private UserDAO userDAO;
  List<User> allUser = new ArrayList<User>();

请问我需要更改的地方。 提前致谢。

你的 bean 定义是:

<bean id="userDAO" class="com.titas.dao.UserDAOImpl" >
    <property name="persistenceManagerFactory" ref="pmfProxy"/>
</bean>

在您的 UserDaoImpl 中,您还没有定义一个名为 setPersistenceManagerFactory 的方法,Spring 对此表示不满。

根据上面的 bean 定义,Spring 将通过 setter 方法在用户 dao 中注入工厂对象,该方法不存在,因此您会看到异常。

所以代替:

@Qualifier("myPmf")

你应该为代理工厂对象使用 bean 名称(虽然我没有看到使用 myPmf 的问题,这正是你试图在 userDao bean 中使用 <property name="persistenceManagerFactory" ref="pmfProxy"/> 注入 属性 的内容,因此这个建议你可以忽略这个)比如:

@Qualifier("pmfProxy")

并在您说自动装配时删除试图注入 属性 的行:

<bean id="userDAO" class="com.titas.dao.UserDAOImpl" />

根据您的 userDAO bean 定义,因为 Spring 将为您注入它。

如果您想手动指定依赖项并通过 setter 注入它,那么您需要定义 setter 方法,例如:

 .. setPersistenceManagerFactory(..) {
       ....
 }