初识spring框架<1>

spring框架基础

概念

  1. 1Spring是一个轻量级的DI、IOC与AOP的容器框架
  2. 轻量级:使用更加简单(功能不够强大)
  3. DI(依赖注入)/IOC(控制反转):->spring创建对象把对象给你
  4. AOP:面向切面编程-》解决重复代码
  5. 容器:面向过程->面向对象->面向接口->面向工厂
    (解耦)装的都是对象
  6. 高内聚低耦合
  7. spring作为工厂使用
  8. spring帮我们整合外面的框架

Spring Hello

  1. 导包
    Spring以前是一个jar包,后面变成了一堆jar包
    core:核心包----产生Spring
    bean:想要–创建对象,必须要bean
    context:扩展spring功能
    expression:表达式包(有它之后才能写XML中写
    logging:日志包(是一个依赖包,不导入的话会报错)

  2. 写配置

    <?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">
       <!--
            Spring要帮我们创建对象,我们把创建的对象都叫做bean
            咋们得写配置告诉Spring,去帮咋们创建哪一个对象
            bean:配置一个对象
                class:要配置对象的类型
                id:这个bean的唯一
       -->
        <bean id="helloBean" class="cn.itsource._01_hello.HelloBean"></bean>
    
    </beans>
    
  3. 拿核心对象(启动框架)–的三种方式–(BeanFactory)

    public void testHello() {
            //获得核心配置文件
            Resource classPathResource = new ClassPathResource("applicationContext.xml");
            //拿到核心对象
            BeanFactory Factory = new XmlBeanFactory(classPathResource);
            //拿到配置对象
            //更根据名称获取bean的方式。。第一种
    
            //HelloBean helloBean = (HelloBean)Factory.getBean("HelloBean");
            //第二种..根据类型获取bean对象---缺陷不能出现多个类型
            HelloBean bean = Factory.getBean("helloBean",HelloBean.class);
            System.out.println(bean);
        }
    

    4.集成启动框架的策葎–使用(ApplicationContext)

    public void testHello02(){
            ClassPathXmlApplicationContext Factory = new ClassPathXmlApplicationContext("applicationContext.xml");
            HelloBean helloBean = Factory.getBean("helloBean", HelloBean.class);
            System.out.println(helloBean);
        }
    
    • BeanFactory和ApplicationContext的区别

    BeanFactory使用的时懒加载策略,当你用的时候我才创建出来

    懒加载:用到的时候才创建
    ApplicationContext是及时加载策略,框架已启动自动创建

DI依赖注入

IOC控制反转

public class Mybean {
    /*
    * DI:依赖注入
    * 这个对象的创建不是我来做的而是依赖Spring创建对象,由Spring把值注入进来
    * IOC:控制反转
    * 这个对象的控制权(对象创建,销毁的过程)都反转(全部交给Spring去控制)
    * */
    private Yourbean yourbean;
    public void save(){
        System.out.println("yourbean"+yourbean);
    }
}

运行结果:运行截图

普通值注入(xml注入)

可以直接设置值

注解注入

@Autowired:在Spring只要看到这个注解就会直接将你要的对象注入进来

用其他的方式启动框架

目的:让测试在Spring的环境中

  1. 导包:Test(测试),aop包(面向切面)
    在测试中用到了面向切面的功能
  2. 搭建环境:咱们得想个办法:在运行这个类之前,Spring就已经运行起来了(测试类中夹注解)@Runwith(SpringJUnit4ClassRunner.class)
    这个注解告诉你:这个测试是使用Spring的测试来完成ContextConfiguration:上下文配置->读取配置文件
    我们可以配置路径:classpath : applicationContext.xml
    如果你没有配置路径,它默认会去找=〉当前类名-Context.xml文件(SpringTest-Context .xml
  3. 获到spring中的对象(使用注解注入)Autowired
    private scopeBean scopeBean;

Bean对象的作用域

​ Spring创建的对象单例的还是多例的
​ 将Spring创建的对象变为多例在xml配置文件中加入scope=“prototype”
​ 配置文件
​ 及时加载(使用时才创建)转换为懒加载(在创建完类之后才创建出来)

<?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"
       default-lazy-init="true"
>

在bean中加lazy-init="true"实现对象个体的懒加载

对于Spring的细节认识

  1. scope:作用域
    默认的bean都是单例的,通过配置scope="prototype"改成多例

  2. 懒加载

  3. Bean的生命周期:创建,初始化,销毁

    <bean id="test05" class="cn.itsource._03_1011Test.test05"
       init-method="init" destroy-method="destory"
       
    ></bean>
    

Bean的自动装配

  • 自动装配
  • Spring会在上下文中自动寻找,并给bean装配属性

在Spring中的三种装配方式

  • 在xml中显示的配置(中使用autowise="byName"来实现bean的自动装配)

    • byName自动装配
    <bean id="people" class="com.kuang.pojo.People"   autowire="byName">
        <property name="name" value="大帅哥" />
    </bean>
    
    • byType自动装配
    <bean class="com.kuang.pojo.Cat"></bean>
        <bean class="com.kuang.pojo.Dog"></bean>
        <bean name="people" class="com.kuang.pojo.People" autowire="byType">
            <property name="name" value="大帅哥"/>
        </bean>
    

    小结:byname的时候需要保证所有bean的唯一,并且都有唯一的自动属性注入set方法的值一致

    bytype的时候需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致

    • 使用注解实现自动装配
  • 在java中显示配置

  • 隐式的自动装配bean

一定要主页添加注解实现方式
  • 详解动态代理的模式

  • 动态代理的代理类时东陶生成的不是我们直接写好的

  • 动态代理分为两大类:基于接口的动态代理,基于类的动态代理

    • 基于接口----JDK动态代理
    • 基于类
    • java字节码的实现

需要了解两个:Proxy,incocationHandler(调用处理程序)

AOP

什么是AOP

概念:面向切面编程通过预编译

AOP实现接口----在Spring中实现AOP类的作用
<! --方式一:使用原生Spring API接口 --><! --配置aop:需要导入aop的约束-->
< ! --<aop : config>--
<!--&Lt; !&ndash;切入点: expression:表达式,execution(要执行的位置! *****&ndash;&gt; -
<!--<aop:pointcut id="pointcut" expression="execution(*com.kuang.service.UserServiceImpl.*( ..))"/>--><! --&lt; ! &ndash;执行环绕增加!&ndash;&gt;-->
< !--<aop : advisor advice-ref="Log" pointcut-ref="pointcut" />-->
<!--<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut" />-->< ! --</ aop:config>-->
                                               
                                               
< ! --方式二:自定义类-->
<bean id="diy" class="com.kuang.diy.DiyPointcut" />
<aop:config>
<! --自定义切面. ref要引用的类--><aop: aspect ref="diy">
<! --切入点-->
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImp1.*(..))"/><! --通知-->
<aop: before method="before" pointcut-ref="point" /><aop:after method="after" pointcut-ref="point" /></ aop:aspect>
</ aop: config>
                                               
<! --方式三:使用注解开发AOP类的实现 -->                                               
@Aspect //标注这个类是一个切而public class AnnotationPointcut i
@Before( "execution(* com.kuang.service.UserServiceImpl.*(..))")public void before(){
system.out.printin("=====方法执行前======");
}
@After( "execution(*com.kuang.service.UserServiceImp1.*(..))")public void after()i
system.out.print1n( "=====方法执行后======");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点;@Around("execution(* com.kuang.service.UserService1mpl.*(..))")public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
signature signature = jp.getsignature();//获得签名System.out.println("signature : "+signature) ;
object proceed = jp.proceed();1/执行方法system.out.println("环绕后");
system.out.println(proceed);
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值