spring概括
使用spring的好处:
1、降低组件之间的耦合度,实现软件各层之间的解耦
2、可以使用容器(Controller)事物处理 开发人员无需手工编写事物处理
3、容器提供单例模式支持,开发人员无需编写实现代码。
4、容器提供AOP技术
5、容器提供很多辅助类
6、spring对主流框架提供了集成支持 如:Hibernate Struts2。
轻量级框架与重量级框架概念划分:
1、spring即是轻量级的又可以是重量级的,取决与你应用的资源的多少。
实例化spring容器的方法
1、方法一:在类路径下寻找配置文件来实例化容器
ApplicationContext context=new ClassPathXmlApplicationContext("springDemo1/bean.xml");
ApplicationContext context=new ClassPathXmlApplicationContext(
new String[]("springDemo1/bean.xml","springDemo1/bean2.xml"));
2、方法二:在文件路径下寻找配置文件来实例化容器
ApplicationContext context1=new FileSystemXmlApplicationContext(new String[]
{"/aa/bean1.xml","/bb/bean2.xml"});
3、方法三:通过Resurse装载,然后获得BeanFactory
4、方法四:通过带@Configuration的配置类
三种实例化Bean的方式
1、使用类构造器的方法(调用无参构造方法)
<bean id="personDao" class="springDemo2.PersonDao"></bean>
2、使用静态工厂方法实例化
<bean id="personDao" class="springDemo2.PersonDaoFactory" factory-
method=“createPersonDao”></bean>
public class PersonDaoFactory{
public static PersonDao createPersonDao(){
return new PersonDao();
}
}
3、使用实例工厂方法实例化
<bean id="personDaoFactory1" class= "springDemo.PersonDaoFactory1"/>
<bean id="personDao" factory-bean="personDaoFactory1" factory-method="createPersonDao">
</bean>
public class PersonDaoFactory1 {
public PersonDao createPersonDao(){
return new PersonDao();
}
}
Bean的初始化方法与销毁方法
1、public void init(){}初始化方法
2、public void destory(){}销毁方法
<bean id="person" class="springDemo2.Person" init-method="init" destroy-
method="destory"></bean>