● 按照AppFuse帮助,实践
实践次步时,一切都还正常,唯有最后测试时,始终不通,试了n多办法,无果。如果那位网友看到,知道结果的,烦请告知,在此先谢过了。
具体的错误分析见文尾。
Person是基本的POJO,PersonDAO是Person的管理类,PersonManager是 Business Facades. 这些façade用于连接前端和DAO层之间的通讯。
Ø 在web环境下,Spring通过配置初始化后,使用WebApplicationContextUtils来获得ApplicationContext.
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
Ø 在非web环境中,则通过ClassPathXmlApplicationContext获得ApplicationContext
ResourceBundle db = ResourceBundle.getBundle("database");
String daoType = db.getString("dao.type");
String[] paths = {"/WEB-INF/applicationContext-resources.xml",
"/WEB-INF/applicationContext-" + daoType + ".xml"};
ctx = new ClassPathXmlApplicationContext(paths);
Ø 建立PersonDAO接口
package org.dudu.dao;
import org.dudu.model.Person;
public interface PersonDAO extends DAO
{
public Person getPerson(Long personId);
public void savePerson(Person person);
public void removePerson(Long personId);
}
Ø 建立PersonDAO的的具体实现类
package org.dudu.dao.hibernate;
import org.dudu.model.Person;
import org.dudu.dao.PersonDAO;
import org.springframework.orm.ObjectRetrievalFailureException;
public class PersonDAOHibernate extends BaseDAOHibernate implements PersonDAO
{
public Person getPerson(Long personId)
{
Person person = (Person)getHibernateTemplate().get(Person.class,personId);
if(person == null)
{
throw new ObjectRetrievalFailureException(Person.class,personId);
}
return person;
}
public void removePerson(Long personId)
{
getHibernateTemplate().delete(getPerson(personId));
}
public void savePerson(Person person)
{
getHibernateTemplate().saveOrUpdate(person);
}
}
Ø 建立PersonDAO的测试类
package org.dudu.dao;
import org.dudu.model.Person;
import org.springframework.dao.DataAccessException;
public class PersonDAOTest extends BaseDAOTestCase{
private Person person = null;
private PersonDAO dao = null;
protected void setUp() throws Exception {
super.setUp();
dao = (PersonDAO)ctx.getBean("personDAO");
}
protected void tearDown() throws Exception {
super.tearDown();
dao = null;
}
public void testGetPerson() throws Exception
{
person = new Person();
person.setFirstName("dudu");
person.setLastName("tu");
dao.savePerson(person);
assertNotNull(person.getId());
person = dao.getPerson(person.getId());
assertEquals(person.getFirstName(),"dudu");
}
public void testSavePerson() throws Exception
{
person = dao.getPerson(new Long(1));
person.setFirstName("dudu");
person.setLastName("last name updated");
dao.savePerson(person);
if(log.isDebugEnabled())
{
log.debug("update person" + person);
}
}
public void testAddAndRemovePerson() throws Exception
{
person = new Person();
person.setFirstName("Zhang");
person.setLastName("San");
dao.savePerson(person);
assertEquals(person.getFirstName(),"Zhang");
assertNotNull(person.getId());
if(log.isDebugEnabled())
{
log.debug("remove person...");
}
dao.removePerson(person.getId());
try
{
person = dao.getPerson(person.getId());
fail("Person found in database");
}
catch(DataAccessException dae)
{
log.debug("Excepted exception:" + dae.getMessage());
assertNotNull(dae);
}
}
}
Ø 配置信息
Ø applicationContext-hibernate.xml
<bean id="personDAO" class="org.dudu.dao.hibernate.PersonDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
Ø applicationContext-resources.xml
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
<value>database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
</beans>
Ø database.properties
#Hibernate Configuration for JUnit tests
#Sun Jul 10 18:03:26 CST 2005
hibernate.connection.username=test
dao.type=hibernate
hibernate.connection.password=test
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc/:mysql/://localhost/appfuse?autoReconnect/=true&useUnicode/=true&characterEncoding/=utf-8
hibernate.connection.show_sql=true
hibernate.connection.driver_class=com.mysql.jdbc.Driver
Ø 运行测试:ant test-dao -Dtestcase=PersonDAO
报错:
[junit] [dudu] WARN [main] JDBCExceptionReporter.logExceptions(57) | SQL Err
or: 0, SQLState: 08001
[junit] [dudu] ERROR [main] JDBCExceptionReporter.logExceptions(58) | Server
connection failure during transaction. Due to underlying exception: 'java.net.S
ocketException: java.net.SocketException: Permission denied: connect'.
[junit] ** BEGIN NESTED EXCEPTION **
[junit] java.net.SocketException
[junit] MESSAGE: java.net.SocketException: Permission denied: connect
[junit] STACKTRACE:
[junit] java.net.SocketException: java.net.SocketException: Permission denie
d: connect
[junit] at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFa
ctory.java:156)
[junit] at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:283)
[junit] at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
[junit] at com.mysql.jdbc.Connection.<init>(Connection.java:1474)
[junit] at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDri
ver.java:264)
[junit] at java.sql.DriverManager.getConnection(DriverManager.java:525)
[junit] at java.sql.DriverManager.getConnection(DriverManager.java:171)
[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g
etConnectionFromDriverManager(DriverManagerDataSource.java:156)
[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g
etConnectionFromDriverManager(DriverManagerDataSource.java:144)
[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g
etConnection(DriverManagerDataSource.java:132)
[junit] at org.springframework.orm.hibernate.LocalDataSourceConnectionPr
ovider.getConnection(LocalDataSourceConnectionProvider.java:59)
[junit] at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFa
ctory.java:84)
[junit] at net.sf.hibernate.cfg.Configuration.buildSettings(Configuratio
n.java:1160)
[junit] at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Config
uration.java:794)
[junit] at org.springframework.orm.hibernate.LocalSessionFactoryBean.new
SessionFactory(LocalSessionFactoryBean.java:475)
[junit] at org.springframework.orm.hibernate.LocalSessionFactoryBean.aft
erPropertiesSet(LocalSessionFactoryBean.java:413)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCap
ableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:990)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCap
ableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:275)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCap
.getBean(AbstractBeanFactory.java:163)
[junit] at org.springframework.beans.factory.support.DefaultListableBean
Factory.preInstantiateSingletons(DefaultListableBeanFactory.java:230)
[junit] at org.springframework.context.support.AbstractApplicationContex
t.refresh(AbstractApplicationContext.java:310)
[junit] at org.springframework.context.support.ClassPathXmlApplicationCo
ntext.<init>(ClassPathXmlApplicationContext.java:80)
[junit] at org.springframework.context.support.ClassPathXmlApplicationCo
ntext.<init>(ClassPathXmlApplicationContext.java:65)
[junit] at org.dudu.dao.BaseDAOTestCase.<clinit>(BaseDAOTestCase.java:35
)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:164)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.
<init>(JUnitTestRunner.java:204)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.
<init>(JUnitTestRunner.java:177)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.
main(JUnitTestRunner.java:519)
[junit] ** END NESTED EXCEPTION **
Ø 错误分析
从错误报告中分析,错误原因是和Mysql数据没有建立起连接。
我尝试了多种方法,直接改写applicationContext-resources.xml文件、更好最新的mysql
驱动等,都徒劳。
但通过web容器访问,使用Jndi
(1)<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>java:comp/env/jdbc/appfuse</value></property>
</bean>
(2) ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
访问时就可以连接数据库。
不知何故,如有那位兄台知道答案,望不吝赐教。