博文

目前显示的是 十一月, 2016的博文

Bean作用域和生命周期(三)

图片
Scope Description singleton This scopes the bean definition to a single instance per Spring IoC container (default). prototype This scopes a single bean definition to have any number of object instances. request This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext. session This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. BeanScope.java public class BeanScope{   public void say(){     System.out.pringln("BeanScope say"+this.hashCode()); } } spring-beanscope.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"     xsi:sch...

Spring注入方式(二)

图片
Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为 1.设值注入 2.构造注入 InfectionService.java public interface InfectionService{   public void save(String arg); } InfectionDAO.java public interface InfectionDAO{   public void save(String arg); } InjectionDAOImpl.java public class InjectionDAOImpl implements InfectionDAO{    public void save(String arg){ //模拟数据库保存操作     System.out.println("保存参数:"+arg"); } } InjectionServiceImpl.java public class InjectionServiceImpl implements InjectionService{   private InjectionDAO injectionDAO;   //设值注入setter  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); } } spring-injection.xml 设值注入 TestInjection.java import org.junit.Test; @...

spring框架(一)

图片
spring简介 慕课网 http://www.imooc.com/video/3668 IOC(配置,注解) Bean(配置,注解) AOP(配置,注解,AspectJ,API) 如何学习Spring:掌握用法 深入理解 不断实践 反复总结 资源: http://spring.io http://projects.spring.io.spring-framework spring概况 spring是一个开源框架,已经不至于企业级 轻量级的控制反转(Ioc) 面向切面(AOP)的容器框架 面向接口编程 结构设计中,分清层次及调用关系,每层只向外提供一组功能接口,各层间只依赖接口而非实现类(接口实现的变动不影响各层间的调用) --OneInterface.java public interface OneInterface(){   String hello(String word); } --OneInterfaceImpl.java public class OneInterfaceImpl implements OneInterface{   public String hello(String word){    return "word from interface\"OneInterface\":"+word; } } --Main.java:  OneInterface oif=new OneInterfaceImpl();  System.out.println(oif.hello("word"); 什么是IOC IOC:控制反转,控制权的转移。应用程序本身不负责对象的创建和维护,而是由外部容器负责创建和维护 IOC机制提供了对象 DI(依赖注入)是一种实现方式 Spring的bean配置 /resources/spring.ic.xml 单元测试 --TestOneInterface.java import org.junit.Test; @RunWith(BlockJUnit4ClassRunner.class) public class TestOneInterf...