Spring基础知识

一、Spring介绍
1.1什么是Spring
Spring是一个主要解决了对象依赖问题及现有技术的整合问题,同时彻底解决了事务方面的问题。
1.2自定义Spring
Spring实质是配置对象,创建工厂生产对象,使用对象时都要从工厂中获取

学习Spring 主要是学习IOC、DI、AOP、声明事务
轮子理论:现在的技术如果已经很成熟,那么就直接包容它,拿来用。
二、Spring快速入门
导入jar包
commons-lohhing.jar
spring-bean.release.jar
spring-context.release.jar
spring-core.release.jar
spring-expression.release.jar
编写applicationContext.xml配置文件
找到schema约束文件  找到bean约束
xsd-configuration.html
三、Spring原理分析
1.创建配置文件
2.编写配置文件(加入一些对象的配置信息)
3.Spring本身就是一个大工程,内部可以认为是xml文件解析,加上反射技术,就可以根据用户的配置信息,生成相应对象。
4.提供一个getBean()方法,供用户直接从工厂中获取对象

Spring创建同类型同属性值的对象,只会创建一次。单例对象
Spring在加载,读取配置文件,直接生成相应对象。
三、Spring对象生成以及注值方式
生成方式:默认使用类的无参构造来创建对象,静态工厂,实例工厂
1.无参构造创建对象
    <bean id="zhangsan" class="cn.lx.domain.Customer">
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
2.静态工厂创建对象 

    public class StaticFatory {
        public static Customer getStaticCustomer(){
            return new Customer();
        }   
    }
    <bean id="staticFactory" class="cn.lx.factory.StaticFatory" factory-method="getStaticCustomer">

3.实例工厂创建对象
    public class InstanceFactory {
        public Customer getInstance(){
            return new Customer();
        }
    }

    <bean id="instanceFactory" class="cn.lx.factory.InstanceFactory"></bean> 
    <bean id="c1" factory-bean="instanceFactory" factory-method="getInstance"/>
    <bean id="c2" factory-bean="instanceFactory" factory-method="getInstance"/>

注值方式:setter注值,构造构造函数注值,p名称空间注值,注对象
1.setter注值
    <bean id="zhangsan" class="cn.lx.domain.Customer" >
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
2.构造函数注值
    <!-- 构造 函数注值-->
    <bean id="lisi" class="cn.lx.domain.Customer">
    <!--
        构造函数参数
        index表示构造函数对应参数的位置,从0开始
        type表示参数的类型 java.lang,类型
        value表示参数的值

      -->
    <constructor-arg index="0" type="java.lang.Long" value="3000"></constructor-arg>
    <constructor-arg index="1" type="java.lang.String" value="张三"></constructor-arg>
    </bean>    
3.p名称空间注值
    <bean
    xmlns:p="http://www.springframework.org/schema/p">
    <!--p名称空间注值  -->
<bean id="wangwu" class="cn.lx.domain.Customer" p:custId="200" p:custName="王五"></bean>  
4.注入对象
    普通注入
    <bean id="zhangsan" class="cn.lx.domain.Customer" scope="">
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
    <bean id="zhangsan" class="cn.lx.domain.LinkMan" >

    <property name="customer" ref="张小三"></property>
    </bean> 
    构造器注对象:
<constructor-arg index="1" type="包名.类名" ref="zhangsan"></constructor-arg>
    P名称空间注对象
<bean id="aj" class="cn.itcast.domain.Linkman " p:custId="200" p:customer-ref="cgx"></bean>

关闭工厂 
    applicationContext子类.close()
四、Spring对象补充
4.1 对象生命周期
    <bean id="" class=""  init-method="" destroy-method=""></bean>
        init-method 就是指定初始化方法
        deftory-method 就是指定销毁方法
    1、当spring配置文件加载时,就会完成对象的实例化工作
    2、初始化,就会调用初始化方法
    3、当ApplicationContext对象销毁后,执行销毁方法
4.2 对象的作用域
bane上面配置一个属性  scope  
类别                 说明
sigleton           在spring loc 容器中仅存在一个bean实例,bean实例以单例方式存在
prototype         每次从容器中调用bean时,都返回一个新的实例,即存在多实例
request           每次http 请求都会创建一个新的bean,该作用域进适用于WebApplicationContext环境
session           同一个session中共享一个bean
主要使用singleton   prototype 
注意:配置Action类时,一定要配置scope="prototype"
五、spring、web、struts2整合
5.1 SprIng与web整合
目的是实现spring的配置文件只加载一次
注意导入jar包 spring-web.jar
1、首先配置web.xml文件     
<!-- 配置spring 与web项目结合
    需要配置一个监听器,实现启动服务器的时候加载spring配置文件,实现配置文件只加载一次
 -->
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!--因为监听默认加载的spring文件路径是/web-inf/applicationContext.xml 
    name:
    路径value:classpath:applicationContext.xml
  -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

