Spring笔记1---概述

1 构造注入  

<?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-3.0.xsd>

<bean id=”quest” class=”com.springinaction.knights.SlayDragonQuest” />

<bean id=”knight” class=”com.springinaction.knights.BraveKnight”>

  <constructor-arg ref=”quest”/>

   <constructor-arg value="1/>//直接参数

<constructor-arg><bean class="xxx" /></constructor-arg>

</bean>

</beans>

2 加载XML方式1

 

package test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpeakTest2 {

    public static void main(String[] args){

       ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");

       Speak s = (Speak)appContext.getBean("speak",Speak.class);

       System.out.println(s.getMessage());

    }

}

 3 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

        xmnls:aop="http://www.springframework.org/schema/aop"

        Xsi:schemaLocation=”http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>

<!-- 执行具体切面逻辑的类 -->

<bean id="minstrel" class="x.x.x" />

<aop:config>

    <aop:aspect ref="minstrel">

        <aop:pointcut id="embark" expression="execution(* *.embark(..))" />

        <aop:before pointcut-ref="embark" method="beforeMethod" /><!-- 前置通知 -->

        <aop:after pointcut-ref="embark" method="afterMethod" /><!-- 后置通知 -->

    </aop:aspect>

</aop:config>

<beans/>

 4 JDBC模板

 

public Employee getEmployeeById(long id){

       return jdbcTemplate.queryForObject(

              "sql from where id=?",

              new RowMapper<Employee>(){

                  public Employee mapRow(ResultSet rs,int rowNum)throws SQLException{

                     ...

                     return employee对象~

                  }

              },id<---具体的参数...

              )

       }

    }

 5 关于容器的类型

Bean工厂---org.springframework.beans.factory.BeanFactory

提供基本的DI支持

应用上下文---org.springframework.context.ApplicationContext基于BeanFactory

提供面向应用的服务! 有以下几种类型:

ClassPathXmlApplicationContext---从类路径下的XML配置文件中加载上下文定义,把应用上下文定义文件当做类资源。

FileSystemXmlApplicationContext---读取文件系统下的XML配置文件并加载上下文定义。

XmlWebApplicationContext---读取web应用下的XML配置文件并装载上下文定义。

 

 Bean的生命周期
1 Spring对Bean进行实例化
2 Spring将值和Bean的引用注入进Bean对应的属性中。
3 如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()接口方法。
4 如果bean实现了BeanFactoryAware接口,spring将调用setBeanFactory()接口方法,将BeanFactory容器实例传入。
5 如果Bean实现了ApplicationContextAware接口,spring将调用setApplicationContext()接口方法,将应用上下文的引用传入。
6 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessBeforeInitialization()接口方法。
7 如果bean实现了InitializingBean接口,spring将调用它们的afterPropertiesSet()接口方法,类似的,如果bean使用
init-method声明了初始化方法,该方法也会被调用。
8 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessAfterInitialization()方法。
9 此时,bean已经准备就绪,可以使用,一直驻留在应用上下文中,直到应用上下文被销毁。
10 如果bean实现了DisposableBean接口,spring将调用它的destroy()接口方法,同样,如果bean使用destroy-method声明了
销毁方法,该方法也会被调用。

 命名空间

aop

beans

context

jee

jms

lang

mvc

oxm

tx

util

------------------------

 

 

 

你可能感兴趣的:(依赖注入)