-
Nov 15th, 2010, 02:59 PM
#1
Basic wiring : Static factory method takes string
I am very new to spring...
What I am trying to do is create a bean from a static factory method which takes a string and am having issues. I've looked for examples but most seem to return the same type as the factory (Singleton examples) and most don't show the use of argument passing. So I've been a bit confused.
First I'll show the xml I've got and after a working program with "###" in front of the lines where I have object creation which I want to replace with DI.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="emf"
factory-method="javax.persistence.Persistence.createEntity Manager"
class="javax.persistence.EntityManagerFactory">
<constructor-arg type="java.lang.String" value="myPersistenceUnit" />
</bean>
</beans>
************************************************** ****
package test;
import com.aerose.marnevic.db.security.Users;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
* @author ken
*/
public class DbTest extends ActionSupport{
private List<Users> list;
@Override
public String execute(){
### EntityManagerFactory emf = Persistence
### .createEntityManagerFactory("myPersistenceUnit");
### EntityManager em = emf.createEntityManager();
setList((List<Users>) em.createNamedQuery("Users.findAll")
.getResultList());
return SUCCESS;
}
/**
* @return the list
*/
public List<Users> getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List<Users> list) {
this.list = list;
}
}
Last edited by Quaternion; Nov 15th, 2010 at 03:09 PM.
-
Nov 15th, 2010, 05:42 PM
#2
Okay...
So this did it...
<bean id="emf" class="javax.persistence.Persistence" factory-method="createEntityManagerFactory">
<constructor-arg value="myPersistenceUnit" />
</bean>
My issue was that I thought that spring would have to explicitly name the resulting type of object which is a "javax.persistence.EntityManager" but apparently not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules