Spring 笔记

Spring

控制反转IOC

IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建HelloService对象的控制权,交由Spring框架管理,简单说,就是创建HelloService对象控制权被反转到了Spring框架

BeanFactory,延迟实例化bean,第一次调用getBean
ApplicationContext 一般常用,功能更强
    ClassPathXmlApplicationContext 加载classpath xml文件
    FileSystemXmlApplicationContext 加载指定盘符文件 , ServletContext.getRealPath()
加载Spring容器
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("cn/ccj/a/hello/beans.xml");
ClassPathXmlApplicationContext(String)加载一个spring容器
ClassPathXmlApplicationContext(String[])加载多个spring容器

Spring DI 设置字段内容

DI:Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时(控制反转),动态的将依赖对象注入到Bean组件。
getBean(“helloService”)从spring容器中获得指定名称对象的实例时,通过此设置<property name="info" value="haha"></property> 相当于执行 servcie.setInfo(“传智播客”);

<?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">
    <bean id="helloService" class="cn.ccj.a.hello.HelloService">
        <!—通过spring容器,给HelloService类的info属性注入“传智播客”-->
        <property name="info" value="haha"></property>
    </bean>
</beans>

从spring直接获得注入的内容

package cn.ccj.a.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHello {

    public static void main(String[] args) {
        //使用spring 控制反转,交予spring创建对象
        ApplicationContext applicationContext = 
new ClassPathXmlApplicationContext("cn/ccj/a/hello/beans.xml");
        HelloService servcie = 
(HelloService) applicationContext.getBean("helloService");

        //servcie.setInfo("hello");
        servcie.sayInfo();
    }

}

2.Bean.xml的配置

类别说明

类别说明
singleton在Spring IoC容器中仅存在一个Bean实例,Bean以单例方式存在,默认值
prototype每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean()
request每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
session同一个HTTP Session 共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext 环境
globalSession一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext 环境
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <!-- 4 bean 作用域
        * singleton 单例,只创建一个实例。默认值
        * prototype 多例,每一次都创建实例
        * request,request范围,request.setAttribute
        * session,session范围,session.setAttribute
        * globalSession , prolet 分布式门户,sso(单点登录)将不同的应用的数据保存到globalSession中,达到数据共享
     -->
     <bean id="person" class="cn.ccj.c_beanscope.Person" scope="prototype"></bean>

</beans>

bean的生命周期

  1. instantiate bean对象实例化
  2. populate properties 封装属性
  3. 如果Bean实现BeanNameAware 执行 setBeanName
  4. 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
  5. 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
  6. 如果Bean实现InitializingBean 执行 afterPropertiesSet
  7. 调用 指定初始化方法 init
  8. 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
  9. 执行业务处理
  10. 如果Bean实现 DisposableBean 执行 destroy
  11. 调用 指定销毁方法 customerDestroy

Spring 注解

注解装配bean

  • @Component 普通组件bean注解
  • @Controller 用于配置表示层web
  • @Service 用于配置业务层逻辑service层
  • @Repository 用于配置数据访问层Dao

举例子

//表现层 web
@Controller("myweb")
public class Web {

    @Autowired
    private Service service;

    public void save(){
        service.save();
        System.out.println("web save");
    }

}
//业务逻辑service
@org.springframework.stereotype.Service
public class Service {

    @Autowired
    @Qualifier("myDao")
    private Dao dao;

    public void save(){
        dao.save();
        System.out.println("service save");
    }

}
//数据持久层 Dao层
@Repository("myDao")
public class Dao {

    public void save(){
        System.out.println("dao save");
    }
}
//bean.xml配置
//Spring容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 确定使用注解  -->
    <context:annotation-config></context:annotation-config>
    <!-- 自动扫描指定的包 -->
    <context:component-scan base-package="cn.ccj.spring"></context:component-scan>
</beans>
spring里面的注解

@Qualifier

@Autowired注解来指定自动装配,@Autowired可以修饰setter方法、普通方法、实例变量和构造器等。当使用@Autowired标注setter方法时,默认采用byType自动装配策略。在这种策略下,符合自动装配类型的候选Bean实例常常有多个,这个时候就可能引起异常,为了实现精确的自动装配,Spring提供了@Qualifier注解,通过使用@Qualifier,允许根据Bean的id来执行自动装配。

使用@PostConstruct和@PreDestroy定制生命周期行为

@PostConstruct和@PreDestroy同样位于javax.annotation包下,也是来自JavaEE规范的两个Annotation,Spring直接借鉴了它们,用于定制Spring容器中Bean的生命周期行为。它们都用于修饰方法,无须任何属性。其中前者修饰的方法时Bean的初始化方法;而后者修饰的方法时Bean销毁之前的方法。

@Component("book")
public class Book {

    @PostConstruct
    public void init(){
        System.out.println("初始化");
    }

    @PreDestroy
    public void destory(){
        System.out.println("销毁");
    }

}

@Scope(“singleton”)
* @Scope(“singleton”) 给当前bean配置范围,取值:singleton、prototype等

spring里面的注解详解

Spring基础知识汇总 Java开发必看

Spring的AOP

,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP(面向对象编程)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

经典应用:事务管理、性能监视、安全检查、缓存 、日志等
AOP核心概念

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值