Spring 基于注解的 IOC 配置
创建 spring 的 的 xml 配置 文件
<context:component-scan base-package="com.itheim"/> 指定创建容器时要扫描的包
<?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"
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.xsd">
<context:component-scan base-package="com.itheim"/>
</beans>
常用注解
用于创建对象的
@Component
相当于: <bean id="" class="">
作用:
把资源让 spring 来管理。相当于在 xml 中配置一个 bean。
属性:
value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名。首字母小写
@Controller @Service @Repository
他们三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的。
他们只不过是提供了更加明确的语义化。
@Controller :一般用于表现层的注解。
@Service :一般用于业务层的注解。
@Repository :一般用于持久层的注解。
细节:如果注解中有且只有一个属性 要赋值时是 ,且名称是 value ,value 在赋值是可以不写。
用于注入数据的
相当于: <property name="" ref=""><property name="" value="">
@Autowired
作用:
自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他 bean 类型。当有多个
类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到
就报错。
@Qualifier
作用:
在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和
@Autowire 一起使用;但是给方法参数注入时,可以独立使用。
属性:
value:指定 bean 的 id。
@Resource
作用:
直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
属性:
name:指定 bean 的 id。
@Value
作用:
注入基本数据类型和 String 类型数据的
属性:
value:用于指定值 value值的方式 ${jdbc.url}
用于改变作用范围的:
相当于: <bean id="" class="" scope="">
@Scope
作用:
指定 bean 的作用范围。
属性:
value:指定范围的值。
取值:singleton prototype request session globalsession
和生命周期相关的:( 了解)
相当于: <bean id="" class="" init-method="" destroy-method="" />
@PostConstruct
用于指定初始化方法
@PreDestroy
用于指定销毁方法
关于 Spring 注解和 XML 的选择问题
注解的优势:
配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。
XML 的优势:
修改时,不用改源码。不涉及重新编译和部署。
Spring 管理 Bean 方式的比较:

案例
通过注解实现从数据库查询信息
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.itheim.service.Imp.accountServiceImp">
<property name="dao" ref="dao"/>
</bean>
<bean id="dao" class="com.itheim.dao.Imp.accountDaoImp">
<property name="runner" ref="runner"/>
</bean>
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<constructor-arg name="ds" ref="ds"/>
</bean>
<!-- 配置数据源-->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
使用Service注解配置管理资源
/**
* 账户的业务层实现类
* @author
* @Company
* @Version 1.0
*/
@Service("accountService")
public class accountServiceImp implements accountService {
private accountDao dao;
static {
System.out.println("service创建完成");
}
public void setDao(accountDao dao) {
this.dao = dao;
}
public List<Account> findAll() {
return dao.findAll();
}
public void Update(int id) {
dao.Update(id);
}
public void saveUser(Account account) {
dao.saveUser(account);
}
}
/**
* 账户的持久层实现类
* @author
* @Company
* @Version 1.0
*/
public class accountDaoImp implements accountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAll() {
try {
String sql="select * from account";
return runner.query(sql,new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void Update(int id) {
try {
String sql="update account set money=? where id=? ";
runner.update(sql,7000,id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void saveUser(Account account) {
try {
String sql="insert into account(money) values(?)";
runner.update(sql,account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
注意:
1、当我们使用注解注入时,set 方法不用写
编写测试类
@Test
public void springTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
accountService service = (accountService) context.getBean("accountService");
Account account = (Account) context.getBean("account");
List<Account> list = service.findAll();
System.out.println(list);
account.setMoney(6000);
service.saveUser(account);
}
pom.xml下的配置为
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--导入spring依赖jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
Spring 基于注解的 IOC 配置的更多相关文章
- 10 Spring框架--基于注解的IOC配置
1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...
- spring基于注解的IoC以及IoC的案例
1.Spring中IoC的常用注解 1.1明确: (1)基于注解的配置和xml的配置要实现的功能都是一样的,都是要降低程序之间的耦合,只是配置的形式不一样 2.案例:使用xml方式和注解方式实现单表的 ...
- 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置
复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...
- 基于注解的IOC配置
1 明确 注解配置和XML配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. 关于实际的开发中到底是使用XML还是注解,每家公司有着不同的习惯.具体问题具体分析. 2 环境搭建 ...
- spring的基于注解的IOC配置
1.配置文件配置 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http: ...
- spring基于注解的IOC
曾经的XML配置: <bean id="accountService" class="com.itheima.service.impl.AccountService ...
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...
- 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache
本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...
- 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式
8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...
随机推荐
- Flask 上下文机制和线程隔离
1. 计算机科学领域的任何问题都可以通过增加一个间接的中间层来解决, 上下文机制就是这句话的体现. 2. 如果一次封装解决不了问题,那就再来一次 上下文:相当于一个容器,保存了Flask程序运行过程中 ...
- redis(十):Redis 列表(List)
Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素到列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...
- windows python的多进程
最近打比赛,apply操作极慢,队友使用了线程池,用多核开辟多线程跑,加速. 在阿里平台上,都没问题. 我是win10系统+jupyter notebook 多线程那个模块运行,会显示一直运行,p.c ...
- Angular 懒加载找不到模块问题解决方法
问题: 懒加载无法找到模块 解决办法: 在app-routing.module.ts中引入该模块
- vscode切换虚拟环境报错无法加载文件 E:\Python_project\shop_env\Scripts\Activate.ps1,因为在此系统上禁止运行 脚本。
在使用vscode切换python的虚拟环境时报错 解决方法如下: Windows+x打开面板,选择以管理员身份运行PowerShell,输入: set-executionpolicy remotes ...
- 深入理解JVM内存回收机制(不包含垃圾收集器)
目录 垃圾回收发生的区域 如何判断对象是否可以被回收 HotSpot实现 垃圾回收算法 JVM中使用的垃圾收集算法 GC的分类 总结 参考资料 垃圾回收发生的区域 堆是java创建对象的区域(Stri ...
- 计算思维(Computational Thinking)在少儿编程中的体现
本文主要针对少儿编程从业人员及正在学习编程的学生家长 大家好,我是C大叔,国内早期的少儿编程从业人员.一直以来都是在做scratch,JavaScript,python以及信息学奥赛C++的讲师,教研 ...
- javascript : 对象取值练习
let obj = { "qqq":0, "www":0, "eee":0, "rrr":1, "ttt&qu ...
- Python对列表去重的各种方法
一.循环去重 二.用 set() 去重 1.set()对list去重 2.list 是有序的,用 sort() 把顺序改回来 三.利用 dict 的属性来去重 1.用 dict 的 fromke ...
- 一分钟速学 | NMS, IOU 与 SoftMax
非极大抑制 NMS的英文是Non-maximum suppression的缩写. 简单的说,就是模型给出了多个重叠在一起的候选框,我们只需要保留一个就可以了.其他的重叠的候选框就删掉了,效果可见下图: ...