2
林学习弹簧(2和3)和i得到这个方法在ClientDao弹簧JDBC DAO
public Client getClient(int id) {
List<Client> clients= getSimpleJdbcTemplate().query(
CLIENT_GET,
new RowMapper<Client>() {
public Client mapRow(ResultSet rs, int rowNum) throws SQLException {
Client client = new ClientImpl(); // !! this (1)
client.setAccounts(new HashSet<Account>()); // !! this (2)
client.setId(rs.getInt(1));
client.setName(rs.getString(2));
return client;
}
},id
);
return clients.get(0);
}
和下面的弹簧布线:
<bean id="account" class="client.AccountRON" scope="prototype">
<property name="currency" value = "RON" />
<property name="ammount" value="0" />
</bean>
<bean id="client" class="client.ClientImpl" scope="prototype">
<property name="name" value="--client--" />
<property name="accounts">
<set>
</set>
</property>
</bean>
的事情是,我不喜欢的java代码(1)和(2)的注释行。我会从(2)开始,我认为这很简单:有没有一种方法可以在.xml文件中连接bean,告诉spring实例化ClientImpl中设置的“帐户”的集合实现?所以我可以摆脱(2)
现在转到(1):如果实施更改会发生什么?我真的需要为另一个实现编写另一个DAO吗?或者我必须构建一个BeanFactory?还是有另一个更美丽的解决方案?
谢谢!
谢谢。我不知道为什么我认为在DAO中有一个工厂是不好的。现在看起来没问题。我一定很累。 – Blitzkr1eg 2010-09-02 19:26:38