bean基于xml文件

bean管理指的是两件事:

1.spring创建对象

2.属性注入 给对象中的属性赋值

1.bean创建对象:

无参构造函数

xml文件:

<bean  id ="user" class="com.atuguigu.spring5.User"></bean>

属性值:id是唯一的标识 ,也别的也可以 ,class是对象所在的全路径 包名.类名

创建对象的时候,默认执行的是无参的构造函数,所以需要类中要有无参构造函数,否则会报错

有参构造函数

java如果定义了一个有参构造函数,则不会自动生成无参的构造函数,和c++一样,这时如果不想重新写无参构造函数,就想使用有参的构造函数,应该这样写

<bean id="user1" class="com.testdemo.User">
    <!--  index代表 第几个属性 -->
    <constructor-arg index="0" value="10"/>
    <constructor-arg  index="1" value="name1"/>

</bean>

test类中生成user对象

这是通过绝对路径读取xml文件

        ApplicationContext context =new FileSystemXmlApplicationContext("d:\\IdeaProjects\\untitled\\test_user.xml");

通过相对路径读取xml文件 

UserDao userDao=  context.getBean(UserDaoMysqlimpl.class);  
通过类型获取,匹配class为usrdaomysqlimpl的类
UserDao userDao2= (UserDao) context.getBean("userdao1");
通过id获取,获取id为userdao1的类

 User user =(User) applicationContext.getBean("user1",User.class);  通过id和class获取

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("demo1.xml");
        //第一个参数是xml文件中该类对应的bean的id的值
        User user =(User) applicationContext.getBean("user1",User.class);
        System.out.println(user.toString());
    }

}

有三种属性注入的方法:

1)set方法

1……在 user类中创建属性,并生成对应的set方法


public class User {
    public User(int aa) {
        this.aa = aa;
    }
    public User() {
    }
    public void setAa(int aa) {
        this.aa = aa;
    }

    public int aa;
    public static void add1()
    {
        System.out.println("nihao shijie ");
    }
}

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">
<!--配置user对象创建-->
    <bean  id ="user" class="com.atuguigu.spring5.User">
        <property name="aa" value="aavalue"></property>
    </bean>
</beans>

2)简化的set p名称空间注入


实际用的不多

1......在xml文件中进行属性注入

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/

2.....在bean中设置属性

<bean  name="student-config" class="com.atuguigu.spring5.testdemo.Student" p:id="20" p:loc="loc2" p:name="name2">
<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean  name="student-config" class="com.atuguigu.spring5.testdemo.Student" p:id="20" p:loc="loc2" p:name="name2">
</bean>
</beans>

3)通过xml文件设置属性的空值和特殊值 

<property name="id"> <null></null></property>
<property name="loc" > <value> <![CDATA[<北京>]]></value></property>

<北京> 是要放的特殊值   其他的是固定格式 也这样写就行了

4)通过xml方式注入集合类型属性

1.注入数组类型属性

2.注入list 集合类型

3.注入map 集合类型

4.注入 set集合类型

5.注入 list<自定义类>  集合类型

<?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="stu" class="com.testdemo.Student">
        <property name="courses">
            <array>
                <value>"java_array"</value>
                <value>"python_array"</value>
                <value>"c++_array"</value>
            </array>
        </property>

        <property name="list">
            <list>
                <value>"java_list"</value>
                <value>"python_list"</value>
                <value>"c++_list"</value>
            </list>
        </property>

        <property name="map">
            <map>
                <entry key="JAVA_map" value="java_map"></entry>
                <entry key="PYTHON_map" value="python_map"></entry>
                <entry key="c++_map" value="c++_map"></entry>
            </map>
        </property>

        <property name="set">
            <set>
                <value>"set java"</value>
                <value>"set python"</value>
                <value>"set c++"</value>
            </set>
        </property>
        <property name="lessons" >
            <list>
                <ref bean="lesson1"></ref>
                <ref bean="lesson2"></ref>
            </list>
        </property>
    </bean>
    <bean id="lesson1" class="com.testdemo.Lession">
        <property name="name" value="java"></property>
    </bean>
    <bean id="lesson2" class="com.testdemo.Lession">
        <property name="name" value="python"></property>
    </bean>
</beans>

外部bean

一个service类中要调用dao类,可以把dao类看做是service类的一个属性,当做属性来 声明 和 注入

public class Studentservice {
    Studentdao studentdao =new Studentdaoimpl();
//set方法是为了在配置文件中生成service类时可以给这个属性赋值
        public void setBooks(Books books2) {
        this.books=books2;
    }
        return studentdao;
    }

 外部类指的是我在service类声明的外面 , 声明dao 类 ,然后在service类中引用dao类

<?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="dao" class="com.testdemo.dao.Studentdaoimpl"></bean>

    <bean id="service" class="com.testdemo.service.Studentservice">
       <!-- <property name="studentdao" ref="dao" ></property>-->
         <!-- 将上面的dao对象传给service中的属性-->
         <!--因为参数是一个类,所以要用ref-->

这种写法是一样的,暂时未发现不同
   <property name="money1">
            <ref bean="money2" ></ref>
        </property>
    </bean>

</beans>
   public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("demo1.xml");
         //第一个参数是xml文件中该类对应的bean 的 id值
        Studentservice studentservice =(Studentservice) applicationContext.getBean("service" ,Studentservice.class);
        studentservice.print2();
    }

 内部bean

在外部类里面声明内部的类 ,(可能没说清楚,看下面代码) ,

student里面声明department类, department里面有个属性 d_id,也给这个属性赋值

    <bean id="student" class="com.testdemo2.Student">
     <property name="department">
         <bean id="department2" class="com.testdemo2.Department">
             <property name="d_id" value="10"></property>
         </bean>
     </property>
    </bean>

 student类

package com.testdemo2;

public class Student {
    public int id;
    public Department department;//这里就表示一对一的关系,一个学生要对应一个部门
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

department类 

package com.testdemo2;

public class Department {
    public int d_id;

    public int getD_id() {
        return d_id;
    }

    public void setD_id(int d_id) {
        this.d_id = d_id;
    }
}

 级联赋值

在配置外部bean之后,可以name="department.d_id"  来直接配置 department对象中的d_id属性,注意department对象中一定要写 d_id的get方法

(在声明department对象的时候就可以给d_id属性赋值(比如下面,赋值d_id为10),为什么要用级联赋值呢)

<?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="student" class="com.testdemo2.Student">
     <property name="id" value="10"></property>
     <property name="department" ref="dept"></property>
        <property name="department.d_id" value="100"/>
    </bean>
<bean id="dept" class="com.testdemo2.Department">
    <property name="d_id" value="10"/>
</bean>
</beans>

通过下面的方式 ,可以让不同的类共享同一个list集合(注意,开头的引用中增加了东西)

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

    <util:list id="booklist">
        <value> java从入门到精通</value>
        <value> python从入门到精通</value>
        <value> php从入门到精通</value>
    </util:list>


    <bean class="com.testdemo.Lession" id="lession">
    <property name="name" value="bookname"></property>
    <property name="books" ref="booklist"></property>
    </bean>

    <bean class="com.testdemo.Books" id="books">
      <property name="books" ref="booklist"></property>
    </bean>
</beans>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值