spring通过注解来初始化参数

spring通过注解来初始化参数

xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--使用注解-->
   <context:component-scan base-package="com.imooc.dao.impl"></context:component-scan>
   <!--<context:annotation-config></context:annotation-config>-->
   <!--配置数据源-->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql://localhost:3306/selection_course?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT&amp;useSSL=false"></property>
       <property name="username" value="root"></property>
       <property name="password" value="123456"></property>
   </bean>

   <!--配置springTemplate工具类的调用-->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <!--引用数据源-->
       <property name="dataSource" ref="dataSource"></property>
   </bean>

</beans>

StudentDaoImpl类代码:

package com.imooc.dao.impl;

import com.imooc.dao.StudentDao;
import com.imooc.entity.Student;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;

@Component("studentDao")
public class StudentDaoImpl implements StudentDao {

   Logger log = Logger.getLogger("StudentDaoImpl");
   @Resource(name = "jdbcTemplate")
   private JdbcTemplate jdbcTemplate;
   public void insert(Student student) {
       String sql = "insert into student(name,sex,born) values(?,?,?)";
       jdbcTemplate.update(sql,student.getName(),student.getSex(),student.getBron());
   }

   public void update(Student student) {
       String sql = "update student set name=?,sex=?,born=? where id=?";
       jdbcTemplate.update(sql,student.getName(),student.getSex(),student.getBron(),student.getId());
   }

   public void delete(int id) {
       String sql ="delete from student where id = ?;";
       jdbcTemplate.update(sql,id);
   }

   public Student select(int id) {
       String sql = "select * from student where id = ?";
      Student student =  jdbcTemplate.queryForObject(sql, new StudentRowMapper(),1);
       return student;
   }


   public List<Student> selectAll() {
       log.info("查询所有学生信息之前==");
       String sql = "select * from  student";
       List<Student> students = jdbcTemplate.query(sql,new StudentRowMapper());
       log.info("查询所有学生信息后===");
       return students;
   }


   private class StudentRowMapper implements RowMapper<Student> {

       public Student mapRow(ResultSet resultSet, int i) throws SQLException {
           Student stu = new Student();
           stu.setId(resultSet.getInt("id"));
           stu.setName(resultSet.getString("name"));
           stu.setSex(resultSet.getString("sex"));
           stu.setBron(resultSet.getDate("born"));
           return stu;
       }
   }
}

测试代码:

import com.imooc.dao.StudentDao;
import com.imooc.entity.Student;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.List;


public class DaoTest {

   @Autowired
   @Qualifier("studentDao")
   private StudentDao studentDao;

   @Test
   public void selectAll(){
       List<Student> students = studentDao.selectAll();
       System.out.println(students);
   }
}

老师 我测试了一下我的代码 发现报错为空指针异常 ,尝试输出jdbcTemplate 和 studentDao发现为空

请问老师 我这样使用注解方式初始化类有错吗?错在哪呀



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿莹 2019-01-18 19:13:31

我看同学之前加入了这个约束:

http://img1.sycdn.imooc.com//climg/5c41b49b0001a2fa06460080.jpg

同学是又加入了一个其他版本的么?

如果不添加http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

按照老师回复你的方法还是报一样的错吗?

祝学习愉快。

好帮手慕阿莹 2019-01-18 17:24:03

1、同学的jdk使用的是哪个版本的呢?如上是如果同学是jdk10,建议同学把

@Resource(name = "jdbcTemplate")建议改为 

   @Autowired
   @Qualifier("jdbcTemplate") 试试

或者在pom文件中加入下边这个注解:

<dependency> 
<groupId>javax.annotation</groupId> 
<artifactId>javax.annotation-api</artifactId> 
<version>1.2</version> 
</dependency>

 

2、同学的DaoTest没有直接交给spring管理,所以没有办法直接用交给spring管理的对象。同学可以用这种方式引入一下配置文件spring.xml试试:

http://img1.sycdn.imooc.com//climg/5c419b120001164705060155.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕圣2241928 #1
    老师 我在xml中加了http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 才报 错:出现提示“”通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明“”应添加完整的 约束文件 。请问这是 为什么呀
    2019-01-18 17:57:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师