Spring3 - @Autowired
这是我的 applicationContext.xml
<bean id="JdbcUserDao" class="controller.User.JdbcUserDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.apache.derby.jdbc.ClientDriver"
p:url="jdbc:derby://localhost:1527/TodoDb"
p:username="root"
p:password="root" />
这是我的 implDao 类:
@Repository
public class JdbcUserDao implements IUserDao {
private JdbcTemplate jt;
@Autowired
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
jt = new JdbcTemplate(this.dataSource);
}
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
@Override
public List<User> getUsers(final String username, final String password) {
List<User> users = this.jt.query("SELECT username, password FROM USERS",
new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int i) throws SQLException {
User user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
return user;
}
});
return users;
}
}
问题:
- this.dataSource 在通过 @Autowired 设置 dataSource 时可用,就像 xml 中的配置一样
- ,当我在 getUsers 中使用 dataSource 时,它会变成 null 吗?
问题:
- 我怎样才能得到这个作品?
我是 spring3 的新手,所以我真的需要你的帮助。
This is my applicationContext.xml
<bean id="JdbcUserDao" class="controller.User.JdbcUserDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.apache.derby.jdbc.ClientDriver"
p:url="jdbc:derby://localhost:1527/TodoDb"
p:username="root"
p:password="root" />
This is my implDao class :
@Repository
public class JdbcUserDao implements IUserDao {
private JdbcTemplate jt;
@Autowired
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
jt = new JdbcTemplate(this.dataSource);
}
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
@Override
public List<User> getUsers(final String username, final String password) {
List<User> users = this.jt.query("SELECT username, password FROM USERS",
new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int i) throws SQLException {
User user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
return user;
}
});
return users;
}
}
Problems:
- this.dataSource available when it sets the dataSource through @Autowired like the configs in xml
- when I use dataSource in getUsers, it become null ?
Questions:
- How can I get this works ?
I'm new to spring3 so I really need your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了使用自动装配,您需要将以下内容添加到 xml 文件配置中。
如果没有帮助请添加
In order to use autowiring, you need to add the following to your xml file configuration.
If it doesn't help then please add
尝试将 AutowiredPostProcessor 添加到配置中
Try adding the AutowiredPostProcessor to the config
您可以尝试将自动装配添加到 set 方法而不是属性中。
You could try adding the autowire to the set method instead of the property.
您需要导入正在执行自动装配的类,而无需在存储库类文件
和 spring 注释中使用访问修饰符
you need to import the class which you are doing autowired without access modifiers in repository class file
and spring annotation