具体错误如下: Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testDao' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:694) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) at test.Test.main(Test.java:15) 我的代码: 1.Dao层的实现类如下:
1@Repository("testDao") 2public class TestDaoImpl implements TestDao { 3@Autowired 4//使用配置文件中的JDBC模板 5private JdbcTemplate JdbcTemplate; 6 @Override 7 public int update(String sql, Object[] param) { 8 // TODO Auto-generated method stub 9 return JdbcTemplate.update(sql,param); 10 } 11 @Override 12 public List<MyUser> query(String sql, Object[] param) { 13 RowMapper<MyUser> rowMapper=new BeanPropertyRowMapper<MyUser>(MyUser.class); 14 return JdbcTemplate.query(sql, rowMapper, param); 15 } 16}
2.applicationContext.xml文件配置如下:
1 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:task="http://www.springframework.org/schema/task" 8 xmlns:cache="http://www.springframework.org/schema/cache" 9 xmlns:p="http://www.springframework.org/schema/p" 10 xsi:schemaLocation="http://www.springframework.org/schema/jdbc 11 http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 12 13 http://www.springframework.org/schema/task 14 http://www.springframework.org/schema/task/spring-task-4.1.xsd 15 16 http://www.springframework.org/schema/beans 17 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 18 19 http://www.springframework.org/schema/cache 20 http://www.springframework.org/schema/cache/spring-cache-4.1.xsd 21 22 http://www.springframework.org/schema/tx 23 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 24 25 http://www.springframework.org/schema/context 26 http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 27 28<!-- 指定需要扫描的包,使注解生效 --> 29<context:component-scan base-package="com.dao"/> 30 31<!-- 1.配置数据源 --> 32 <bean id="dataSource" 33 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 34 <!-- mysql.数据库驱动 --> 35 <property name="driverClassName" 36 value="com.mysql.jdbc.Driver"></property> 37 <!-- 1.2.连接数据库的url --> 38 <property name="url" 39 value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8"></property> 40 <!-- 1.3.连接数据库的用户名 --> 41 <property name="username" value="root"></property> 42 <!-- 1.4.连接数据库的密码 --> 43 <property name="password" value="123456"></property> 44 </bean> 45 46 <!-- 2配置JDBC模板 --> 47 <bean id="jdbcTemplate" 48 class="org.springframework.jdbc.core.JdbcTemplate"> 49 <!-- 默认必须使用数据源 --> 50 <property name="dataSource" ref="dataSource"></property> 51 </bean> 52 53</beans>
3.测试类代码如下:
1public class Test { 2public static void main(String[] args) { 3 ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml"); 4 TestDao td= (TestDao)appCon.getBean("testDao"); 5 //从容器中获取增强后的目标对象 6 String insertSql = "insert into user values(null,?,?)"; 7 //数组param的值与insertSql中的?一一对应 8 Object param1[]={"张三","男"}; 9 Object param2[]={"李四","女"}; 10 Object param3[]={"王二","男"}; 11 Object param4[]={"麻子","女"}; 12 //添加用户 13 td.update(insertSql, param1); 14 td.update(insertSql, param2); 15 td.update(insertSql, param3); 16 td.update(insertSql, param4); 17 //查询用户 18 String selectSql = "select*from user"; 19 List<MyUser> list = td.query(selectSql, null); 20 for(MyUser mu:list){ 21 System.out.println(mu); 22 } 23 24} 25} 26
新手自学,,,网上搜了好多没解决,希望大神指点
