发表于: 2016-12-29 22:49:33

1 2307


今天完成的事情:

今天主要学了 Spring 框架,

明天计划的事情:

1、继续学习 java 框架

2、有可能的话想去郑州线下看一下

遇到的问题:

年底很多人走了,住的地方越来越冷清了,没有学习氛围,无心学习,看来该考虑到线下班去了,小伙伴们一块学才有氛围

收获:

对 Spring 框架有了一个整体认识,看视频还是太慢,找到一篇参考文章好不错挺适合初学者:链接

通过静态工厂创建Bean

<bean id="userDaoImpl2" class = "sample.spring.ioc.UserDaoFactory" factory-method = "getUserDao"/>

通过实例工厂创建Bean

<bean id="factory" class="sample.spring.ioc.UserDaoFactory2"/>

<bean id="userDaoImpl3" factory-bean="factory" factory-method="getUserDao"/>

通过Set方式为对象属性赋值

<bean id="userService" class="sample.spring.ioc.UserServiceBean"> 

  <property name="userID" value="1"/> 

  <property name="userName" value="张三"/> 

  <property name="userDao" ref="userDaoImpl"/> 

  <property name="hobbies"> 

    <list> 

      <value>羽毛球</value> 

      <value>看电影</value> 

      <value>弹吉他</value>

    </list>

  </property>

</bean>

通过构造函数为对象属性赋值

<bean id="userService2" class="sample.spring.ioc.UserServiceBean"> 

  <constructor-arg index="0" value="1"/> 

  <constructor-arg index="1" value="张三"/> 

  <constructor-arg index="2" ref="userDaoImpl"/> 

  <constructor-arg index="3"> 

    <list> 

      <value>羽毛球</value> 

      <value>看电影</value> 

      <value>弹吉他</value>

    </list>

  </constructor-arg>

</bean>

使用Annotation来定位Bean

在Spring中,除了在xml配置文件中定义对象,我们还可以使用Annotation来定位,这位我们提供了很大的方便。

这里我们使用的Annotation主要包括:@Resource/@Autowried/@Qualifier。

第二种类型的Annotation,它把Autowired和Qualifier组合在一起使用,Qualifier来设置bean的名称,Autowired来设置bean找不到时的行为,required为true时会抛出异常,required为false时会返回null

自动加载对象定义

<context:component-scan base-package="sample.spring.ioc"/>




返回列表 返回列表
评论

    分享到