使用注解须知:
1:导入约束:导入context的命名空间
2:配置注解的支持:<context:annotation-config/>
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--注解支持-->
<context:annotation-config/> </beans>

一:@Autowired

赋值:通过class类型自动会找容器中bean【利用反射机制,可以不用set】

项目改造:【场景】一个人拥有两只宠物

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解支持-->
<context:annotation-config/>
<!--注册Cat类-->
<bean class="com.spring.vo1.Cat"></bean>
<!--注册Dog类-->
<bean class="com.spring.vo1.Dog"></bean>
<!--注册People类-->
<bean id="people" class="com.spring.vo1.People" ></bean> </beans>

People 注解注入Dog和Cat

package com.spring.vo1;

import org.springframework.beans.factory.annotation.Autowired;

public class People {

    @Autowired/*通过class自动会找容器中bean */
private Dog dog;/*宠物狗*/
@Autowired/*通过class自动会找容器中bean */
private Cat cat;/*宠物猫*/
private String name;/*人的姓名*/ public Dog getDog() {
return dog;
} public Cat getCat() {
return cat;
}
}

二:@Qualifier

配合@Autowired注解使用,如果容器中有两个同类型的bean,可以通过指定beanId进行注解

<!--开启注解支持-->
<context:annotation-config/>
<!--注册Cat类-->
<bean class="com.spring.vo1.Cat"></bean> <!--注册Dog类01-->
<bean id="dog1" class="com.spring.vo1.Dog"></bean> <!--注册Dog类02-->
<bean id="dog2" class="com.spring.vo1.Dog"></bean> <!--注册People类-->
<bean id="people" class="com.spring.vo1.People" ></bean>
package com.spring.vo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; public class People { @Autowired/*通过class自动会找容器中bean */
@Qualifier("dog1")/*通过beanId匹配*/
private Dog dog;
@Autowired/*通过class自动会找容器中bean */
private Cat cat;
private String name;/*人的姓名*/ public Dog getDog() {
return dog;
} public Cat getCat() {
return cat;
}
}

三:@Resource

功能等同于@Autowired,比@Autowired更高级,JAVA提供的注解,既可以通过属性名--beanId(优先)查找,也可以通过对象类型查找;

另外:@Resource(name = "dog1") 等同于 @Autowired + @Qualifier

<!--开启注解支持-->
<context:annotation-config/>
<!--注册Cat类(对象类型)-->
<bean class="com.spring.vo1.Cat"></bean> <!--注册Dog类(beanId)-->
<bean id="dog" class="com.spring.vo1.Dog"></bean> <!--注册People类-->
<bean id="people" class="com.spring.vo1.People" ></bean>
package com.spring.vo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import javax.annotation.Resource; public class People { @Resource
private Dog dog;/*宠物狗*/
@Resource
private Cat cat;/*宠物猫*/
private String name;/*人的姓名*/ public Dog getDog() {
return dog;
} public Cat getCat() {
return cat;
}
}

四:@Component

要先在spring配置文件中配置注解生效的扫描包:

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描包,这个包下面的注解就会生效-->
<context:component-scan base-package="com.spring" /> <!--开启注解支持-->
<context:annotation-config/>
</beans>

@@Component相当于实例化这个对象放到Spring容器中,相当于在spring配置文件中定义了一个bean;bean的id就是小写的类名

定义一个 UserInfo对象:

package com.spring.Vo;

import org.springframework.stereotype.Component;

@Component/*实例化这个对象*/
public class UserInfo { private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

测试:

@Test
public void test01(){
//获取容器
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
UserInfo userInfo =applicationContext.getBean("userInfo", UserInfo.class);
userInfo.setName("zhangsan");
System.out.println(userInfo.getName());
}

四:@Value

给bean的属性做简单赋值,如果是复杂类型,还是建议用配置文件的方式

package com.spring.Vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component/*实例化这个对象*/
public class UserInfo { @Value("king")/*给name赋值*/
private String name; public String getName() {
return name;
}
}

测试:

@Test
public void test01(){
//获取容器
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
UserInfo userInfo =applicationContext.getBean("userInfo", UserInfo.class);
System.out.println(userInfo.getName());
}

五:@Repository

@Component的衍生注解,功能相同,一般会注解到Dao层。

六:@Service

@Component的衍生注解,功能相同,一般会注解到Service层。

七:@Controller

@Component的衍生注解,功能相同,一般会注解到Controller层。

八:@Scope

作用域注解:单例【singleton】、原型【prototype】

package com.spring.Vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; @Component/*实例化这个对象*/
@Scope("singleton")//作用域单例
public class UserInfo { @Value("king")/*给name赋值*/
private String name; public String getName() {
return name;
}
}

Spring5:IOC注解的更多相关文章

