十7、Spring框架(IOC/DI)

1、Spring框架

Spring是一個基於IOC和AOP的結構J2EE系統的框架。

一、IOC反轉控制是Spring的基礎(Inversion Of Control)。也就是說建立對象由之前的程序員本身new構造方法來調用,變成了交給Spring建立對象。

二、DI依賴注入(Dependency Inject):就是拿到的對象的屬性,已經被注入好相關值了,能夠直接使用。

三、將lib.jar導入到新建的Spring項目的lib目錄(類庫)中。

導入jar包方法:右鍵project->properties->java build path->libaries->add external jars就行。java

四、在src目錄下,創建兩個文件夾,一個pojo(放實體類),一個test(測試用)

準備pojo Product,用來測試IOC和DI程序員

package com.demo.pojo;
public class Product{
    private int id;
    private String name;
    //屬性的getter和setter方法
    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;
    }
}
View Code

五、在src目錄下新建一個applicationContext.xml文件

  applicationContext.xml是Spring的核心配置文件,經過配置bean的屬性,根據關鍵字product來獲取Product對象,該對象獲取的時候,被注入了字符串"Apple"到屬性中。spring

<?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="product" class="com.demo.pojo.Product">
        <property name="name" value="Apple"/>
    </bean>                    
</beans>

六、測試TestSpring

在test文件夾下,創建TestSpringapp

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;

import com.demo.pojo.Product;

public class TestSpring{
    public static void main(String[] args){
    //加載applicationContext.xml配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        //根據ApplicationContext對象提供的getBean()方法,獲取配置bean的name屬性值。
        Product product=(Product)context.getBean("product");
        System.out.println(product.getName());
    }
}
View Code

七、工做原理:

IOC方式獲取對象方式:對象的生命週期交給Spring來管理,直接從Spring那裏獲取一個對象。框架

傳統方式:經過new關鍵字主動建立一個對象。ide

2、注入對象

以前直接對Product的name屬性注入"Apple"字符串,這一次咱們注入一個Category分類對象。測試

一、首先修改Product.java實體類,新增一個Category屬性。

package com.demo.pojo;
public class Product{
    private int id;
    private String name;
    private Category category;//類別屬性
    //屬性的getter和setter方法
    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;
    }
    pulic vodi setCategory(Category category){
        this.category=category;
    }
}
View Code

二、修改applicationContext.xml配置文件

在建立Product的時候注入一個Category對象,這裏要使用ref來注入另外一個對象。ui

<?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="product" class="com.demo.pojo.Product">
        <property name="name" value="Apple"/>
        <property name="category" ref="Category"/><!--將category對象注入到product-->
    </bean>    
    <!--配置Category的bean-->    
    <bean name="category" class="com.demo.pojo.Category">
        <property name="name" value="Fruit"/>
    </bean>            
</beans>

三、TestSpring測試

經過Spring獲取Product對象被注入的Category對象this

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;

import com.demo.pojo.Product;

public class TestSpring{
    public static void main(String[] args){
    //加載applicationContext.xml配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        //根據ApplicationContext對象提供的getBean()方法,獲取配置bean的name屬性值。
        Product product=(Product)context.getBean("product");
        System.out.println(product.getName());//Apple
        System.out.pringln(product.getCategory().getName());//Fruit
    }
}
View Code

3、採用註解的方式進行IOC/DI

一、修改applicationContext.xml

添加配置:spa

<context:annotation-config/>

具體配置修改以下:

<?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="product" class="com.demo.pojo.Product">
        <property name="name" value="Apple"/>
        <!--<property name="category" ref="Category"/><!--將category對象注入到product--><!--註釋掉剛纔給Product注入的category屬性,後面採用註解完成該操做-->
    </bean>    
    <!--配置Category的bean-->    
    <bean name="Category" class="com.demo.pojo.Category">
        <property name="name" value="Fruit"/>
    </bean>    

二、採用註解@Autowired

2.一、在Product.java的Category屬性上面加上@Autowired註解

package com.demo.pojo;
public class Product{
    private int id;
    private String name;
    @Autowried//採用註解注入Category屬性,不用在xml配置bean的Product引入Category
    private Category category;//類別屬性
    //屬性的getter和setter方法
    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;
    }
    pulic vodi setCategory(Category category){
        this.category=category;
    }
}
View Code

2.二、也能夠在setCategory方法上面加上@Autowired註解,達到一樣效果

package com.demo.pojo;
public class Product{
    private int id;
    private String name;
    //@Autowried//採用註解注入Category屬性,不用在xml配置bean的Product引入Category
    private Category category;//類別屬性
    //屬性的getter和setter方法
    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;
    }
    @Autowried
    pulic void setCategory(Category category){
        this.category=category;
    }
}
View Code

2.三、還能夠採用@Resource註解

package com.demo.pojo;
import javax.annotation.Resource;
public class Product{
    private int id;
    private String name;
    //@Autowried//採用註解注入Category屬性,不用在xml配置bean的Product引入Category
    @Resource(name="category")
    private Category category;//類別屬性
    //屬性的getter和setter方法
    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;
    }
    //@Autowried
    pulic void setCategory(Category category){
        this.category=category;
    }
}
View Code

三、TestSpring測試

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;

import com.demo.pojo.Product;

public class TestSpring{
    public static void main(String[] args){
    //加載applicationContext.xml配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        //根據ApplicationContext對象提供的getBean()方法,獲取配置bean的name屬性值。
        Product product=(Product)context.getBean("product");
        System.out.println(product.getName());//Apple
        System.out.pringln(product.getCategory().getName());//Fruit
    }
}
View Code

四、也能夠不用再applicationContext.xml中一個一個配置bean。

配置bean屬性的都去掉,直接加上:

<context:component-scan base-package="com.demo.pojo"/>

做用:告訴Spring,bean都放在com.demo.pojo包下

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">
    
    <context:component-scan base-package="com.demo.pojo"/>
    
</beans>

4.一、使用@Component註解,爲pojo加上註解,說明這個類是bean

Product.java:

package com.demo.pojo;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("product")
public class Product{
    private int id;
    private String name="Apple";
    //@Autowried//採用註解注入Category屬性,不用在xml配置bean的Product引入Category
    @Resource(name="category")
    private Category category;//類別屬性
    //屬性的getter和setter方法
    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;
    }
    //@Autowried
    pulic void setCategory(Category category){
        this.category=category;
    }
}
View Code

Category.java

package com.demo.pojo;

import org.springframework.stereotype.Component;

@Component("category")
public class Category {
 
    private int id;
    private String name="Fruit";
    
    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;
    }
}
View Code

4.二、TestSpring測試

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;

import com.demo.pojo.Product;

public class TestSpring{
    public static void main(String[] args){
    //加載applicationContext.xml配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        //根據ApplicationContext對象提供的getBean()方法,獲取配置bean的name屬性值。
        Product product=(Product)context.getBean("product");
        System.out.println(product.getName());//Apple
        System.out.pringln(product.getCategory().getName());//Fruit
    }
}
View Code
相關文章
相關標籤/搜索