spring框架常用

一.IOC的原理

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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">

<bean id="user" class="entity.FirstBean" scope="prototype"></bean>

 <!-- 
set方式注入
name属性:类里面定义的属性名称
value属性:是我们设置的具体的值
ref:以来的bean的id值(我们自己设置的)
 -->
<bean id="examplBean" class="entity.ExamplBean" scope="singleton">
    <property name="firstBean" ref="user"></property>
</bean>


<!-- 
    1.加载spring核心配置文件
    //初始化IOC容器,参数值是要加载的配置文件相对于src的相对路径
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   (1)new对象,功能可以实现,效率很低
   2.实现思想:把加载配置文件和创建对象过程,在服务器启动时候完成
   3.实现原理
    (1)ServletContext对象
    (2)监听器
    (3)具体使用
        在服务器启动的时候,为每个项目创建一个ServletContext对象
        在ServletContext对象创建的时候,使用监听器可以监听到ServletContext对象在什么时间创建的
        加载spring配置文件,把配置文件配置对象创建
        把创建出来的对象放到ServletContext域对象里面(setAttribute方法)
        互取对象时候,到ServletContext域对象里面(getAttribute方法)
 -->

</beans>

public static void main(String[] args) {
//初始化IOC容器,参数值是要加载的配置文件相对于src的相对路径
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据xml文件中的id来创建对象
FirstBean firstBean = (FirstBean) context.getBean("first");
//调用方法
firstBean.method1pspeak();
}

AOP的原理
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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">

 <bean id="first" class="entity.FirstBean" ></bean>
 <!-- 通知类 -->
 <bean id="advisor" class="advice.advisor" ></bean>
 <!-- 配置切入点  value=".*method1" 可以以方法名结尾也可以以speak结尾-->
 <bean id="speakerPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="pattern" value=".*speak"></property>
 </bean>
 <!-- 配置顾问 -->
 <bean id="speakadvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="advisor"></property>
        <property name="pointcut" ref="speakerPointCut"></property>
 </bean>
 <!-- 配置代理 -->
 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

</beans>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring 复习 [toc] spring介绍 三层架构中spring位置,连接三层。 spring一站式框架正...
    inke阅读 4,151评论 0 11
  • 一、Spring框架 1.1 Spring框架是什么 Spring是一种容器框架,用于配置各个组件(bean)并且...
    Jane_Static阅读 3,030评论 0 0
  • 专业术语了解 组件/框架设计侵入式设计 : 引入了框架,对现有的类的结构有影响;即需要实现或继承某些特定类。例如:...
    奋斗的老王阅读 4,117评论 1 50
  • IOC的概念 IOC Inversion of Controller 控制反转。IOC 就是将对象的创建、初始化及...
    小窗风雨阅读 2,675评论 0 0
  • 今天内容概述 Spring框架的概述 SpringIOC的快速入门 IoC容器XML的方式 在web项目中集成Sp...