  1. Spring的IOC注解开发入门1

    基本知识点如下: 引入注解约束,配置组件扫描 类上的注解: @Conponent  @Controller @Service @Repository 普通属性的注解   @value 对象属性的注解  ...

  2. 自己来实现一套IOC注解框架

    我们自己来实现一套IOC注解框架吧,采用的方式反射加注解和Xutils类似,但我们尽量不写那么麻烦,也不打算采用动态代理,我们扩展一个检测网络的注解,比如没网的时候我们不去执行方法而是给予没有网络的提 ...

  3. Spring入门(二)— IOC注解、Spring测试、AOP入门

    一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...

  4. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  5. Spring顾问、IOC注解和注解增强

    一.顾问 通知的一种表现方式(顾问包装通知/增强) Advisor: 名称匹配方法: NameMecthMethodPointcutAdvisor 1.定义了一个业务类 package cn.spri ...

  6. 【归纳】springboot中的IOC注解:注册bean和使用bean

    目前了解的springboot中IOC注解主要分为两类: 1. 注册bean:@Component和@Repository.@Service.@Controller .@Configuration 共 ...

  7. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

  8. Spring的IOC注解开发入门2

    注解方式设置属性的值 在我们IOC基于xml属性注入的方式中有(一般推荐set方法) 构造方法注入普通值:<constructor-arg>的使用 set方法注入普通值:<prope ...

  9. Android IOC注解库EasyUI

    EasyUI介绍 1.使用反射机制和注解实现类似于butterknife的IOC框架 2.快速的findViewById和OnClick 3.扩展了click时无网络监测 4.扩展了快速点击监测 使用 ...

随机推荐

  1. 使用一行Python代码从图像读取文本

    处理图像不是一项简单的任务.对你来说,作为一个人,很容易看着某样东西然后马上知道你在看什么.但电脑不是这样工作的. 对你来说太难的任务,比如复杂的算术,或者一般意义上的数学,是计算机毫不费力就能完成的 ...

  2. 开发一个基础的npm包

    初始化项目 # 新建文件夹 mkdir whosmeya-npm-package-test # 进入 cd whosmeya-npm-package-test/ # 初始化 package.json, ...

  3. arcgis连接oracle发布服务,提示数据未注册到服务器,手动注册服务器失败

    arcgis连接oracle数据库发布服务时候,分析之后提示:数据未注册到服务器上. 手动注册之后提示:数据客户端没有正确配置.实际上数据库客户端已经安装完成也可以使用. 设置 PATH 环境变量(仅 ...

  4. NEKO's Maze Game - Codeforces 题解

    题目 NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms o ...

  5. 《JAVA与模式》之责任链模式 【转载】

    转载自java_my_life的博客 原文地址:http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html 在阎宏博士的&l ...

  6. MATLAB——nctoolbox安装及使用

    1.nctoolbox安装 nctoolbox是一个Matlab工具箱,它提供对通用数据模型数据集的只读访问. (1)下载nctoolbox安装包. 地址:https://code.google.co ...

  7. 200行PYTHON代码实现贪吃蛇

    200行Python代码实现贪吃蛇 话不多说,最后会给出全部的代码,也可以从这里Fork,正文开始: 目前实现的功能列表: 贪吃蛇的控制,通过上下左右方向键: 触碰到边缘.墙壁.自身则游戏结束: 接触 ...

  8. CAP定理和BASE理论

    CAP定理和BASE理论 标签(空格分隔): 操作系统 CAP定理 CAP定理: 一个分布式系统最多只能满足一致性 (Consistency), 可用性(Availability)和分区容错性(Par ...

  9. docker下搭建nginx

    一.拉取nginx镜像 # docker pull nginx 等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像. 二.运行容器 以下命令使用 NGINX ...

  10. C++头文件应该干的事情

    C++头文件应该干的事情 最近在写自己项目的时候,头文件老是编译错误,后来发现还是对头文件掌握不牢. 头文件应该干什么? 所谓的头文件,其实它的内容跟 .cpp 文件中的内容是一样的,都是 C++ 的 ...