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构造注入的更多相关文章

  1. spring 构造注入 异常 Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments

    你可能在做项目的时候,需要在项目启动时初始化一个自定义的类,这个类中包含着一个有参的构造方法,这个构造方法中需要传入一些参数. spring提供的这个功能叫“构造注入”, applicationCon ...

  2. 7.28.1 Spring构造注入还是设置注入

    1. 构造方法注入代码如下:public UserManagerImpl(UserDao userDao) {                                              ...

  3. Spring 构造注入 传參数

    1.        提供相应的构造方法 //构造器注入 public class Bean6 { private String name; private Integer age; // 服务于构造器 ...

  4. Spring 设值注入 构造注入 p命名空间注入

    注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...

  5. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  6. Spring接口编程_设值注入和构造注入

    说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...

  7. 【Spring学习笔记-2.1】Spring的设值注入和构造注入

    设值注入: 先通过无参数的构造函数创建一个Bean实例,然后调用对应的setter方法注入依赖关系: 配置文件: <?xml version="1.0" encoding=& ...

  8. 【Spring实战】—— 2 构造注入

    本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法. 如果想要使用spring来实现依赖注入,需要几个重要的步骤: 1 定义主要的类和需要分 ...

  9. Spring常用的jar+普通构造注入

    常用工具 jar 说明 提供AOP(面向切面编程)实现:spring -aop spring提供对AspectJ框架的整合:spring-aspects 提供 IoC(控制反转)的基础实现:sprin ...

随机推荐

  1. js拼的onclick调用方法需要注意的地方 之一

    1.首先,明确一点,js方法中参数可以传递字符串,对象,number类型等,对象传递的是引用,方法中修改了,会影响到方法外面的对象. 2.下面重现项目中遇到的一个问题:(其实就是要明白通过引号来拼接字 ...

  2. rsyn文件传输

    Rsync的命令格式可以为以下六种: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]. ...

  3. PopupWindow 防微信弹出右 侧窗体(继承PopupWindow )

    1, pop自定义 public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn ...

  4. JDBC编程之数据更新

    -------------------siwuxie095                             JDBC 编程之数据更新             首先下载 MySQL 的 JDBC ...

  5. EPEL for CentOS or Redhat

    注:地址可能会变 RHEL/CentOS 7 64 Bit # wget http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release ...

  6. Frequent Pattern 挖掘之二(FP Growth算法)

    Frequent Pattern 挖掘之二(FP Growth算法) FP树构造 FP Growth算法利用了巧妙的数据结构,大大降低了Aproir挖掘算法的代价,他不需要不断得生成候选项目队列和不断 ...

  7. js或jQuery中 邮箱跳转的问题,跳转到指定邮箱(通过layui的ifram实现)

    对刚做的东西记个笔记 如果遇到同样问题解决起来又问题的欢迎留言 var emailtext = $("#TextBoxEmail").val();//获得要截取的值 var arr ...

  8. 远程桌面连接KVM虚拟机

    问题描述 有些时候,由于网络存在问题,虚拟机无法获取到IP地址,无法使用spice或vnc来连接虚拟机,但是又需要连到虚拟机来排查故障 解决办法 编辑虚拟机配置 设置xml命名空间 <domai ...

  9. LOJ6053 简单的函数(min_25筛)

    题目链接:LOJ 题目大意:从前有个积性函数 $f$ 满足 $f(1)=1,f(p^k)=p\oplus k$.(异或)求其前 $n$ 项的和对 $10^9+7$ 取模的值. $1\le n\le 1 ...

  10. webpack@3.6.0(2) -- css及图片相关问题

    本篇内容 css3前缀处理postcss 消除未使用的css部分 图片处理 css分离和分离后的图片处理 css3前缀处理postcss cnpm i -D postcss-loader autopr ...