如何建设网站和app,重庆网站建设企业,网络设计与集成,沈阳男科医院好吗标题注入依赖注入的方式通过Set方法注入通过构造方法注入自动注入依赖注入的数据类型注入Bean对象注入基本数据类型和字符串注入List注入Set注入Map注入Properties注解实现IOCComponentRepository、Service、Controller注入
依赖注入的方式
在使用依赖注入时#xff0c;如果…
标题注入依赖注入的方式通过Set方法注入通过构造方法注入自动注入依赖注入的数据类型注入Bean对象注入基本数据类型和字符串注入List注入Set注入Map注入Properties注解实现IOCComponentRepository、Service、Controller注入
依赖注入的方式
在使用依赖注入时如果注入的是 Bean 对象那么要求注入的 Bean 对象与被注入的 Bean 对象都需要 Spring
通过Set方法注入
需要为注入的成员变量提供 Set 方法。
配置文件
bean idusersDao classcom.bjsxt.dao.impl.UsersDaoImpl/
bean idusersDaoMybatis classcom.bjsxt.dao.impl.UsersDaoImpl/
bean idusersService namename1,name2,name3 classcom.bjsxt.service.impl.UsersServiceImpl!--name要和UsersServiceImpl中属性名相同--property nameusersDaoref beanusersDaoMybatis//property
!--property nameusersDao refusersDaoMybatis/--
/beanBean对象
private UsersDao usersDao;
public UsersDao getUsersDao() {return usersDao;
}
public void setUsersDao(UsersDao usersDao) {this.usersDao usersDao;
}通过构造方法注入
Bean 对象中需要提供有参的构造方法
配置文件
bean idusersDaoMybatis classcom.bjsxt.dao.impl.UsersDaoMybatisImpl/
bean idusersService namename1, name2, name3 classcom.bjsxt.service.impl.UsersServiceImpl!--property nameusersDao refusersDaoMybatis/--!--一个constructor-arg标签表示构造方法中的一个参数name:根据参数名称识别参数index:根据参数的位置识别参数type:根据参数类型识别参数--constructor-arg typecom.bjsxt.dao.UsersDaoref beanusersDaoMybatis//constructor-arg
/bean2.Bean对象
private UsersDao usersDao;
public UsersServiceImpl(UsersDao usersDao){this.usersDao usersDao;
}自动注入
自动注入的方式有两种一种是全局配置自动注入另一种是局部配置自动注入。 无论全局配置或局部单独配置都有 5 个值可以选择
no当 autowire 设置为 no 的时候Spring 就不会进行自动注入。byName在 Spring 容器中查找 id 与属性名相同的 bean并进行注入。需要提供 set 方 法。byType在 Spring 容器中查找类型与属性名的类型相同的 bean并进行注入。需要提 供 set 方法。constructor仍旧是使用 byName 方式只不过注入的时候使用构造方式进行注入。default全局配置的 default 相当于 no局部的 default 表示使用全局配置设置
局部自动注入通过 bean 标签中的 autowier 属性配置自动注入有效范围仅针对当前 bean 标签生效 全局自动注入通过 beans 标签中的 default-autowire 有效范围配置文件中的所有 bean
依赖注入的数据类型
注入Bean对象
方式一
property nameFieldNameref beanBeanID/
/property方式二
property nameFieldName refBeanID/注入基本数据类型和字符串
方式一
property nameFieldNamevaluecontent/value
/property
!--content是要传入内容的值--方式二
property nameFieldName valueContent/注入List
property nameuserslistbean classcom.bjsxt.pojo.Usersproperty nameusername valueOldlu/property nameuserage value30//beanbean classcom.bjsxt.pojo.Usersproperty nameusername valueadmin/property nameuserage value20//bean/list/property注入Set
property nameusersSetsetbean classcom.bjsxt.pojo.Usersproperty nameusername valueOldlu-set/property nameuserage value30//beanbean classcom.bjsxt.pojo.Usersproperty nameusername valueadmin-set/property nameuserage value20//bean/set/property注入Map
方式一 property namemapmapentry keykey1 valuevalue1/entry keykey2 valuevalue2//map/property方式二
bean idusers1 class......
bean idusers1 class......
property nameFieldNamemapentry keykey1 value-refusers1/entry keykey2 value-refusers2//map
/property注入Properties
property nameFieldName propsprop keyKeyNameContent/prop/props
/property注解实现IOC
E:\java\Spring\spring_ioc2Component
作用用于创建对象放入Spring容器相当于 bean id class 注意
要在配置文件中配置扫描的包扫描到该注解才能生效。 context:component-scan base-packagecom.itbaizhan /context:component-scan Component 注解配置bean的默认id是首字母小写的类名。也 可以手动设置bean的id值。
/ 此时bean的id为studentDaoImpl
Component
public class StudentDaoImpl implements
StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,百战程序
员,北京);}}// 此时bean的id为studentDao
Component(studentDao)
public class StudentDaoImpl implements
StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,百战程序
员,北京);}
}Repository、Service、Controller
作用这三个注解和Component的作用一样使用它们是为了区分该类属于什么层。
Repository用于Dao层Service用于Service层Controller用于Controller层
Scope指定bean的创建策略:singleton prototype request session globalsession