2、获取容器的方法
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
WebApplicationContext wac1 = (WebApplicationContext) this.getServletContext().getAttribute(
            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
建议使用第一种方法
3、获取对象的方法   
Customer customer = (Customer) wac.getBean("c1");
5.2 SprIng与strutsb整合开发
注意导入jar 包
    struts-spring-plugin.jar
1、web.xml文件中红配置struts2核心控制器  StrutsPrepareAndExcuteFileter
2、web.xml 配置spring 对应的监听器   ContextLoaderLinstener
    同时配置初始化参数,用于加载spring配置文件   contextConfigLocation=classpath:applicationContext.xml
3、配置applicationContext.xml配置文件
4、更新程序,将所有获取对象的方式采用对象注入的方法获取
    Service层
        //使用对象注入的方式生成对象
        CustomerDao  customerDao;
        public void setCustomerDao(CustomerDao customerDao) {
            this.customerDao = customerDao;
        }
    Action类中
        //由对象注入的方式生成CustomserServcice对象
        private CustomerService customerService;

        public void setCustomerService(CustomerService customerService) {
            this.customerService = customerService;
        }
5、配置applicationContext.xml文件
      <bean id="customerDao" class="cn.lx.dao.imp.CustomerDaoImp" ></bean>
       <!-- 管理CustomerService -->
       <bean id="customerService" class="cn.lx.service.imp.CustomerServiceImp" >
            <property name="customerDao" ref="customerDao"></property>
       </bean>
       <!-- 管理CustomerAction
       scope=prototype 用于表示可以生成多实例
        -->
        <bean id="customerAction" class="cn.lx.web.action.CustomerAction"  scope="prototype">
            <property name="customerService" ref="customerService"></property>
       </bean>
        注意:配置Action时,一定要声明scope="prototype"属性,将生成的Action对象变为多实例
6、配置struts.xml文件
    注意:class需要指定在applicationContext.xml中配置对应的Action的id值的<bean id="" class="">
    <action name="customerAction_*" class="customerAction" method="{1}" >
    </action>
7、测试是否成功运行
注解开发Spring
1.加入aop.jar包
2.引入context.schema约束
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 测试使用注解进行开发 -->

    <!-- 代表在指定的base-package=“cn.lx”包开头的包中,都要进行组件扫描,也就是查找哪些使用了注解 -->

    <context:component-scan base-package="cn.lx"></context:component-scan>

    </beans>
3、配置注解
    dao层
        类上@Repository("customerDao")//相当于<bean id="customerDao">
    service
        类上@Service("customerService")//<bean id="customerService">

        对象上@Autowired//按类型取值
            @Qualifier("customerDao")//相当于ref的取值
        private CustomerDao  customerDao;
        public void setCustomerDao(CustomerDao customerDao) {
            this.customerDao = customerDao;
        }
    action类
        类上@Controller("customerAction")//<bean id="customerAction">
            @Scope("prototype")//多实例

        对象上@Autowired
            @Qualifier("customerService")
        private CustomerService customerService;

        public void setCustomerService(CustomerService customerService) {
            this.customerService = customerService;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring是一个广泛使用的轻量级开源框架,用于构建企业级Web应用程序。Spring框架提供了模块化的解决方案,简化了企业级应用的开发、管理以及依赖注入(Dependency Injection,DI)等功能。以下是一些Spring基础知识点的整理: 1. **IoC(Inversion of Control)和DI(Dependency Injection)**:Spring的核心思想就是IoC,它反转了传统的控制流动,使对象之间通过容器管理彼此的依赖关系,而不是硬编码。DI是IoC的一种具体实现方式,通过配置文件或注解自动为对象提供所需依赖。 2. **Bean的作用域和生命周期**:Spring中的Bean有多种作用域,如Singleton(单例)、Prototype(原型)、Request、Session等。每个Bean都有其生命周期,从创建、初始化到使用和销毁。 3. **Spring配置文件**:通常使用XML配置文件(如applicationContext.xml)或Java配置(@Configuration classes)来定义Spring应用的组件和依赖关系。 4. **AOP(Aspect Oriented Programming)**:Spring AOP支持面向切面编程,可以编写跨组件的行为,比如日志记录、事务管理等。 5. **Spring MVC**:Spring提供的web MVC架构,包括Controller处理HTTP请求,Model负责数据访问和业务逻辑,View负责渲染结果给用户。 6. **Spring Boot**:Spring Boot简化了Spring应用的初始搭建,自动配置了许多常用的功能,使得快速开发变得更容易。 7. **Spring Data**:提供了一套高级API,用于简化数据访问操作,如JPA、MongoDB等。 8. **Spring Security**:用于实现Web应用的安全管理,包括认证、授权、会话管理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值