• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
终天
博客园    首页    新随笔    联系   管理    订阅  订阅

Spring-注入

一.给属性注入值(四种)

1.setter方法注入(就是在bean节点下有property节点给里面的属性值赋值

<bean id="service" class="demo04.service.HelloServiceImpl">
        <property name="dao" ref="dao"></property>
    </bean>

2.构造注入

3.p命名空间注入(使用前要先要在Spring配置文件中引入p命名空间xmlns:p="http://www.springframework.org/schema/p")这里的两种我写在一起了

首先写一个学生类,车类

复制代码
package demo05;

/**
 * Created by mycom on 2018/3/5.
 */
public class Student {
    private String name;
    private Integer age;
    private Car car;

    public Student() {
    }

    public Student(String name, Integer age) {

        this.name = name;
        this.age = age;
    }

    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;
    }
}
复制代码
复制代码
package demo05;

/**
 * Created by mycom on 2018/3/5.
 */
public class Car {
    private String color;
    private String brand;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {

        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
复制代码

在xml中的配置

复制代码
<!--构造注入-->
    <!--<bean id="stu" class="demo05.Student">
        <constructor-arg index="0" value="小明"></constructor-arg>
        <constructor-arg index="1" value="12"></constructor-arg>
    </bean>-->
    <!--p命名空间注入-->
    <bean id="stu" class="demo05.Student" p:name="小明" p:age="12" p:car-ref="car"></bean>
复制代码

最后编写测试类

以p命名空间注入为例进行测试

复制代码
import demo04.service.HelloServiceImpl;
import demo04.service.IHelloService;
import demo05.MyCollection;
import demo05.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by mycom on 2018/3/3.
 */
public class Test20180305 {
    @Test
    public void t1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextCon.xml");
Student service2 =(Student) context.getBean("stu");
System.out.println(service2.getName());
 } }
复制代码

4.集合注入

再写一个MyCollection类

复制代码
package demo05;


import java.util.*;

/**
 * Created by mycom on 2018/3/5.
 */
public class MyCollection {
    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

    public MyCollection() {
        System.out.println("创建对象===========");
    }

    public MyCollection(String[] array, List<String> list, Set<String> set, Map<String, String> map, Properties properties) {
        this.array = array;
        this.list = list;
        this.set = set;
        this.map = map;
        this.properties = properties;
    }

   @Override
    public String toString() {
        return "MyCollection{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

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

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

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

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

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

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
复制代码
复制代码
<!--集合注入-->
    <bean id="collection" class="demo05.MyCollection" scope="prototype">
        <!--Array-->
        <property name="array">
            <array>
                <value>小明</value>
                <value>小兰</value>
            </array>
        </property>
        <!--list-->
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
            </list>
        </property>
        <!--set-->
        <property name="set">
            <set>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>
        <!--map-->
        <property name="map">
            <map>
                <entry key="1">
                    <value>01</value>
                </entry>
                <entry key="2">
                    <value>02</value>
                </entry>
            </map>
        </property>
        <!--propreties-->
        <property name="properties">
            <props>
                <prop key="001">001</prop>
                <prop key="002">002</prop>
            </props>
        </property>
    </bean>
复制代码
复制代码
import demo04.service.HelloServiceImpl;
import demo04.service.IHelloService;
import demo05.MyCollection;
import demo05.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by mycom on 2018/3/3.
 */
public class Test20180305 {
    @Test
    public void t1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextCon.xml");
       //返回的类型只能是接口
        MyCollection service =(MyCollection) context.getBean("collection");
        System.out.println(service);
       }


}
复制代码
posted @ 2018-03-06 09:20  一场梦一首歌一个人  阅读(109)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3