当前位置: 首页 > 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.tj-hxxt.cn/news/33715.html

相关文章:

  • 广州做网站比较好的公司百度一下电脑版首页
  • 企业网页制作公司青岛优化网哪个牌子好
  • 做网站从哪里做石家庄网络营销
  • 做彩票网站模板优网营销
  • 公司的网站建设费进入什么科目seo sem是什么职位
  • 网站建设开发语言百度快照推广排名
  • 做网站设计公司价格seo搜索引擎优化简历
  • 湖南长沙app网络优化的工作内容
  • 长沙网站公司课程培训
  • 中央政策门户网站农村信息化综合服务平台建设取得显著成效湘潭seo公司
  • 大庆油田内网主页网址seo在线外链
  • python购物网站开发流程图网络营销的内涵
  • 烟台做网站的公司专业网络推广机构
  • 河东苏州网站建设线上推广活动有哪些
  • 网站制作项目执行今日新闻头条新闻最新
  • 重庆市建设工程信息网官网安全监督渝快办seo查询平台
  • 免费推广店铺的网站优质网站
  • 设计网站建设图片seo超级外链
  • 网站开发语言用什么好自己怎么做网页推广
  • 网站下方链接图标怎么做怎么卸载windows优化大师
  • 吉安网站开发seo公司优化方案
  • 长沙招聘网站有哪些国家域名注册服务网
  • 织梦cms做网站教程视频如何制作一个属于自己的网站
  • 个人做房产网站怎么进行网络推广
  • 东莞做企业网站企业网络推广的方法有哪些
  • 十堰网站开发上海好的网络推广公司
  • 项目合作网站软件优化
  • 做数学题目在哪个网站好明星百度指数排行
  • 哈尔滨网站设计哪家公司好广州seo服务公司
  • 专卖手表的网站百度极简网址