写在之前,作为正在学习的程序员,对于Spring理解比较差,给两个简单的定义大家看一下。

控制反转(Inversion of Control),是一个重要的面向对象编程法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心beans

 

DI依赖注入:说的是创建对象实例时,为这个对象注入属性值其它对象实例,侧重于实现。

今天的博客主要写的是DI,所以我们看的是实现,而不是思想。

首相我们先来看最简单的set方法注入

我们准备一个实体类就可以做第一个例子,

 

publi class Users(){
   private String name;

    private Integer age;
}
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- <context:component-scan base-package="cn.curry"></context:component-scan>
-->
    <bean id="users" class="cn.curry.spring.Users">
        <property name="name" value="呵呵"></property>
    </bean>

上面是我们的部分配置文件。

最后我们看简单的测试

 @Test
    public void test01(){
        Resource resource=new ClassPathResource("applicationContext.xml");
        BeanFactory bf=new XmlBeanFactory(resource);
        Users users=(Users)bf.getBean("users");
        Users userss=(Users)bf.getBean("users");
        System.out.println(users==userss);
        System.out.println(users.getName());
    }

然后我们再看域属性注入和P命名空间注入

这时我们会添加第二个实体类,并且在Users类里加一个构造

public Users() {
        System.out.println("Object Init");
    }

    public Users(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

 

public class Car {private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

配置文件

<bean id="us2" class="cn.curry.spring.Users">
        <constructor-arg index="0" value="花花"></constructor-arg>
        <constructor-arg index="1" value="12"></constructor-arg>
        <constructor-arg index="2" ref="car"></constructor-arg>
    </bean>

    <bean id="usersP" class="cn.curry.spring.Users" p:name="花花" p:age="12" p:car-ref="car"></bean>

看测试

@Test
    public void test02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("us2");
        System.out.println(users.getCar().getColor()+" "+users.getName());

    }
    @Test
    public void test03(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usersP");
        System.out.println(users.getCar().getColor()+" "+users.getName());
    }

 

然后看集合属性注入

先修改Users,添加集合属性

    private List<Car> list;

    private Set<Car> set;

    private Map<Integer,Car> map;

在看配置文件

<bean id="uslist" class="cn.curry.spring.Users">
        <property name="list">
            <list>
                <ref bean="car"></ref>
            </list>
        </property>
    </bean>

    <bean id="usset" class="cn.curry.spring.Users">
        <property name="set">
            <set>
                <ref bean="car"></ref>
            </set>
        </property>
    </bean>
    <bean id="usmap" class="cn.curry.spring.Users">
        <property name="map">
            <map>
                <entry key="1" value-ref="car"></entry>
            </map>
        </property>
    </bean>

在看测试

@Test
    public void test04(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("uslist");
        List<Car> list = users.getList();
        for (Car item : list) {
            System.out.println(item.getColor());
        }
    }
    @Test
    public void test05(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usset");
        Set<Car> set = users.getSet();
        for (Car item : set) {
            System.out.println(item.getColor());
        }
    }
    @Test
    public void test06(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usmap");
        Map<Integer, Car> map = users.getMap();
        for (Integer item : map.keySet()) {
            System.out.println(map.get(item).getColor());
        }
    }

最后我们看一下注解这时配置只需一句话

<context:component-scan base-package="cn.curry"></context:component-scan>

然后看我们的实体类

package cn.curry.spring;

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

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Curry on 2017/3/25.
 */
@Component("usss")
public class Users {
    @Value("赫尔")
    private String name;
    @Value("18")
    private Integer age;
    //@Resource(name="car")
    @Autowired
    @Qualifier("car")
    private Car car;
    private List<Car> list;

    private Set<Car> set;

    private Map<Integer,Car> map;

    public Users() {
        System.out.println("Object Init");
    }

    public Users(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public List<Car> getList() {
        return list;
    }

    public void setList(List<Car> list) {
        this.list = list;
    }

    public Set<Car> getSet() {
        return set;
    }

    public void setSet(Set<Car> set) {
        this.set = set;
    }

    public Map<Integer, Car> getMap() {
        return map;
    }

    public void setMap(Map<Integer, Car> map) {
        this.map = map;
    }
}
package cn.curry.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * Created by Curry on 2017/3/25.
 */
@Component("car")
//@Service
//@Controller
//@Repository
public class Car {
    @Value("黄色")
    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

最后一个测试

package cn.curry.test;

import cn.curry.spring.Car;
import cn.curry.spring.Users;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Curry on 2017/3/25.
 */
public class MyTest {
    @Test
    public void test01(){
        Resource resource=new ClassPathResource("applicationContext.xml");
        BeanFactory bf=new XmlBeanFactory(resource);
        Users users=(Users)bf.getBean("users");
        Users userss=(Users)bf.getBean("users");
        System.out.println(users==userss);
        System.out.println(users.getName());
    }
    @Test
    public void test02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("us2");
        System.out.println(users.getCar().getColor()+" "+users.getName());

    }
    @Test
    public void test03(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usersP");
        System.out.println(users.getCar().getColor()+" "+users.getName());
    }
    @Test
    public void test04(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("uslist");
        List<Car> list = users.getList();
        for (Car item : list) {
            System.out.println(item.getColor());
        }
    }
    @Test
    public void test05(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usset");
        Set<Car> set = users.getSet();
        for (Car item : set) {
            System.out.println(item.getColor());
        }
    }
    @Test
    public void test06(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usmap");
        Map<Integer, Car> map = users.getMap();
        for (Integer item : map.keySet()) {
            System.out.println(map.get(item).getColor());
        }
    }
    @Test
    public void test07(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Users users=(Users) context.getBean("usss");
        System.out.println(users.getName()+"   "+users.getCar().getColor());
    }
}

 

posted on 2017-03-25 16:23  北漂-少年  阅读(202)  评论(0编辑  收藏  举报