Spring笔记

 

IOC注入的几种常用方式:

set注入:

使用set注入首先要求需要创建的实例对象有set方法,并且最好要有一个无参的构造函数

需要创建的类实例对象的:

package com.st.spring;

public class User {
    private String name;
    private int age;
    public User()
    {

    }
    public User(String name,int age)
    {
        this.name=name;
        this.age=age;
    }

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

    public void setAge(int age) {
        this.age = age;
    }
    public void print()
    {
        System.out.println(name+":::"+age);
    }
}

首先需要创建一个xml配置文件,我将它起名为bean.xml

代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.st.spring.User">
        <property name="name" value="海洋"></property>
        <property name="age" value="24"></property>
    </bean>
</beans>

我们可以通过ApplicationContext来创建这个实体类,也可以通过BeanFactory来创建这个实体类

如果需要将name属性设为null值,只需要写成这种格式即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.st.spring.User">
        <property name="name">
        <null/>
        </property>
        <property name="age" value="24"></property>
    </bean>
    <bean id="book" class="com.st.spring.Book">

        <constructor-arg value="孟在远方"/>
    </bean>
</beans>

如果需要设置特殊字符,则需要加上<![CDATA[]]>,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.st.spring.User">
        <property name="name">
            <value><![CDATA[<wewewe>]]></value>
        </property>
        <property name="age" value="24"></property>
    </bean>
    <bean id="book" class="com.st.spring.Book">

        <constructor-arg value="孟在远方"/>
    </bean>
</beans>

可以新建一个测试类,命名为test.class

代码如下:

package weTest;
import com.st.spring.User;
import jdk.jfr.StackTrace;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void Test()
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        BeanFactory context1=new ClassPathXmlApplicationContext("bean.xml");
        User user=context.getBean("user",User.class);
        User user1=context1.getBean("user",User.class);
        user.print();
        user1.print();
    }
}

ApplicationContext与BeanFactory的主要区别在于,BeanFactory在启动的时候不会去实例化Bean,只有从容器中拿Bean的时候才会去实例化,而ApplicationContext在启动的时候就把所有的Bean全部实例化了。它还可以为Bean配置lazy-init=true来让Bean延迟实例化

web项目最好用ApplicationContext

输出结果如下:

构造器注入:

我们新建一个Book类,代码如下:

package com.st.spring;

public class Book {
    private String bname;
    public Book(String bname)
    {
        this.bname=bname;
    }
    public void print()
    {
        System.out.println(bname);
    }
}

为了方便,直接在bean.xml配置文件里加一段<bean name="book" class="com.st.spring.Book"></bean>就行了,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.st.spring.User">
        <property name="name" value="海洋"></property>
        <property name="age" value="24"></property>
    </bean>
    <bean id="book" class="com.st.spring.Book">

        <constructor-arg value="孟在远方"/>
    </bean>
</beans>

在test类里用之前的context直接创建一个Book的实例对象

package weTest;
import com.st.spring.Book;
import com.st.spring.User;
import jdk.jfr.StackTrace;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void Test()
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        BeanFactory context1=new ClassPathXmlApplicationContext("bean.xml");
        User user=context.getBean("user",User.class);
        User user1=context1.getBean("user",User.class);
        user.print();
        user1.print();
        Book book=context.getBean("book",Book.class);
        book.print();
    }
}

输出结果如下:

 

IOC注入的底层原理:xml解析+工厂模式+反射

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值