当前位置: 首页 > news >正文

专业网站建设模板今天最新的新闻头条

专业网站建设模板,今天最新的新闻头条,wordpress 手工网站,微信开发小程序开发工具下载标题注入依赖注入的方式通过Set方法注入通过构造方法注入自动注入依赖注入的数据类型注入Bean对象注入基本数据类型和字符串注入List注入Set注入Map注入Properties注解实现IOCComponentRepository、Service、Controller注入 依赖注入的方式 在使用依赖注入时,如果…

标题

  • 注入
    • 依赖注入的方式
      • 通过Set方法注入
      • 通过构造方法注入
      • 自动注入
    • 依赖注入的数据类型
      • 注入Bean对象
      • 注入基本数据类型和字符串
      • 注入List
      • 注入Set
      • 注入Map
      • 注入Properties
  • 注解实现IOC
    • Component
    • @Repository、@Service、@Controller

注入

依赖注入的方式

在使用依赖注入时,如果注入的是 Bean 对象,那么要求注入的 Bean 对象与被注入的
Bean 对象都需要 Spring

通过Set方法注入

需要为注入的成员变量提供 Set 方法。

  1. 配置文件
<bean id="usersDao" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersService" name="name1,name2,name3" class="com.bjsxt.service.impl.UsersServiceImpl"><!--<name要和UsersServiceImpl中属性名相同--><property name="usersDao"><ref bean="usersDaoMybatis"/></property>
<!--<property name="usersDao" ref="usersDaoMybatis"/>-->
</bean>
  1. Bean对象
private UsersDao usersDao;
public UsersDao getUsersDao() {return usersDao;
}
public void setUsersDao(UsersDao usersDao) {this.usersDao = usersDao;
}

通过构造方法注入

Bean 对象中需要提供有参的构造方法

  1. 配置文件
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoMybatisImpl"/>
<bean id="usersService" name="name1, name2, name3" class="com.bjsxt.service.impl.UsersServiceImpl"><!--<property name="usersDao" ref="usersDaoMybatis"/>--><!--一个constructor-arg标签表示构造方法中的一个参数name:根据参数名称识别参数index:根据参数的位置识别参数type:根据参数类型识别参数--><constructor-arg type="com.bjsxt.dao.UsersDao"><ref bean="usersDaoMybatis"/></constructor-arg>
</bean>

2.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 name="FieldName"><ref bean="BeanID"/>
</property>

方式二:

<property name="FieldName" ref="BeanID"/>

注入基本数据类型和字符串

方式一:

<property name="FieldName"><value>content</value>
</property>
<!--content是要传入内容的值-->

方式二

<property name="FieldName" value="Content"/>

注入List

<property name="users"><list><bean class="com.bjsxt.pojo.Users"><property name="username" value="Oldlu"/><property name="userage" value="30"/></bean><bean class="com.bjsxt.pojo.Users"><property name="username" value="admin"/><property name="userage" value="20"/></bean></list></property>

注入Set

<property name="usersSet"><set><bean class="com.bjsxt.pojo.Users"><property name="username" value="Oldlu-set"/><property name="userage" value="30"/></bean><bean class="com.bjsxt.pojo.Users"><property name="username" value="admin-set"/><property name="userage" value="20"/></bean></set></property>

注入Map

方式一:

 <property name="map"><map><entry key="key1" value="value1"/><entry key="key2" value="value2"/></map></property>

方式二:

<bean id="users1" class="......">
<bean id="users1" class="......">
<property name="FieldName"><map><entry key="key1" value-ref="users1"/><entry key="key2" value-ref="users2"/></map>
</property>

注入Properties

<property name="FieldName" ><props><prop key="KeyName">Content</prop></props>
</property>

注解实现IOC

E:\java\Spring\spring_ioc2

Component

作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
注意:

  1. 要在配置文件中配置扫描的包,扫描到该注解才能生效。
    context:component-scan base-package="com.itbaizhan"> </context:component-scan>
  2. @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

http://www.hotlads.com/news/5097.html

相关文章:

  • 政务公开网站建设整改方案百度竞价排名又叫
  • 汨罗做网站中国十大公关公司排名
  • 百度网址大全最新版北京网站seo设计
  • 洛阳作公司网站广告网络
  • 微商网站如何做推广今日热点新闻事件摘抄2022
  • 北海网站制作公司友情链接交换平台源码
  • 那个网站专门做二手衣服什么软件可以推广自己的产品
  • 真人做爰网站视频外链吧怎么使用
  • discuz 分类网站网址域名大全2345网址
  • 做个简单的导航网站微信营销软件群发
  • 宁波网站建设制作方法网络营销推广策划方案
  • 网站域名找回密码 用户名搜索引擎营销就是seo
  • 云服务器做网站好吗百度指数网站
  • 阿里云做电脑网站网址关键词查询网站
  • 小网站推广个人接外包项目平台
  • 长沙的企业网站建设seo关键字优化技巧
  • 建设网站要求哪里备案属于免费的网络营销方式
  • 杭州软件测试培训机构排名seo优化方案策划书
  • 部门网站管理建设工作汇报专业的网站优化公司
  • 如何做电影网站 去哪里找片源网络安全有名的培训学校
  • ai做图标教程网站seo是什么姓
  • 台州网站怎么推广广州seo网站管理
  • 福建省建设厅网站 保证金打开百度搜索引擎
  • 长沙市天心建设局网站cpa广告联盟平台
  • 设计师可以做兼职的网站有哪些东莞百度推广排名优化
  • 做网站为什么要用固定ip本网站三天换一次域名
  • 南京做网站开发 待遇站长之家域名信息查询
  • 建一个自己用的网站要多少钱seo怎么收费seo
  • 企业做不做网站的坏处富阳网站seo价格
  • 做外贸建网站需要推广吗seo培训师