Spring第二天

第一种

 <bean id="date" class="java.util.Date"></bean>

    <bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
       <property name="title" value="P8"></property>
        <property name="date" ref="date">

        </property>

    </bean>

第二种

<bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
       <property name="title" value="P8"></property>
        <property name="date" >
            <bean class="java.util.Date"></bean>
        </property>

    </bean>

自动装配autowire

<bean id="rvrt" class="com.qfedu.ioc.bean.Clazz">
           <property name="number" value="3"></property>
       </bean>

        <bean id="clazzyfyufuf" class="com.qfedu.ioc.bean.Clazz">
            <property name="number" value="6"></property>
        </bean>
       <bean id="student" class="com.qfedu.ioc.bean.Student" autowire="byName" >
            <property name="stuNum" value="1"/>
              <property name="stuName" value="ygl"/>
              <property name="stuAge" value="21"/>
            <property name="enterenceTime" ref="date"></property>
      </bean>
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object tianfen =  context.getBean("student");
        System.out.println(tianfen);

    }
}

在这里插入图片描述
spring注解配置
修改xml如下

<beans xmlns="http://www.springframework.org/schema/beans"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<?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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.qfedu.ioc.bean"></context:component-scan>
<!--
       <bean id="student" class="com.qfedu.ioc.bean.Student">
            <property name="stuNum" value="1"/>
              <property name="stuName" value="ygl"/>
              <property name="stuAge" value="21"/>
            <property name="enterenceTime" ref="date"></property>
      </bean>
      -->
       <bean id="date" class="java.util.Date"></bean>

    <bean id="tianfen" class="com.qfedu.ioc.bean.Tianfen" autowire="byType" >
       <property name="title" value="P8"></property>
        <property name="date" >
            <bean class="java.util.Date"></bean>
        </property>

    </bean>


</beans>

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
@Scope
在这里插入图片描述
在这里插入图片描述
加上@Scope(“prototype”),将其设置为多例模式
在这里插入图片描述
在这里插入图片描述
@Lazy(false) 类注解,用于声明一个单例模式的Bean是否为懒汉模式,默认为饿汉式
@Lazy(true)表示声明为懒汉模式
在这里插入图片描述
测试类

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
        System.out.println("------------------------------------");
        Object student = classPathXmlApplicationContext.getBean("student");
        System.out.println(student);
    }
}

Student类

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy(false)
public class Student {
    private int age=111;
    private String name="e";
    private int dengji=9;

    public Student(){
        System.out.println("创建");
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public int getDengji() {
        return dengji;
    }

    public void setDengji(int dengji) {
        this.dengji = dengji;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", dengji=" + dengji +
                '}';
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值