Spring 如何注入此类类功能。春季公开赛
如何在这种语句中注入类UserProfile?这是使用Struts2-Spring插件 (UserProfile UserProfile=em.find(UserProfile.class,“1”);)——>我不知道如何在spring中更改这个for injection样式Spring 如何注入此类类功能。春季公开赛,spring,jakarta-ee,struts2,dependency-injection,openjpa-maven-plugin,Spring,Jakarta Ee,Struts2,Dependency Injection,Openjpa Maven Plugin,如何在这种语句中注入类UserProfile?这是使用Struts2-Spring插件 (UserProfile UserProfile=em.find(UserProfile.class,“1”);)——>我不知道如何在spring中更改这个for injection样式 package tester; /** * * @author god-gavedmework */ import javax.persistence.EntityManager; import javax.pe
package tester;
/**
*
* @author god-gavedmework
*/
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import lotmovement.business.entity.UserProfile;
/**
*
* @author god-gavedmework
*/
public class Record_exist {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("LotMovementPU");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
UserProfile userProfile = em.find(UserProfile.class,"1");
if(userProfile.getUserId().equals("tok"))
{
System.out.println("find");
System.out.println("write successful");
}
entityManagerFactory.close();
}
}
我的弹簧注射不起作用。
我已经使用setter注入了UserProfile实体,但我认为存在一个问题,因为它无法检索任何数据
private UserProfile userProfile; --> it does inject.
entityStart.em.find(UserProfile.class, userId)--> will not work in the sense that no values are retrieved.
全班
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lotmovement.business.crud;
import lotmovement.business.entity.UserProfile;
/**
*
* @author god-gavedmework
*/
public class RecordExistUserProfile {
private EntityStart entityStart;
private UserProfile userProfile;
public boolean checkrecordexist(String userId) {
entityStart.StartDbaseConnection();
entityStart.em.find(UserProfile.class, userId); ---> this one will not work.
if (userId.equals(userProfile.getUserId())) {
return true;
} else {
return false;
}
}
public boolean CheckPasswordCorrect(String userId, String password) {
entityStart.StartDbaseConnection();
UserProfile up = entityStart.em.find(UserProfile.class, userId); --> this one will work but it is not injected...
if (password.equals(up.getPassword())) {
return true;
} else {
return false;
}
}
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public EntityStart getEntityStart() {
return entityStart;
}
public void setEntityStart(EntityStart entityStart) {
this.entityStart = entityStart;
}
}
最后我找到了问题的根本原因和解决办法 根本原因: UserProfile UserProfile;-->它将注入UserProfile实体 entityStart.em.find(UserProfile.class,userId);-->这将不起作用,因为未将其传递给类变量userProfile 解决方案: UserProfile UserProfile;-->注入UserProfile实体
userProfile=entityStart.em.find(userProfile.class,userId);-->find()现在可以使用find。您到底想注入什么以及注入到哪里?而不是这样做:UserProfile up=entityStart.em.find(UserProfile.class,userId);在srping中应该做什么@aleksandr M你希望它是什么?你到底想注射什么!?为了脱钩。。这就是我在这个程序中使用spring的原因。。。。或者可能我没有得到它?我试图通过这个注入实体用户配置文件。但是,此代码不起作用。entityStart.em.find(UserProfile.class,userId);原始代码是:UserProfile UserProfile=em.find(UserProfile.class,userId)@Aleksandr M你现在明白了吗?