Spring基于xml自动装配

举报
牛哄哄的柯南 发表于 2021/05/26 15:11:58 2021/05/26
【摘要】 基于xml自动装配 手动装配自动装配 首先写两个类Dept类和Emp类。 Dept类: package com.Keafmd.spring5.autowire; /** * Keafmd * * @ClassName: Dept * @Description: 部门类 * @author: 牛哄哄的柯南 * @date: 2021-01...

基于xml自动装配


首先写两个类Dept类和Emp类。

Dept类:

package com.Keafmd.spring5.autowire;

/**
 * Keafmd
 *
 * @ClassName: Dept
 * @Description: 部门类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-16 13:43
 */
public class Dept { @Override public String toString() { return "Dept{}"; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Emp类:

package com.Keafmd.spring5.autowire;

/**
 * Keafmd
 *
 * @ClassName: Emp
 * @Description: 员工类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-16 13:42
 */
public class Emp { private Dept dept; // id和这里保持一致 public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp{" + "dept=" + dept + '}'; } public void test(){ System.out.println(dept); }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

手动装配

手动装配的bean5.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="emp" class="com.Keafmd.spring5.autowire.Emp"> <!--手动装配--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="com.Keafmd.spring5.autowire.Dept"></bean>

</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试代码:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.autowire.Emp;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 { @Test public void test5(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml"); Emp emp = context.getBean("emp",Emp.class); System.out.println(emp); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

测试结果:

Emp{dept=Dept{}}

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3

自动装配

自动装配分为两种方式:根据属性名称自动注入,根据属性类型自动注入。

自动装配的配置文件:

<?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标签属性 autowire ,配置自动装配 autowire属性常用的两个值: byName 根据属性名称自动注入 ,要注入的那个值bean的id值要和类里面属性名称一样 byType:根据属性类型自动注入 --> <bean id="emp" class="com.Keafmd.spring5.autowire.Emp" autowire="byType"> <!--手动装配-->
<!-- <property name="dept" ref="dept"></property>--> </bean> <bean id="dept" class="com.Keafmd.spring5.autowire.Dept"></bean> <!--根据属性类型注入,多写这行就会报错,类型匹配到了两个bean,byName不会错-->
<!-- <bean id="dept1" class="com.Keafmd.spring5.autowire.Dept"></bean>-->


</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

测试代码相同:

package com.Keafmd.spring5.testdemo;

import com.Keafmd.spring5.autowire.Emp;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 { @Test public void test5(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml"); Emp emp = context.getBean("emp",Emp.class); System.out.println(emp); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

测试结果:

Emp{dept=Dept{}}

Process finished with exit code 0

  
 
  • 1
  • 2
  • 3

这就是自动装配,但是这种基于xml自动装配并不常用,常用的是基于注解的。
以上就是基于xml的自动装配。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

文章来源: keafmd.blog.csdn.net,作者:牛哄哄的柯南,版权归原作者所有,如需转载,请联系作者。

原文链接:keafmd.blog.csdn.net/article/details/112706908

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。