自动扫描
程序自动注入属性,得到对象,适用基本类型
-
配置文件
<?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.xxx"/> <context:annotation-config></context:annotation-config> </beans> -
注入对象前面添加注释@Component/Controller/Service/Repository
-
注入对象的属性前面添加@Value(“舒畅”)
-
其他什么都不用写。包括set方法,bean标签
思考:重点掌握xml那套,后面的自动装配,自动扫描,有个印象,后面用再说
本文介绍了如何使用Spring框架进行自动扫描和自动装配。通过配置XML文件中的`<context:component-scan>`和`<context:annotation-config>`,可以实现注解驱动的对象创建和属性注入,无需编写setter方法或bean标签。只需在注入对象前添加如`@Component`、`@Service`等注解,以及在属性前使用`@Value`注解即可完成配置。
3090

被折叠的 条评论
为什么被折叠?



