admin 管理员组文章数量: 888526
跟杨春娟学Spring笔记:自动装备Bean
跟杨春娟学Spring笔记:自动装配Bean
完成:第一遍
1.如何自动装配Bean?
要使用自动装配,就需要配置< bean >元素的autowire属性
名称:byName
说明:根据 Property 的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。
名称:byType
说明:根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。
名称:constructor
说明:根据构造方法的参数的数据类型,进行 byType 模式的自动装配。
名称:autodetect
说明:如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。近期版本已经不支持该方式了
名称:no
说明:默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。
UserModel
package com.adbycool.ioc;public class UserModel {private String id;private String name;private String sex;public UserModel() {super();}public UserModel(String id, String name, String sex) {this.id = id;this.name = name;this.sex = sex;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "UserModel [id=" + id + ", name=" + name + ", sex=" + sex + "]";}}
UserService
package com.adbycool.ioc;public class UserService {private UserModel userModel;public UserModel getUserModel() {return userModel;}public void setUserModel(UserModel userModel) {this.userModel = userModel;}public void findUser() {System.out.println("----------findUser()构造方法自动注入---------userModel:"+userModel);}public UserService(UserModel userModel) {this.userModel = userModel;}public UserService() {super();}@Overridepublic String toString() {return "UserService [userModel=" + userModel + "]";}}
TestUserService
package com.adbycool.ioc;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;class TestUserService {@Testvoid testFindUser() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = context.getBean("userService",UserService.class);userService.findUser();}}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xmlns:p=""xmlns:c=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans-4.3.xsd"><bean id="userModel" class="com.adbycool.ioc.UserModel"><property name="id" value="Haha123"></property><property name="name" value="Jack"></property><property name="sex" value="man"></property></bean><!-- autowire属性通过byName、byType、constructor自动注入 <bean id="userService" class="com.adbycool.ioc.UserService" autowire="byName"></bean> <bean id="userService" class="com.adbycool.ioc.UserService" autowire="byType"></bean>--><bean id="userService" class="com.adbycool.ioc.UserService" autowire="constructor"></bean></beans>
本文标签: 跟杨春娟学Spring笔记自动装备Bean
版权声明:本文标题:跟杨春娟学Spring笔记:自动装备Bean 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1693584764h230695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论