您的位置:首页 > 编程语言 > Java开发

spring基础——ssh学习笔记

2016-08-31 14:45 447 查看

Spring 基础

1.Spring将容器中的一切对象称为bean

2.spring通过xml(或配置文件)管理bean,利用反射

<beans>
<bean id="" class="必须是带包名的完整类名">
</beans>


3.ApplicationContext是最常用的接口

,有两个具体实现的类:

ClassPathXmlApplicationContext:从类加载路径上搜索配置文件

FileSystemXmlApplicationContext:使用相对路径或绝对路径搜索配置文件

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person person = ctx.getBean("person",Person.class);//或者使用getBean(String id)


依赖注入和控制反转

:不同的名字相同的意思,就是调用者无需关心被调用者的实现,等待spring注入。

4.constructor-args构造函数的参数值,其实就是反射调用含参的构造函数

//两种形式:传入对象,传入值
<constructor-arg ref="id"//该值指向另一个bean/>
<constructor-arg value="hello"/>


相比较而言,这种构造注入的方式没有依赖注入好,因为不仅使得构造器臃肿,最主要的spring创建bean时,需要初始化全部的依赖的bean

但是datasource使用构造注入,因为很多其他组件依赖于它,它必须优先于它们注入。

spring容器:

BeanFactory:管理bean,applicationContext是其子接口

//实例化beanFactory
Resource isr = new ClassPathResource("beans.xml");
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

//讲配置文件传入beanFactory
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(isr);


如果需要加载多个配置文件,则使用ApplicationContext

ApplicationContext app = new ClassPathXmlApplicationContext("a.xml","b.xml")


ApplicationContext

1.applicationContext在创建时,会初始化所有的singleton Bean(没有任何特殊配置),包括完成初始化和创建实例,但beanFactory不会。

如果想要阻止初始化,可在bean中设置

2.国际化支持

applicationContext继承了MessageSource接口

<bean id="messageSource" class="org.spring....MessageSource">
<property name="basenames">
<list>
<value>message_a</value>
<value>message_b</value>
</list>
</property>

/**MessageSource接口中定义的是
*getMessage(String code,Object[] a,Local loc);
*getMessage(String code,Object[] a,String default,Local loc);
**/
//调用
String a = ctx.getMessage("hellow",new String[]{"haha"},Local.getDefault(Locale.Category.FORMAT));


事件机制——观察者模式

ApplicationEvent和ApplicationListener

当ApplicationContext发布ApplicationEvent时,自动触发ApplicationListener

//event类
class EmailEvent extends ApplicationEvent{
//一个普通的java类
}
//listener
class EmailNotifier implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent evt){
if(evt instanceof EmailEvent)
}
}
//无需再代码中完成观察者,只要讲listener在beans中注册就行了
<bean class="xxxxx.EmailNotifier"/>

//实际调用
ApplicationContext ctx = new ...
EmailEvent ele = new EmailEvent();
ctx.publishEvent(ele);//触发事件时自动调用listener


ApplicationContextAware接口:使得bean可以获取spring容器

//将容器以变量形式保存
private ApplicationContext cx;
//spring在创建bean后会自动检测是否实现了aware接口,如果是,自动调用set方法,无需显式设置
public void setApplicationContext(ApplicationContext ctx){
this.ctx = ctx;
}


Bean的作用域

singleton:单例模式,整个spring容器中,只会生成一个实例

prototype:每次通过容器的getBean()方法获取prototype作用于的bean时,生成一个实例

request/session/global session

<bean id="p1" scope="singleton">//或者不写
<bean id ="p2" scope="prototype">

ctx.getBean("p1") == ctx.getBean("p1") true
ctx.getBean("p2") == ctx.getBean("p2") false


Bean 的创建方法

1.调用构造器创建Bean

2.使用静态工厂方法创建Bean

bean的class属性指向的是实现该实例的静态工厂类

interface Being{
public void testBeing();
}
class Dog implements Being{
private String msg;
public void testBeing(){
System.out.println(msg);
}
}
public class BeingFactory{
public static Being getBeing(String arg){
if(arg.equalsIgnoreCase("dog")){//忽略大小写
return new Dog();
}
}
}

//xml中配置
<bean id="dog" class="xx.BeingFactory" factory-method="getBeing">
<constructor-arg value=="dog"/>
<property name="msg" value="I am dog">
</bean>


3.实例工厂创建Bean

interface Person{}
class Chinese implements Person{}
class PersonFactory{
public Person getPerson(String a){
if(a.equalsIgnoreCase("chin")){
return new Chinese();
}
}
}
//配置文件
PersonFactory pf = container.get("personFactory");
chinese = pf.getPerson("chin");
<bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
<constructor-arg value="chin">
</bean>


抽象Bean

<bean id="personTemplate" abstract="true">
<property name="name" value="a"/>
</bean>
<bean id="chinese" class="" parent="personTemplate"/>


4.BeanNameAware

用于获取bean本身的id

协调作用域不同的Bean

比如一个singleton依赖于prototype,当初始化singleton时,就会生成对应的prototype实例,之后通过singleton调用prototype就不能够每次都产生新的实例

解决:方法注入(字节码修改)

public abstract class Chinese implements Person{
private Dog dog;
public abstract Dog getDog();
public void hunt(){
getDog();
}
}

//xml中配置
<bean id="chinese" class="xx.Chinese">
<lookup-method name="getDog" bean="gunDog"/>
</bean>
<bean id="gunDog" class="xx.GunDog" scope="prototype">
<property name="name" value="xiaobai">
</bean>


按我的理解,应该是spring按照配置文件每次调用chinese中hunt方法时,会动态修改字节码,加载具体的方法,所以每次生成的都是新的dog对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring