Spring前提

来源:互联网 发布:win10 企业版激活软件 编辑:程序博客网 时间:2024/06/18 00:23
前提:
接口:
.用于沟通的中介物的抽象化
.实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式
.对应java接口即声明,声明了那些方法是对外公开提供的
.在java8中,接口可以拥有方法体


面向接口编程:
.结构设计中,分清层次及调用关系,每层只向外提供一组功能接口,各层间仅依赖接口而非实现类
.接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要
.面向对象编程中的接口是用于隐藏具体实现和实现多态性的组件



单元测试:
.下载junit-*.jar并引入工程
.创建ubitTestBase类,完成对spring配置文件的加载,销毁
.所有的单元测试类都继承自UnitTestBase,通过它的getBean方法获取想要的对象
.子类(具体执行单元测试的类)加注解:@RunWith(BlockJUnit4ClassRUnner.class)
.单元测试方法加注解:@test
.右键选择执行的单元测试方法执行或者执行一个类的全部单元测试方法



Bean容器初始化:
.基础:两个包
 - org.springframework.beans
 - org.springframework.context
 - BeanFactory提供配置结构和基本功能,加载并初始化Bean
 -ApplicationContext保存了Bean对象并在Spring中被广泛使用
.方式,ApplicationContext
 - 本地文件
 - Classpath
 - Web应用中依赖servlet或Listener




bean容器初始化:
.文件: FileSystemXmlApplicationContext context = newFileSystemXmlApplicaitonContext("F:/workspace/application.xml");
.Classpath: ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext("classpath:spring-context.xml")
.web应用:
《listener》
 《listener-class》org.springframework.web.context.ContextLoaderListener《/listener-class》
 《/listener》
 《servlet》
 《servlet-name》context《/servlet-name》
 《servlet-class》org.springframework.web.context.ContextLoaderServlet》《/servlet-class》
 《load-on-startup》1《/load-on-startup》
 《/servlet》





Spring注入:
.Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
.常用的两种注入方式
 - 设值注入
 - 构造注入

《?xml version="1.0" encoding="UTF-8"?》
《beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd"》
       
《!--        《bean id="injectionService"   设值注入class="com.imooc.ioc.injection.service.InjectionServiceImpl"》--》
《!--        《property name="injectionDAO"ref="injectionDAO"》《/property》 --》
《!--        《/bean》 --》

《bean id="injectionService"   构造注入class="com.imooc.ioc.injection.service.InjectionServiceImpl"》
       《constructor-arg name="injectionDAO"ref="injectionDAO"》《/constructor-arg》
       《/bean》
       
       《bean id="injectionDAO"class="com.imooc.ioc.injection.dao.InjectionDAOImpl"》《/bean》
 《/beans》


public interface InjectionDAO {
public void save(String arg);
}
public class InjectionDAOImpl implements InjectionDAO {
public void save(String arg) {
//模拟数据库保存操作
System.out.println("保存数据:" + arg);
}

}

public interface InjectionService {
public void save(String arg);
}

public class InjectionServiceImpl implements InjectionService{
private InjectionDAO injectionDAO;
//构造器注入
public InjectionServiceImpl(InjectionDAO injectionDAO1){
this.injectionDAO = injectionDAO1;
}
//设值注入
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}

public void save(String arg) {
//模拟业务操作
System.out.println("Service接收参数:" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
public TestInjection() {
super("classpath:spring-injection.xml");
}
@Test
public void testSetter() {
InjectionService service =super.getBean("injectionService");
service.save("这是要保存的数据");
}
@Test
public void testCons() {
InjectionService service =super.getBean("injectionService");
service.save("这是要保存的数据");
}
}




Bean专题:


Bean配置项:
.Id       Bean的唯一标识
.Class     具体要实例化的那一个类
.Scope     作用域
.Constructor arguments   构造器的参数
.Properties       属性
.Autowiring mode   自动装配的方法
.lazy-initialization mode   懒加载方式
.Initialization/destruction method     初始化和销毁的方法


Bean的作用域:
.singleton:单例,指一个Bean容器中存在一份
.prototype:每次请求创建新的实例,destroy方式不生效
.request:每次http请求创建一个实例且仅在当前request内有效
.session:同上,每次http请求创建,当前session内有效
.global session:基于portlet的web中有效(portlet定义了globalsession),如果实在web中,同session

 

《?xml version="1.0" encoding="UTF-8"?》
《beansxmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd"》
       
       《bean id="beanScope"class="com.imooc.bean.BeanScope" scope="singleton"》《/bean》
       
 《/beans》

public class BeanScope {
public void say() {
System.out.println("BeanScope say : " +this.hashCode());
}
}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
@Test
public void testSay2() {
BeanScope beanScope  =super.getBean("beanScope");
beanScope.say();
}

}

结果:
BeanScope say : 12039161
BeanScope say : 12039161

BeanScope say : 9716945
每次单元测试之前都会调用before执行之后都会调用after,上面的2个测试是从2个实例中获取的。


将scope更改成prototype,结果是
BeanScope say : 20843194
BeanScope say : 3299256

BeanScope say : 6109469
1,3一样的,原因一样,1.2因为prototype每次请求创建新的实例,destroy方式不生效

0 0
原创粉丝点击