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

Spring 框架知识知识总结

2016-12-10 22:32 357 查看
1.xml文件中的bean起到中介的作用,从而对类之间的关系进行解耦

类与XML配置文件的配合关系:

a.实现类

package com.spring.test;

public class MyFactory {
//动态工厂
/*	public SomeServiceImpl someService(){
return new SomeServiceImpl();
}*/
//静态工厂模式
public static SomeServiceImpl someService(){
return new SomeServiceImpl();
}

}
b.测试类

public class TestService {

public void ServiceTest1(){
SomeServiceImpl ss=new SomeServiceImpl();
ss.doFirst();
ss.doLast();
}
@Test
public void ServiceTest2(){
String configLocation = "com/spring/test/applicationContext.xml";
//通常这样使用,初始化时便创建了对象,对比与类工厂创建,浪费了内存,但提高了执行效率
ApplicationContext at=new ClassPathXmlApplicationContext(configLocation);
ISomeService is=(ISomeService) at.getBean("someService"); is.doFirst(); is.doLast(); }}




b.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 注册Service对象 -->
<!--
<bean id="SomeService" class="com.spring.test.SomeServiceImpl"/>
-->
<!--动态工厂的方式创建Factory对象 -->
<!--
<bean id="serviceFactory" class="com.spring.test.MyFactory"/>非静态的方法需要新创建对象之后才能被调用,每个bean相当于一个new
<bean id="someService" factory-bean="serviceFactory" factory-method="someService"/>
-->
<!-- 静态工厂模式 -->
<!--  -->
<bean id="someService" class="com.spring.test.MyFactory" factory-method="someService"/><!-- 静态的方法直接用类名来调用 -->
</bean
2.容器中的bean作用域
scope=prototype 原型模式
scope=singleton 单例模式(默认)
WEB中:     

scope=request
scope=response 单例模式(默认)
3.Bean的装配:Bean后处理器
 通过代理模式来增强bean功能
//Bean后处理器测试方法
@Test
public void ServiceTest3(){

ApplicationContext at=new ClassPathXmlApplicationContext("com/spring/test/beanPost/applicationContext.xml");
ISomeService service=(ISomeService) at.getBean("someService1");
String result=service.doFirst();
System.out.println(result);
service.doLast();
System.out.println("-------------------");
ISomeService service2=(ISomeService) at.getBean("someService2");
String result2=service2.doFirst();
System.out.println(result2);
}
//Bean后处理器XML
<bean id="someService1" class="com.spring.test.beanPost.MyFactory" factory-method="someService"/><!-- 静态的方法直接用类名来调用 -->
<bean id="someService2" class="com.spring.test.beanPost.MyFactory" factory-method="someService" /><!-- 静态的方法直接用类名来调用 -->
<bean class="com.spring.test.beanPost.beanPostProcessor"></bean><!-- Bean后处理器在bean进行初始化时开始进行,不必命名ID -->
</beans>

//BEAN后处理器的实现类的BEAN功能增强

package com.spring.test.beanPost;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class beanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization( Object bean, String beanName)
throws BeansException {
System.out.println("*******执行doAfter方法******");
return bean;
}
@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName)

throws BeansException {
System.out.println("*******执行doFirst方法******");
Object proxy=null;
if ("someService1".equals(beanName)) {
proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(),
bean.getClass().getInterfaces(), new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
Object result = method.invoke(bean, args);
if (result != null) {
result = ((String) result).toUpperCase();
}
return result;
}
});
return proxy;//密切注意返回的对象的位置,否则可能造成空指针;
}
return bean;
}
}

3.Bean的装配:定制bean对象的生命始末

    关注点:销毁对象需要满足两点要求:

 (1)销毁的对象是singleton的,即单例的

(2)容器显示需是关闭的[ApplicationContext的接口是没有关闭方法的,但是其实现方法有关闭方法,接口变量需要强转成实现方法classpathXmlApplicationContext的类型进行关闭.close()]

//Bean定制生命始末
@Test
public void ServiceTest3(){

ApplicationContext at=new ClassPathXmlApplicationContext("com/spring/test/beanPost/applicationContext.xml");
ISomeService service=(ISomeService) at.getBean("someService");//注意xml文件的类的路径名  易出现类型无法转换为接口类的异常
String result=service.doFirst();
System.out.println(result);
service.doLast();
((ClassPathXmlApplicationContext)at).close();
}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: