首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring初学札记2

2012-09-02 
Spring初学笔记21spring容器实例化bean默认是:容器启动时实例化bean,并且是单实例的2.bean的scope属性为sc

Spring初学笔记2
1spring容器实例化bean默认是:容器启动时实例化bean,并且是单实例的

2.bean的scope属性为scope=“pototype“时,容器启动时不实例化bean,而是调用getBean方法是实例化的
3.当lazy-init属性为true时,spring容器启动时不会实例化bean。
4.init-method属性指定初始化放放方法,distory-method属性指定bean销毁时执行的方法
5.bean的注入
  set方式
  service类

package org.spring.imp;import org.spring.dao.PersonDao;import org.spring.service.PersonService;public class PersonServiceImp implements PersonService {PersonDao personDao;public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void savePerson() {//System.out.println("执行了PersonSerciceImp.savePerson()方法...");personDao.addPerson();}}

beans.xml配置
 <bean id="personDao" > <property name="personDao" ref="personDao"/><!--前一个personDao为属性,后一个对应bean配置的id--> </bean>


6.集合注入,set(list,property,map雷同)
Set<String> sets = new HashSet<String>();

<bean id="personService" > <property name="sets"><set><value>1</value><value>2</value><value>3</value></set></property> </bean>

7.构造器方式注入值
public PersonServiceImp(PersonDao persondao,String name){this.personDao = persondao;this.name = name;}<bean id="personService" ><constructor-arg index="0" type="org.spring.dao.PersonDao" ref="personDao"></constructor-arg><constructor-arg index="1" value="老大"></constructor-arg></bean>


8

热点排行