疑问:@Autowired的作用?[待解答]
有下面一个Spring的工程,工程结构如下:

代码如下:
applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd"> <!-- 配置自动扫描 -->
<context:component-scan base-package="com.tt.spring"></context:component-scan> <!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置C3P0数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!-- 配置Spring的JdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置 NamedParameterJdbcTemplate,该对象可以使用具名参数,其没有无参数的构造器,所以必须为其构造器指定参数-->
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean> </beans>
db.properties:
jdbc.user=root
jdbc.password=1234
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring jdbc.initPoolSize=5
jdbc.maxPoolSize=10
DepartmentDao.java:

EmployeeDao.java:

JdbcTest.java:

疑问: 1.为什么在EmployeeDao类里面对JdbcTemplate进行@Autowired,就可以使用jdbcTemplate对象了?而JdbcTest.java类里必须通过ctx获取bean的方式获取到
jdbcTemplate对象?如果在JdbcTest.java类里直接@Autowired对jdbcTemplate进行自动装配,运行会报错:空指针异常,为什么会这样?
2.为什么在EmployeeDao里面对jdbcTemplate进行@Autowired自动转配,就可以调用jdbcTemplate呢?
3.@Autowired到底什么作用?
疑问:@Autowired的作用?[待解答]的更多相关文章
- JSP里的<c:if>不起作用[待解答]
JSP页面的部分代码如下: 下面的title作为请求参数,shoppingCart作为session范围域的属性. 问题1: 如果去掉<c:if>的判断条件,第一行打印:可以正常显示出来, ...
- @Autowired的作用,自动装配,省去写get/set方法
这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="userDao" clas ...
- @RequestMapping与@Autowired的作用
@RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. @Autowired @ ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- @Autowired @Resource用法
@Autowired的用法和作用 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="u ...
- 【转】Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 注解:@Autowired
Spring的bean对象自动装配注解,@Autowired有三种使用方式,下面分别介绍! 1. 作用于类成员变量上,即在定义类的成员变量的时候,至于其上方. public class Mobile{ ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- 使用Spring的JavaConfig 和 @Autowired注解与自动装配
1 JavaConfig 配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...
随机推荐
- 基础知识总结之 jdk部分
第一次安装jdk 按照操作走完 会出现 C:\Program Files\Java\jdk1.8.0_91 和 C:\Program Files\Java\jre1.8.0_91 两个目录 (平级目 ...
- css 中 transition 需要注意的问题
cubic-bezier 是 transition-timing-function 的值的一种. 四个参数的关系式如下(t 代表时间,取值范围 [0, 1]):P0(1-t)3 + 3P1t(1-t) ...
- ::before ::after CSS3中的伪类和伪元素
::before和::after伪元素的用法 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:fo ...
- igraph Tutorial
igraph Tutorial¶ 参考http://www.cs.rhul.ac.uk/home/tamas/development/igraph/tutorial/tutorial.html ...
- 20145314郑凯杰 《Java程序设计》课程总结
20145314郑凯杰 <Java程序设计>课程总结 每周读书笔记链接汇总 ①寒假预习--"helloworld" ②第一周读书笔记 ③第二周读书笔记 ④第三周读书笔记 ...
- STC51六中中断配置点亮一个LED
一.外部中断0.1(分别點亮一個LED) /****************************************************************************** ...
- 树梅派配置ad-hoc网络
树梅派配置ad-hoc网络 更新与安装 1.更改源/etc/apt/source.list: http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian ...
- centos下利用mail命令进行邮件发送
centos下默认自带mail命令: 可以用如下命令查看存放位置: which mail 结果如下: 如果没有安装可以使用 如下命令安装 yum -y install mailx 利用mail命令进行 ...
- 从0开始 数据结构 字典树 hdu1251
字典树 知识补充 '\0'和'\n'的区别 '\0' 是一个字符串的结尾 '\n' 是换行符 gets 和 scanf 的区别 gets()函数总结: gets() 从标准输入设备读取字符串,以回车结 ...
- 在.Net中进行SQL Server数据库备份与还原操作实用类
#region 类说明 //----------------------------------------------------------------------------- // // 项目 ...