Spring笔记

Spring就是一种单纯的管理代码的框架,和web编程没什么直接关系,作用:

一是可以在提前的声明一些类,然后如果在编程中用到这些类的对象,就可以直接通过相应的方式直接拿到该类的对象,而不用自己去new,这样做可以让Spring来管理所有的类,方便使用。提前声明这些类的方式有两种,一个是在相应的xml中用xml代码来声明,第二种是直接在java类中加上一些标识(注解方式),表示这个类是Spring来管理的,可以直接获取他的对象,而不用使用new的方法来获取。

第二个,面向切面编程,详见另外两篇AOP(面向切切面编程)的配置。

IOC和DI(xml方式)

IOC:控制反转,就是把创建对象的权利交给Spring。

DI:对象注入,在一个新的类中加入已有的对象(下面第二个bean中,Product对象中注入了一个已有的对象c,也就是Category类的对象)

TIPs:前提是所有的bean类都已经创建好了。

1 在src文件夹下建立applicationContext.xml文件,applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中。

<?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>
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />  <!-- 对象的注入 -->
    </bean>
 
</beans>

 

配置之后,就不用自己新建对象了,当想用某个对象的时候,直接获取即可:
通过 contex.getBean("c")就可以获得c代表的类的对象,即Category类的对象。

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.pojo.Category;
 
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());
    }
}

 IOC和DI(注解方式)

DI的注解方式:首先在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">
  
    <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>
View Code

同时,在Product类中加入注解@Autowired,标识Category的注入。也可以用 @Resource(name="c")注解,效果是一样的

package com.how2java.pojo;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Product {
 
    private int id;
    private String name;
    @Autowired      //这个注解也可以加在该属性的set方法上
    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;
    }
}

IOC的注解配置:
首先移除XML中的有声明,加上<context:component-scan base-package="com.how2java.pojo"/>其作用是告诉Spring,bean都放在com.how2java.pojo这个包下

 
<?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.pojo"/>
     
</beans>

然后在bean类中加入注解@component,表明该类交由Spring管理

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;
    }
}
Category类
package com.how2java.pojo;
 
import org.springframework.stereotype.Component;
 
@Component("c")
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="category 1";
}
Product类

调用方式和之前一样。

 

 

posted @ 2017-07-19 21:56  sucs  阅读(520)  评论(0编辑  收藏  举报