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" class="org.spring.dao.imp.PersonDaoImp"/>  
		<bean id="personService" class="org.spring.imp.PersonServiceImp" > 
			<property name="personDao" ref="personDao"/><!--前一个personDao为属性,后一个对应bean配置的id-->
 		</bean>


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

<bean id="personService" class="org.spring.imp.PersonServiceImp" > 
			<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" class="org.spring.imp.PersonServiceImp" >
<constructor-arg index="0" type="org.spring.dao.PersonDao" ref="personDao"></constructor-arg>
			<constructor-arg index="1" value="老大"></constructor-arg>
</bean>


8

你可能感兴趣的:(java,DAO,spring,bean,xml)