自动装配,注解开发,用Java的方式配置Spring
-
自动装配是Spring满足bean依赖的一种方式!
-
Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种自动装配的方式。
-
在xml中显示的配置
-
在java中显示配置
-
1.1、测试
-
环境搭建
-
一个人有两个宠物!
-
1.2、ByName自动装配
<!-- byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid! --> <bean id="people" class="com.kuang.pojo.People" autowire="byName"> <property name="name" value="小狂神呀"/> </bean>
1.3、ByType自动装配
<bean class="com.kuang.pojo.Cat"/> <bean class="com.kuang.pojo.Dog"/> <!-- byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid! byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean! --> <bean id="people" class="com.kuang.pojo.People" autowire="byType"> <property name="name" value="小狂神呀"/> </bean>
小结:
-
byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的set方法的值一致!
-
byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
1.4、使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了!
要使用注解须知:
-
导入约束
-
==配置注解的支持: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
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired 我们可以不用编写Set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname!
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null public @interface Autowired { /** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */ boolean required() default true; }
测试代码
//如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空 @Autowired(required = false) private Cat cat; @Autowired private Dog dog; private String name; 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解完成的时候【@Autowired】完成的时候、我们可以使用@Qualifier(value = "xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入! public class People { //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空 @Autowired @Qualifier(value = "cat111") private Cat cat; @Autowired @Qualifier(value = "dog222") private Dog dog; private String name; } @Resource注解 public class People { @Resource(name = "cat2") private Cat cat; @Resource private Dog dog; private String name; }
小结:
@Resource和@Autowired的区别:
-
都是用来自动装配,都可以放在属性字段上
-
@Autowired 通过byType的方式实现,而且必须要求这个对象存在!【常用】
-
@Resource 通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
-
执行顺序不同:@Autowired通过byType的方式实现。
2、使用注解开发
在Spring4之后,必须要保证aop的包导入了
使用注解需要导入context约束,增加注解的支持
<?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>
-
bean
-
属性如何注入!
@Component public class User { public String name; //相当于 <property name="name" value="kuangshen"/> @Value("kuangshen") public void setName(String name) { this.name = name; } }
-
衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
dao --【@Repository】
service --【@Service】
controller --【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring容器中,装配到Bean
-
自动装配置
-@Autowired:自动装配通过类型,名字。
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value = "xxx")
-@Nullable:字段标记了这个注解,说明这个字段可以为null。
-@Resource:自动装配通过名字,类型。
-
作用域
//等价于 <bean id="user" class="com.kuang.pojo.User"/> // @Component 组件 @Component @Scope("prototype") public class User { public String name; //相当于 <property name="name" value="kuangshen"/> @Value("kuangshen") public void setName(String name) { this.name = name; } }
-
小结
xml与注解:
-
xml更加万能,适用于任何场合!维护简单方便
-
注解 不是自己的类使用不了,维护相对复杂!
xml与注解最佳实践:
-
xml用来管理bean;
-
注解只负责完成属性的注入!
-
我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!-- 指定要扫描的包,这个包下的注解就会生效 --> <context:component-scan base-package="com.kuang"/> <context:annotation-config/>
3、使用Java的方式配置Spring
我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能。
实体类
//这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中 @Component public class User { private String name; public String getName() { return name; } @Value("qingjiang") //属性注入值 public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }
配置文件
//这个也会被Spring容器托管,注册到容器中,因为它本来就是一个@Component, // @Configuration代表是一个配置类,就和我们之前看到的beans.xml一样的 @Configuration @ComponentScan("com.kuang.pojo") @Import(KuangConfig2.class) public class KuangConfig { //注册一个Bean就相当于我们之前写的一个bean标签, //这个方法的名字,就相当于bean标签中的id属性 //这个方法的返回值,就相当于bean标签中的class属性 @Bean public User user(){ return new User(); //就是返回要注入到bean的对象! } }
测试类
public class MyTest { public static void main(String[] args) { //如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载! ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.class); User getUser = (User) context.getBean("user"); System.out.println(getUser.getName()); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!