java-------------Spring 学习01

        
            
------------------------------------------------------------------------------------------------------------------------------------------------
 1、Spring概述:Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
      主要是两大技术:1、IOC (Inverse of Control 控制反转);
                                   2、AOP  (Aspect Oriented Programming 面向切面编程);
                                    
2、Spring优点:
         1、方便解耦,简化开发
                  Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
          2、AOP编程的支持
                   Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
          3、声明式事务的支持
                     只需要通过配置就可以完成对事务的管理,而无需手动编程

          4、方便程序的测试
                     Spring对Junit4支持,可以通过注解方便的测试Spring程序

          5、方便集成各种优秀框架
                   Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
           6、降低JavaEE API的使用难度
                      Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

3、Spring 体系结构:
    Spring 框架是一个分层架构,,它包含一系列的功能要素并被分为大约20个模块。这些模块分为Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation和测试部分。

4、Spring入门案例:
            
                        传统的创建对象是new对象,而使用Spring框架后,使用xml配置文件实例化对象。
                        
                        //创建实体类:
                                
                                public class Student {

                                    public void show(){
                                        System.out.println("Student Show..........");
                                    }
                                }
                            
                                public class Student2 {
                                    
                                    public static Student careStu(){
                                        return new Student();
                                    }
                                }
                                
                                public class Student3 {
                                
                                    public Student careStudent(){
                                        return new Student();
                                    }
                                }
                        
                        //添加配置文件:
                        
                                <?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:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
                                    
                                    <!--1.使用类构造器实例化(默认无参数)-->
                                    
                                        <bean id="stu" class="com.zltz.Student"></bean>
                                    
                                    <!--2.使用静态工厂方法实例化(简单工厂模式)-->
            
                                        <bean id="studentStatic" class="com.zltz.test.Student2" factory-method="careStu"></bean>
                                        
                                    <!--3.使用实例工厂方法实例化(工厂方法模式):-->

                                        <bean id="stu" class="com.zltz.test.Student3"></bean>
                                        <bean id="stu3" factory-bean="stu" factory-method="careStudent"></bean>
                                
                                </beans>
                        
                        //测试类

                                public class StuTest {

                                        public static void main(String[] args) {

                                            //ApplicationContext 应用上下文,加载Spring 框架配置文件
                                            //加载classpath

                                            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

                                            //通过 getBean方法获得Spring容器管理Bean对象

                                            Student studentService = (Student) applicationContext.getBean("studentService");

                                            //调取实体类中的方法
                                            studentService.show();


                                            //2、 使用静态实例工厂

                                            Student studentStatic = (Student) applicationContext.getBean("studentStatic");
                                            studentStatic.show();

                                            //3、使用工厂方法实例化

                                            Student student= (Student) applicationContext.getBean("stu3");
                                            student.show();

                                        }
                                    }


            BeanFactory和ApplicationContext区别:
                                
                                BeanFactory 采取延迟加载,第一次getBean时才会初始化Bean

                                ApplicationContext是对BeanFactory扩展,提供了更多功能
                                
                                国际化处理
                                
                                事件传递
                                
                                Bean自动装配
                                
                                各种不同应用层的Context实现

            5、Spring中bean的作用域:
            
                        singleton:在Spring IoC容器中仅存在一个Bean实例,Bean以单例方式存在。
                        
                        prototype:每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean()。
                        
                        request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境。
                        
                        session:同一个HTTP Session 共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext 环境。
                        
                        globalSession:一般用于Porlet应用环境,该作用域仅适用于WebApplicationContext 环境。
                        
                        
                        
            6、Spring容器中Bean的生命周期
                            
                            1、初始化和销毁必须在配置文件中设置两个属性  <bean id=“foo” class=“...Foo” init-method=“setup” destory-method=“teardown”/>
                            instantiate bean对象实例化
                            
                            2、populate properties 封装属性
                            
                            3、如果Bean实现BeanNameAware 执行 setBeanName
                            
                            4、如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
                            
                            5、如果存在类实现BeanPostProcessor (后处理Bean) ,执行postProcessBeforeInitialization
                            
                            6、如果Bean实现InitializingBean 执行 afterPropertiesSet 
                            
                            7、调用<bean init-method="init"> 指定初始化方法 init
                            
                            8、如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
                            
                            9、执行业务处理
                            
                            10、如果Bean实现 DisposableBean 执行 destroy(新版本已经取消)
                            
                            11、调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy
                            
                    
                    
            7、依赖注入Bean的属性:
            
                        对于类成员变量,注入方式有三种
                    
                        1、构造函数注入
                        
                        2、属性setter方法注入
                        
                        3、接口注入
                        
                        注意:Spring支持前两种

                    
                1、构造函数注入:
                
                        在bean节点中写入<constructor-arg>设置注入的属性 (可以通过index或者type注入
                        
                2、属性setter方法注入:
                
                        <bean id="stuper" class="com.zltz.student.StuPer">
                            <property name="id" value="10"></property>
                            <property name="namme" value="真龙天子"></property>                //其他普通属性值
                            <property name="age" value="21"></property>
                            
                    集合注入:    
                            <property name="hobbby">
                                <list>
                                    <value>音乐</value>
                                    <value>美术</value>                        //list集合添加属性
                                    <value>书法</value>
                                </list>
                            </property>
                            
                            <property name="hobbby">
                                <set>
                                    <value>音乐</value>
                                    <value>美术</value>                        //set集合添加属性
                                    <value>书法</value>
                                </set>
                            </property>
                            
                            <property name="hobbby">
                                <map>
                                    <entry  key="id">1</entry>
                                    <entry  key="name">123</entry>            //map集合添加属性
                                    <entry  key="age">18</entry>
                                </map>
                            </property>
                            
                                <property name="hobbby">
                                <map>
                                    <prop  key="id">1</prop>
                                    <prop  key="name">123</prop>            //properties集合添加属性
                                    <prop  key="age">18</prop>
                                </map>
                            </property>
                            
                        </bean>
                        
                        

        使用多个XML配置文件:
        
                    1、方式一 可以在创建ApplicationContext对象时传入多个配置文件
                        
                        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");

                    2、方式二 可以在配置文件中通过<import>引入其他配置文件
                    
                        <import resource="classpath:bean2.xml"/>

            


                


 

猜你喜欢

转载自blog.csdn.net/qq_42891281/article/details/105126945