Spring框架中基于注解的Bean管理和属性注入详解
创作时间:
作者:
@小白创作中心
Spring框架中基于注解的Bean管理和属性注入详解
引用
CSDN
1.
https://blog.csdn.net/m0_74977981/article/details/144626708
引入
什么是注解
- 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...)
- 使用注解,注解作用在类上面,方法上面,属性上边
- 使用注解的目的:简化XML配置
Spring针对Bean管理中创建对象提供的注解
- @Component:普通的类
- @Controller:表现层
- @Service:业务层
- @Repository:持久层
上边四个功能一样,都可以用来创建bean实例
用注解的方式创建对象
①:编写接口和实现类
package com.qcby.testanno;
public interface UserService {
public void hello();
}
②:在需要管理的类上添加@Component注解(上边四个都可以)
package com.qcby.testanno;
import org.springframework.stereotype.Component;
/* <bean id="us" class="UserServiceImpl"/> */
/**
* 组件,作用:把当前类使用IOC容器进行管理,如果没有指定名称,默认使用类名,首字母是小写。
* userServiceImpl。或者自己指定名称
**/
@Controller(value="us")
public class UserServiceImpl implements UserService {
public void hello() {
System.out.println("使用注解,方便吧!");
}
}
③:编写配置文件,重点是开启注解扫描
<?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">
<!--开启注解扫描 com.qcby所有的包中的所有的类-->
<context:component-scan base-package="com.qcby"/>
</beans>
④编写测试方法
package com.qcby.test;
import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
@Test
public void run1(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContextanno.xml");
UserService us = (UserService) ac.getBean("us");
us.hello();
}
}
用注解的方实现属性注入
- @Value:用于注入普通类型(String,int,double等类型)--比较麻烦,某些情况下不如直接赋值。
- @Autowired:默认按类型进行自动装配(引用类型)---掌握这个
- @Qualifier:不能单独使用必须和@Autowired一起使用,强制使用名称注入---不如直接使用Autowired
- @Resource:Java提供的注解,也被支持。使用name属性,按名称注入
具体的代码如下:
// 默认当前类名就是ID名称,首字母小写
@Component(value = "c")
// @Controller
// @Service(value = "c")
// @Repository(valu = "c")
public class Car {
// 注解注入值,属性set方法是可以省略不写的。
// 只有一个属性,属性的名称是value,value是可以省略不写的
@Value("大奔2")
private String cname;
@Value(value = "400000")
private Double money;
// 也不用提供set方法
// 按类型自动装配的注解,和id名称没有关系
@Autowired //一定要学会(用于注入对象)
// 按id的名称注入,Qualifier不能单独使用,需要Autowired一起使用。
// @Qualifier(value = "person")
// @Resource Java提供的注解,按名称注入对象,属性名称是name
// @Resource(name = "person")
private Person person;
@Override
public String toString() {
return "Car{" +
"cname='" + cname + '\'' +
", money=" + money +
", person=" + person +
'}';
}
}
@Controller
//@Component(value = "person")
public class Person {
@Value("张三")
private String pname;
@Override
public String toString() {
return "Person{" +
"pname='" + pname + '\'' +
'}';
}
}
@Test
public void run1(){
// 工厂
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获取对象
Car car = (Car) ac.getBean("c");
System.out.println(car);
}
纯注解的形式实现IOC
纯注解的方式是微服务架构开发的主要方式,所以也是非常的重要。纯注解的目的是替换掉所有的配置文件。但是需要编写配置类。
常用的注解总结
- @Configuration:声明是配置类
- @ComponentScan:扫描具体包结构的
编写实体类
@Component
public class Order {
@Value("北京")
private String address;
@Override
public String toString() {
return "Order{" +
"address='" + address + '\'' +
'}';
}
}
编写配置类,替换掉applicationContext.xml配置文件
@Configuration
// 扫描指定的包结构
@ComponentScan(value = "com.qcby")
public class SpringConfig {
}
测试方法的编写
package com.qcby.test;
import com.qcby.demo4.Order;
import com.qcby.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Demo4 {
@Test
public void run(){
// 创建工厂,加载配置类
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
// 获取到对象
Order order = (Order) ac.getBean("order");
System.out.println(order);
}
}
热门推荐
为什么狗的服从性更高,但是周围养猫的人越来越多?
合伙公司利润分配:原则、方式与注意事项
高乌甲素治疗胆绞痛:5种有效缓解方案
入职先让员工签离职申请,公司就能随时解雇不用赔钱?结果……
新中式上衣怎么穿才能显得温柔又高级
2024年退休新规实施,灵活就业按60%和100%缴费,退休领多少钱?
推动维语数字化:小程序助力文化传播与语言学习
如何降低多巴胺耐受性,让大脑重新感受简单的快乐?
美国注册合资公司的商定流程详解及其注意事项
糖尿病患者可以吃杨桃吗?
致敬,那群驰骋蓝天的女飞行员
深度解析:影响睡眠质量的关键因素
化橘红的功效与作用:从历史记载到现代研究
公租房申请流程及所需材料
易经详解第11篇—坤卦讲“履霜坚冰至”,这是见微知著的大智慧
孙犁:荷花淀派创始人的一生
黄花闺女:古代女性的象征与含义
中风后遗症:中医疗法能有效舒缓吗?
同升湖地区的房产特点是什么?这个地区有哪些投资价值?
“AI谣言现状与治理”研讨会聚焦谣言治理难点痛点
感受文化遗产保护的三个"变"
从“北京人”到圆明园,历史文化名城漫游指南来了
我国主要茶具产地及喝茶如何配置茶器
刷牙的5个常见误区你中招了吗?
甲醇期货价格受哪些外部因素影响?这些因素如何作用于市场?
英语听力秘籍:掌握关键技巧,轻松征服听力障碍
小米是一味中药,这么吃,大补!
金融IC卡安全技术大揭秘:密钥管理与安全认证的那些事儿
种植牙具体过程详解:从初次询问到满意安装的全流程指南
希腊破产后遗症!用工荒后推行6天工作制,员工权益如何保障?