Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Architecture & Framework » Spring Framework  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教为何每次dataSource都取到null?
frankiehuang





发贴: 2
积分: 0
于 2005-08-29 23:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在 myapp-servlet.xml 已经设置好了:
=================================================
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
      <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bip</value>
    </property>
    <property name="username">
      <value>sa</value>
    </property>
    <property name="password">
      <value></value>
    </property>
  </bean>

<bean id="userDAO" class="com.unknownsoft.user.model.UserDAO">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
=================================================

代码:
===================================
public class UserDAO implements IUserDAO{
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public void insertUser(User user) {
  logger.info("dataSource="+ this.dataSource);
SqlUpdate su = new SqlUpdate(this.dataSource,
    "INSERT INTO USERS(username,password,email) VALUES(?,?,?)");
su.declareParameter(new SqlParameter("username", Types.VARCHAR));
su.declareParameter(new SqlParameter("password", Types.VARCHAR));
su.declareParameter(new SqlParameter("email", Types.VARCHAR));
su.compile();
Object[] oa = new Object[3];
oa[0] = user.getUsername();
oa[1] = user.getPassword();
oa[2] = user.getEmail();
int count = su.update(oa);
logger.info("Rows affected: " + count);
}

}
===================================

但是,当我的程序运行到“logger.info("dataSource="+ this.dataSource);”,就发现dataSource=null,为何?

难道Spring不会自动帮我把dataSource初始化?



作者 Re:请教为何每次dataSource都取到null? [Re:frankiehuang]
ww1ww1





发贴: 202
积分: 5
于 2005-08-30 00:27 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
建议:发贴,写代码的时候前后加个code标签就好看多了。

1.起码必须让spring知道你读哪个设置文件?

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("./xxx/xxx.xml"));


2.spring的xml设置文件基本格式:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean/>
</beans>

以上都是必需的。

3.加载 spring.jar和command.jar工具包

4.另外spring也支持byName的auto设置方法,仅在小程序中使用,需要set it explicitly。



作者 Re:请教为何每次dataSource都取到null? [Re:frankiehuang]
frankiehuang





发贴: 2
积分: 0
于 2005-08-30 10:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
非常感谢楼上的热情答复。
可是,我还是有疑问,
1. 我看一些文档说:Spring默认就会读取“应用名称-servlet.xml“的配置文件,否则要在具体代码中中指定就比较不灵活了。

2. Spring的xml文件格式我是设置正确的,只是主贴省略了而已。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923