Spring mvc Spring3-@自动连线
这是我的applicationContext.xmlSpring mvc Spring3-@自动连线,spring-mvc,Spring Mvc,这是我的applicationContext.xml <bean id="JdbcUserDao" class="controller.User.JdbcUserDao"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana
<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;
}
}
@存储库
公共类JdbcUserDao实现IUserDao{
私有jdbc模板jt;
@自动连线
私有数据源;
公共数据源getDataSource(){
返回数据源;
}
public void setDataSource(数据源数据源){
this.dataSource=数据源;
jt=新的JdbcTemplate(this.dataSource);
}
公共JdbcTemplate getJt(){
返回jt;
}
公共void setJt(jdbc模板jt){
this.jt=jt;
}
@凌驾
公共列表getUsers(最终字符串用户名、最终字符串密码){
List users=this.jt.query(“从用户中选择用户名、密码”,
新的行映射器(){
@凌驾
公共用户mapRow(ResultSet rs,int i)抛出SQLException{
用户=新用户();
user.setUsername(rs.getString(“用户名”);
user.setPassword(rs.getString(“password”);
返回用户;
}
});
返回用户;
}
}
问题:
- this.dataSource在通过@Autowired设置数据源时可用,就像xml中的configs一样
- 当我在getUsers中使用dataSource时,它会变成null吗
我是spring3新手,所以我真的需要您的帮助。为了使用自动连线,您需要在xml文件配置中添加以下内容
<context:annotation-config />
如果没有帮助,请添加
<context:component-scan base-package="org.springframework.jdbc.datasource" />
要使用自动连线,您需要将以下内容添加到xml文件配置中
<context:annotation-config />
如果没有帮助,请添加
<context:component-scan base-package="org.springframework.jdbc.datasource" />
尝试将AutowiredPostProcessor添加到配置中
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
尝试将AutowiredPostProcessor添加到配置中
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
您可以尝试将autowire添加到set方法而不是属性。您可以尝试将autowire添加到set方法而不是属性。您需要导入在存储库类文件中不使用访问修饰符进行自动连接的类
com.<your project>.controller.User.JdbcUserDao
您需要导入正在执行自动连线的类,而不在存储库类文件中使用访问修饰符
com.<your project>.controller.User.JdbcUserDao
你太棒了!在过去的4天里,我一直在努力解决我的类似问题,我几乎在1000个谷歌搜索结果中寻找答案。非常感谢你摇滚!在过去的4天里,我一直在努力解决我的类似问题,我几乎在1000个谷歌搜索结果中寻找答案。非常感谢你