自动装配
3、Bean的自动装配
- spring自动寻找上下文,并自动装配属性
3.1spring中自动装配的三种方式
- 在xml中显示的配置
- 在java中显示的配置
- 隐示自动装配
3.2测试
3.2.1在xml中显示的配置
- byName 会自动从spring上下文中的去查找,有没有和自己属性相同的class的小写的id
<bean id="dog" class="com.mhy.atowired.Dog"/> <bean id="cat" class="com.mhy.atowired.Cat"/> <!-- 手动装配--> <!-- <bean id="people" class="com.mhy.atowired.People">--> <!-- <property name="name" value="水三丫"/>--> <!-- <property name="cat" ref="cat"/>--> <!-- <property name="dog" ref="dog"/>--> <!-- </bean>--> <bean id="people" class="com.mhy.atowired.People" autowire="byName"> <property name="name" value="水三丫"/> </bean>
注意:如果配置的id的名字不是小写的不一样不能够自动装配
例如如这里的id="dog",如果改成dog222就会装配失败
- byType 会根据上下文配置的class类型去寻找,id名可以不是小写,没有id也可以的
<bean id="dog222" class="com.mhy.atowired.Dog" p:dogName="小狗狗旺财"/> <bean id="cat222" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/> <!-- 手动装配--> <!-- <bean id="people" class="com.mhy.atowired.People">--> <!-- <property name="name" value="水三丫"/>--> <!-- <property name="cat" ref="cat"/>--> <!-- <property name="dog" ref="dog"/>--> <!-- </bean>--> <bean id="people" class="com.mhy.atowired.People" autowire="byType"> <property name="name" value="水三丫"/> </bean>
注意:但是不能够配置多个一样的class类型,否则会寻找失败
例如这样:
<bean id="dog222" class="com.mhy.atowired.Dog"/> <bean id="cat222" class="com.mhy.atowired.Cat"/> <bean id="cat22" class="com.mhy.atowired.Cat"/> <bean id="cat2" class="com.mhy.atowired.Cat"/>
3.2.2使用注解的自动装配
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方法使用!class类的属性上使用!
@Autowired private Cat cat; @Autowired public void setDog(Dog dog) { this.dog = dog; } - @Autowired(required = false) :表示该属性可以为null
- @Qualifier(value="cat222") :这个注解可以显示的去定义名字,当有多个相同的class的bean的id不同配置时候可以避免精确的定位
<bean id="cat222" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/> <bean id="cat22233" class="com.mhy.atowired.Cat" p:catName="小苗苗HelloKitty"/> @Autowired @Qualifier(value="cat222") private Cat cat; - @Resource(name="cat222"):是java自带的注解不是spring的注解,它也可以完成自动装配,它也可以加name属性,可以精确定位到
4、使用注解开发
使用注解开发不是使用xml必须导入spring-aop的jar包
配置环境
<!-- 扫描包--> <context:component-scan base-package="com.mhy.pojo2"/> <!-- 使用注解开发--> <context:annotation-config/>
-
@Component(value = "user"):注解是bean的配置,这里的value相当于id,相当于
- Component有很多衍生注解,功能相同
- @Repository:一般用于Dao层
- @Service:一般用于Service层
- @Controller:一般用Web,servlet
<bean id="user2" class="com.mhy.pojo2.User2"/> -
@Value:用于属性的值,可以在属性上,也可以在set方法上
@Value("shuisanya") public void setName(String name) { this.name = name; }
@Value("shuisanya") private String name;
<property name="name" value="shuisanya"/>
5、完全使用java的方式配置spring
-
@Configuration:配置到自己的定义的类上面,该类就相当于applicationContext.xml的配置文件,相当于一个容器,获取context对象的方法也要变AnnotationConfigApplicationContext
-
@Bean:就你需要注册在spring的实体类对象,加在上面configuration注解的类的中作为方法返回你注册的实体类
-
@ComponentScan("com.mhy.pojo3"):扫描包
-
@Import(SyyConfig2.class):导入另一个@Configuration注解的配置容器
@Configuration @ComponentScan("com.mhy.pojo3")//扫描包 @Import(SyyConfig2.class) public class SsyConfig { @Bean public User3 getUser3(){ return new User3(); }
}
```java @Component public class User3 { private String name; public String getName() { return name; } @Value("shuisanya") public void setName(String name) { this.name = name; } @Override public String toString() { return "User3{" + "name='" + name + '\'' + '}'; } }
@Test public void user3Test(){ ApplicationContext context = new AnnotationConfigApplicationContext(SsyConfig.class); User3 user3 = context.getBean("getUser3", User3.class); System.out.println(user3.getName()); }
注意:如果@Component(value = "user3") 加了value,并且加了@ComponentScan("com.mhy.pojo3")这个扫描包的注解,可以用user3来获取;当然如果在@Configuration注解的类写Bean注解用getUser3获取也是可以的
@Component(value = "user3") public class User3 @Bean public User3 getUser3(){ return new User3(); } User3 user3 = context.getBean("user3", User3.class);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)