Spring是一个基于IOC和AOP的结构J2EE系统的框架。
  • IOC 反转控制 是Spring的基础,Inversion Of Control 简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象 
  • DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可
  1. 新建项目,并导入相关的jar包
  2. 准备一个 pojo类
    public class Category {
     
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        private int id;
        private String name;
    }
  3. 在src目录下新建applicationContext.xml文件
    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
        <bean name="c" class="com.how2java.pojo.Category">
            <property name="name" value="category 1" />
        </bean>
      
    </beans>
  4. TestSpring通过spring获取Category对象,以及该对象被注入的name属性。
    public class TestSpring {
     
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "applicationContext.xml" });
     
            Category c = (Category) context.getBean("c");
             
            System.out.println(c.getName());
        }
    }
    拓展:如果是在一个pojo类中引入另一个pojo作为属性,则通过ref
    <bean name="c" class="com.how2java.pojo.Category">
            <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>

以注解的方式完成对象属性的注入

<context:annotation-config/>
在实体类中@Resource等同于@Autowired
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    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
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <context:annotation-config/>
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
<!--         <property name="category" ref="c" /> --> 这个行为在后面将使用注解来完成     </bean>
  
</beans>
public class Product {
 
    private int id;
    private String name;
    @Autowired              //在Product.java的category属性前加上@Autowired注解,也可以在setCategory方法前加上@Autowired     private Category category;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Category getCategory() {
        return category;
    }
 
    public void setCategory(Category category) {
        this.category = category;
    }
}

为所有的bean类加上注解,配置文件只保留:<context:component-scan base-package="com.how2java.pojo"/>
@Component注解  表明此类是bean
package com.how2java.pojo;
 
import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component("p")
public class Product {
 
    private int id;
    private String name="product 1";
     
    @Autowired
    private Category category;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Category getCategory() {
        return category;
    }
     
    public void setCategory(Category category) {
        this.category = category;
    }
}


AOP 即 Aspect Oriented Program 面向切面编程 

首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 
所谓的周边功能,比如性能统计,日志,事务管理等等 

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP

  1. 准备业务类:
    public class ProductService {
         
        public void doSomeService(){
             
            System.out.println("doSomeService");
             
        }
         
    }
  2. 准备日志切面
    public class LoggerAspect {
     
        public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("start log:" + joinPoint.getSignature().getName());
            Object object = joinPoint.proceed();
            System.out.println("end log:" + joinPoint.getSignature().getName());
            return object;
        }
    }
  3. applicationContext.xml
    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
        <bean name="c" class="com.how2java.pojo.Category">
            <property name="name" value="yyy" />
        </bean>
     
        <bean name="p" class="com.how2java.pojo.Product">
            <property name="name" value="product1" />
            <property name="category" ref="c" />
        </bean>
         
        <bean name="s" class="com.how2java.service.ProductService">
        </bean>   
         
        <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
         
        <aop:config>
            <aop:pointcut id="loggerCutpoint"
                expression=
                "execution(* com.how2java.service.ProductService.*(..)) "/>
                 
            <aop:aspect id="logAspect" ref="loggerAspect">
                <aop:around pointcut-ref="loggerCutpoint" method="log"/>
            </aop:aspect>
        </aop:config>    
      
    </beans>


注解方式配置

注解配置业务类:
@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
     
}

注解配置切面
@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
 
@Aspect
@Component
public class LoggerAspect {
     
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}
配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    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
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <context:component-scan base-package="com.how2java.aspect"/>
    <context:component-scan base-package="com.how2java.service"/>
    <aop:aspectj-autoproxy/> 
   
</beans>
测试类:
1. @RunWith(SpringJUnit4ClassRunner.class) 
表示这是一个Spring的测试类

2. @ContextConfiguration("classpath:applicationContext.xml")
定位Spring的配置文件

3. @Autowired
给这个测试类装配Category对象

4. @Test
测试逻辑,打印c对象的名称
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category c;
 
    @Test
    public void test(){
        System.out.println(c.getName());
    }
}