`IOC`操作`Bean`管理(`xml`注入其他类型属性)【重点】

1.字面量值

(1)null值
	解决方案:使用`null`标签

(2)属性值包含特殊符号
	解决方案:
		1.使用转义字符【可以使用特殊字符转义符号代替】
		2.使用`CDATA`区,在`CDATA`区中的数据将原不封不动显示

演示:

1.定义类

package com.haikang.spring.di.dao;

/**
 * @Author 海康
 * @Version 1.0
 */
public class UserDao {
    private String name;
    private String author;
    private String address;

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void add(){
        System.out.println("userDao add方法");
    }

    @Override
    public String toString() {
        return "UserDao{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

2.定义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="userDao" class="com.haikang.spring.di.dao.UserDao">
        <!--1.空值方式-->
        <property name="name">
            <null/>
        </property>
    <!--
        特殊字符:
            1.转义方式
            2.`CDATA`区方式
    -->
        <property name="author" value="`&lt;&lt;海康 &gt;&gt;"/>
        <property name="address" >
            <value><![CDATA[湛江]]></value>
        </property>
    </bean>
</beans>

test

@Test
    public void test1(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        UserDao userDao = context.getBean("userDao", UserDao.class);
        System.out.println(userDao.toString());
        //返回值: UserDao{name='null', author='`<<海康 >>', address='<<湛江>>'}
    }

2.注入属性-外部Bean【就是注入对象形式】

在一个类中,需要使用到另一个类中的方法和属性,就需要注入该类的对象

案例

1.创建两个类`Servlet`类和`Dao`类
2.在`Servlet`类中调用`Dao`里面的方法
	在`Servlet`类中添加`Dao`属性,并提供`setter`方法或`有参构造器`【演示是使用`setter方法`】

3.在Spring配置文件中进行配置

4.·bean·使用子标签是
		property: 表示属性名
		ref: 表示引入对象属性名

步骤
第一步:定义UserDao类和UserService

package com.haikang.spring.di.dao;

/**
 * @Author 海康
 * @Version 1.0
 */
public class UserDao {
    private String name;
    private String author;
    private String address;
	
	并且提供`setter`方法和`getter`方法及`toString`方法
public class UserService {
    // 由于在UserService层使用到UserDao类方法和属性
    // 所以定义UserDao属性
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void service(){
        userDao.add();
        System.out.println("UserService中的service方法。。。");
    }
}

2.创建xml文件

<!--注入外部bean方式-->
    <!--注入UserDao对象-->
    <bean id="userDao" class="com.haikang.spring.di.dao.UserDao">
        <property name="name" value="海康"/>
        <property name="author" value="湛江"/>
        <property name="address" value="西安"/>
    </bean>
    <!--注入UserService对象-->
    <bean id="userService" class="com.haikang.spring.di.service.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>

test

@Test
    public void test2(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.service();
        /**
         * 返回值
         * userDao add方法
         * UserService中的service方法。。。
         */
    }

3.注入集合类型

注入普通属性和对象属性

  1. 数组类型
  2. List集合类型
  3. Set集合类型
  4. Properties集合
  5. Map集合类型【需要使用到map标签中的entry标签,在entry中属性keyvalue , ref属性】

定义类

public class Author {
    private String name;
	并提供setter方法和getter方法及toString方法
}


public class Book {
    // 数组
    private String[] arr;
    // List集合
    private List<String> list;
    // Set集合
    private Set<Integer> sets;
    // Properties集合
    private Properties properties;
    // Map集合
    Map<String,Author> maps;
    
	并提供setter方法和getter方法及toString方法
}

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="book" class="com.haikang.spring.di.pojo.Book">
        <!--数组类型-->
        <property name="arr" >
            <array>
                <value>海康</value>
                <value>湛江</value>
            </array>
        </property>
        <!--List集合-->
        <property name="list">
            <list>
                <value>A_1</value>
                <value>B-1</value>
            </list>
        </property>
        <!--Set集合-->
        <property name="sets">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>
        <!--Properties集合-->
        <property name="properties">
            <props>
                <prop key="电话">10086</prop>
                <prop key="电话">11001</prop>
            </props>
        </property>
        <!--Map集合-->
        <property name="maps">
            <map>
                <entry key="作者1" value-ref="author1"/>
                <entry key="作者2" value-ref="author2"/>
            </map>
        </property>
    </bean>

    <!--注入Author对象-->
    <bean id="author1" class="com.haikang.spring.di.pojo.Author">
        <property name="name" value="月光"/>
    </bean>
    <!--注入Author对象-->
    <bean id="author2" class="com.haikang.spring.di.pojo.Author">
        <property name="name" value="明天"/>
    </bean>
</beans>

test

@Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("collection.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
        /**
         * 返回值:
         * Book{
         * arr=[海康, 湛江]
         * list=[A_1, B-1]
         * sets=[1, 2]
         * properties={电话=11001}
         * maps={作者1=Author{name='月光'}, 作者2=Author{name='明天'}}
         * }
         */
    }

4.IOC两种类型bean

  1. 普通bean
  2. 工厂beanFactoryBean
普通Bean

在配置文件中定义bean类型就是返回类型

如:在bean标签中定义是:Student类型
<bean id="student" class="com.haikang.collection.Student">...</bean>

在获取Bean对象时,返回就是Student类型
  Student student = context.getBean("student", Student.class);

普通bean就是定义的类型和返回类型一致

工厂beanFactoryBean

实现工厂Bean步骤:

  1. 创建类,让该类作为工厂bean,需要实现接口FactoryBean
  2. 实现接口重写方法,在实现的方法中定义返回的bean类型

定义类实现工厂FactoryBean接口

public class MyBean implements FactoryBean<User> {
    // gerObject就是定义返回Bean类型
    @Override
    public User getObject() throws Exception {
        //简化,其实需要读取和解析xml文件,但是演示做了简化
        User user = new User();
        return user;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

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="myBean" class="com.haikang.factorybean.MyBean"/>
</beans>

test

@Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("factoryBean1.xml");
        User user = context.getBean("myBean", User.class);
        System.out.println(user);
        // 返回是User类型
        //com.haikang.pojo.User@262b2c86
    }

可以看出,Bean标签中定义的类型和返回的类型可以不一致,这就是BeanFactory的特点,可以一致,也可以不一致这【如果需要不一致,需要在getObject方法中实现】

5. IOCBean作用域

1.在Spring中,可以设置创建bean实例是单实例还是多实例

2.在默认情况下Bean是单实例对象

如何设置单实例还是多实例

(1)在Spring配置文件bean标签中有scope属性,用于设置单实例还是多实例

(2)scope属性值

  • singleton:表示是单实例对象
  • prototype:表示是多实例对象

重点[面试]:singletonprotoype的区别:

  1. 设置scope值是singleton时,加载Spring配置文件时就会创建单实例对象
  2. 设置scope值是protoype时,不是加载spring配置文件时创建对象,而在调用getBean方法时才创建对象
public class Person {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

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的作用域:
        默认情况下:singleton 属性,表示是单实例的 scope="singleton"
        prototype 属性: 表示是多实例
-->
    <bean id="person" class="com.haikang.bean.Person" scope="prototype">
        <property name="name" value="海康"/>
        <property name="age" value="21"/>
    </bean>
</beans>

test

@Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("personBean.xml");
        Person person1 = context.getBean("person", Person.class);
        Person person2 = context.getBean("person", Person.class);
        // 在单实例情况下返回true,在多实例情况下返回false
        System.out.println(person1==person2);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值