spring基本使用

一.spring概述

1.spring的概念

  • spring是开源的,轻量级的框架
  • spring的核心主要是:

(1)aop(面向切面编程) :增加新的功能不需要修改源代码

(2)ioc(控制反转):把类的创建交给了配置来实现

  • spring是一站式框架

spring实现了javaee中3层架构中的每一层的功能,也就是在service,dao,web层都有实现,可以不需要hibernate,struts2

(1)web层:springMVC
(2)service层:spring的ioc
(3)dao层:spring的jdbcTemplate

  • spring使用4.x版本

2.spring的ioc

(1)ioc实现的方式2种:

  • 使用配置文件的方法
  • 使用注解的方式

(2)ioc底层的原理

  • 使用的技术:

1 xml配置文件
2 dom4j解析xml
3 工厂设计模式
4 反射

  • ioc实现过程

1 创建xml配置文件
2 创建工厂类,在其中解析xml文件,使用反射创建类的实例.

二.spring入门

1.spring的ioc入门

ioc和di的区别

ioc:控制反转,吧对象的创建交给spring 
di: 依赖注入,向类中的属性设置值
关系:依赖注入不单独存在,需要在ioc的基础之上实现
spring中ioc的操作

(1)导入jar包,
初始需要6个包:bean,content,core,express,commons-logging,log4j

(2)创建类

创建一个类,类中创建一个实例方法

(3)创建配置文件

schema约束:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- bean definitions here -->
</beans>

(4)测试文件

//加载配置文件,注意路径,本例在src下
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
//得到实例,参数是配置文件中bean中的id
MyClass myClass=(MyClass)context.getBean("myClass");
myClass.add();

2.spring的bean管理

配置文件的方式

通过配置文件创建类的实例化操作,bean的实例化操作有3中方式:

set方法(主要使用)

配置文件
<bean id="myClass" class="com.spring.ioc.MyClass"></bean>

ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
MyClass myClass=(MyClass)context.getBean("myClass");
myClass.add();

静态工厂方法
实例工厂方法

bean标签的常用属性

* id:取得名字
* class:所在类的全路径
* name: 功能上和id相同,一般不用,处理历史版本问题
* scope:创建类的方式:
    - singleton(默认): 单实例
    - prototype      : 多实例
    - request        : 创建对象放到request域中
    - session        : 创建对象放到session中
    - globalSession  : 创建对象放到globalSession中
注解的方式

通过注解的方式,比较简单,同样能实现配置文件的功能

* 在原来的基础上加入jar包,aop包
* 新建class类,在类名上加入注入, 
        --- @Component(value="user"),这里的user相当于原来配置文件的id
    创建对象有4中,在不同的层用不同的注解单词,但是现在(2017/5/6)功能相同
    @Component , @Controller , @Service , @Repository
* 写测试文件,与上面写测试方法一样,测试通过.
scope的注解
@Scope(value="prototype")  ,--多实例
属性注入
1.  @Autowired
    private UserDao userDao;
2. @Resource(name="userDao");  //用的多些
    private UserDao userDao;

3.spring的属性注入

就是在创建类的实例的时候,将类中的属性设置相应的值
1.属性注入的方式

(1)使用类中属性的set方法注入(spring主要使用)

注入字符串
----------------------------------------------------------------------
1.在类中要创建属性和属性的set方法
    private String username;
    public void setUsername(String username) {
        this.username = username;
    }
2.在配置文件中进行配置
    <bean id="myClass" class="com.spring.ioc.MyClass">
        <!-- 配置属性和属性值 name:属性名,value:属性值-->
        <property name="username" value="申玉超"></property>
    </bean>

注入对象
------------------------------------------------------------------------
1. 在类中要创建属性和属性的set方法
    private Dao dao;
    public void setDao(Dao dao) {
        this.dao = dao;
    }
2.在配置文件中进行配置
<bean id="dao" class="com.spring.testioc.Dao"></bean>
    <bean id="service" class="com.spring.testioc.Service">
        <property name="dao" ref="dao"></property>
</bean>

(2)使用类的有参数构造函数注入
(3)接口注入的方法

4.spring的AOP

AOP思想

(1)AOP概述

AOP采用动态代理的方式,横向抽取的方式,来实现AOP思想.

(2)AOP底层原理

使用动态代理的方式,当有接口的时候,在实现一个接口实现类,这个类用于增强原来的实现类的方法;当没有接口的时候,实现一个继承类,继承那个需要增强方法的类,然后在子类中调用父类方法完成AOP.

(3)AOP操作相关术语

  • jointPoint(连接点):就是类中能够被增强的那些方法
  • pointCut(切入点):就是类中被增强了的那些方法
  • Advice(通知/增强):增强了的功能
    • 前置通知:方法执行之前
    • 后置通知:方法执行之后
    • 异常通知:方法出现异常
    • 最终通知:在后置通知后执行
    • 环绕通知:在方法执行前和执行后都执行
  • Aspect(切面):把通知/增强加入切入点的过程
AOP操作
aspectj的表达式写法:
1.execution(* com.spring.aop.Book.add(..))
2.execution(* com.spring.aop.Book.*(..))
3.execution(* *.*(..))

(1)导入4个jar包
spring-aop , spring-aspect , aopalliance , aspectjweaver

(2)新增配置文件(使用配置文件的方式),要有aop的约束

(使用配置文件的方式)
-----------------------------------------------------------------------
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->


    <!-- 配置对象 -->
    <bean id="book" class="com.spring.aop.Book"></bean>
    <bean id="myBook" class="com.spring.aop.MyBook"></bean>

    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切入点,被增强的 ,id可以自取名称-->
        <aop:pointcut expression="execution(* com.spring.aop.Book.*(..))" id="bookPoint"/>
        <!-- 配置切入面,增强的类 ,把增强用在切入点上-->
        <aop:aspect ref="myBook">
            <aop:before method="before" pointcut-ref="bookPoint"/>
        </aop:aspect>

    </aop:config>
</beans>    
使用注解的方式
---------------------------------------------------------------------
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

    <!-- 开启aop操作 -->    
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- 配置对象 -->
    <bean id="book" class="com.spring.aop.Book"></bean>
    <bean id="myBook" class="com.spring.aop.MyBook"></bean>
</beans>    

接下来在MyBook中添加注解@Aspect

在MyBook中的方法前注解 @before(value="execution(* com.spring.aop.Book.*(..))")

(3)写两个类,一个是被增强的类,一个是增强的类

(4)测试,测试通过

5.log4j介绍

加载配置文件

6.spring整合web项目

(1)配置文件,先从spring开始

  • 导入jar包,导入所有struts2jar包,springjar包,还有spring整合web的包
  • 配置从spring开始:

    配置bean1.xml文件
    配置struts2.xml文件
    配置web.xml文件
    配置log4j.properties文件

  • 写Java代码

    action:需要调用service
    service:调用dao
    dao:

  • 将action的创建中加载spring的xml文件,创建对象的过程通过在web.xml中配置先建立起来
  • 测试通过

三.spring其他内容

1.spring 的jdbcTemplate操作

2.spring连接池配置

3.spring事务管理

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值