码迷,mamicode.com
首页 > 编程语言 > 详细

spring构造注入

时间:2017-08-20 10:10:32      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:表达   spring   结果   --   getbean   1.0   work   应用程序   span   

Sping 结构体系结构4个核心组件

Beans:Bean是包装我们应用程序自定义对象Object的bject存有数据。

Core: context在发现建立,维护Bean之间关系所需的一些工具。如资源的加载,资源的抽象等。

Context:context就是一个Bean关系的集合。

Expression : spring表达式语言。

 

构造注入:

前提的有构造

 技术分享

//学生类

public class Student {
    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + \‘+
                ", age=" + age +
                ", car=" + car +
                ‘}‘;
    }
    private  String name;
    private int age;
    //构造

 

 private Car car;
    public Student(String name, int 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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
}

//车类

public class Car {
    private  String brand;
    private  String color;

    @Override
    public String toString() {
        return "Car{" +
                "brand=‘" + brand + \‘+
                ", color=‘" + color + \‘+
                ‘}‘;
    }
    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;
    }
}

配置:

applicationContextspring03xmldl.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--01.dao-->   
<bean id="car" class="cn.happy.spring03xmldi.Car">
<property name="brand" value="保时捷"></property>
<property name="color" value="蓝色"></property></bean>
    <!-- 构造注入-->
 <bean id="stu" class="cn.happy.spring03xmldi.Student">
      <constructor-arg index="0" value="旺旺"></constructor-arg>
      <constructor-arg index="1" value="30"></constructor-arg>
      <constructor-arg index="2" ref="car"></constructor-arg></bean>
</beans>

测试类:

/构造注入
 public  void test01(){
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextspring03xmldl.xml");
 Student stu=(Student)ctx.getBean("stu");
System.out.println(stu);

结果:

 

P命名空间引入

xmlns:p="http://www.springframework.org/schema/p"

<!-- p命名空间注入 -->
<bean id="stu" class="cn.happy.spring03xmldi.Student" p:name="蛋蛋" p:age="18" p:car-ref="car"></bean>

技术分享

 

 

 

 

spring构造注入

标签:表达   spring   结果   --   getbean   1.0   work   应用程序   span   

原文地址:http://www.cnblogs.com/wangdan123/p/7398838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!