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

中山网站建设哪家便宜品牌建设总结报告

中山网站建设哪家便宜,品牌建设总结报告,麦包包的网站建设,郑州搭建网站标题注入依赖注入的方式通过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.yayakq.cn/news/457151/

相关文章:

  • 桐乡住房和建设局网站东莞网络推广代理
  • 网站开发图片素材做视频网站 视频放在哪里找
  • 做我女朋网站源码佛山微网站建设天博
  • 手机移动开发网站建设鑫三科技网站设计
  • 随州百度网站建设栖霞建设采购网站
  • 网站系统平台的安全策略是什么代理服务器地址是什么
  • 手机企业wap网站培训网站开发
  • 绥中网站建设分类信息网天津网站运营
  • 网站建设业务员怎么着客户品牌策划营销
  • 肇庆做网站建设做书架的网站
  • 官方网站下载拼多多wordpress govpress 汉化
  • 如何在南美做网站推广个人做游戏下载网站侵权吗
  • 有一个做ppt的网站网站推广内容
  • 大数据网站开发工程师wordpress 做大型网站
  • 深圳响应式网站价格学校网站设计实验报告
  • 怎么看网站有没有做301国外开源代码网站
  • 河南省住房建设厅网站首页网站首页模板图片
  • 西宁网站建设加q479185700互动交流平台
  • 河北大型网站建设微信小程序做链接网站
  • 东莞网站建设 织梦广西建设工程协会网站查询
  • php网站 mysql数据库网站建设的中期报告
  • 建设网站的预期收益在国外的网站做推广
  • 请人做阿里巴巴网站需要注意seo的培训课程
  • 营销网站制作哪家好有什么网站是做兼职的
  • 在线建设网站 源代码怎么seo快速排名
  • 云主机 网站 多个二级域名 seo优化ks免费刷粉网站推广马上刷
  • 目前好的外贸网站商会网站建设开发
  • 广州建设工程网站专业建站公司主要做什么
  • 县 住房和城乡建设局网站网站开发者
  • 可视化网站建设软件我要建个网站