1.使用xml配置文件装配

  Spring成功启动的三大要件分别是:Bean定义信息、Bean实现类以及Spring本身。

  在xml文件中配置bean,然后使用ClassPathXmlApplicationContext得到Application从而得到bean

首先定义一个bean类:Student

package com.bean;public class Students {String name;String id;String major;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}
}

在xml文件中配置这个bean:beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd"><bean id="students" class="com.bean.Students"> <!--1-->
<property name="name" value="ironman"></property><!--2--><property name="id" value="111111"></property><property name="major" value="software"></property> </bean>
</beans>

(1)处定义了bean的name和class。

(2)处定义了bean的初始值。

我们可以用一个函数来测试一下这个函数

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Students s =(Students) context.getBean("students");System.out.println(s.name+"\n"+s.id+"\n"+s.major);}}

得到Students的一个对象

2.使用注解配置bean

  不管是XML还是注解,他们都是表达bean定义的载体,其实质都是为Spring容器提供bean的定义信息,表现形式上是将xml定义的东西通过类注解进行描述。

  我们从新定义一个Bean并且注入Students哪个Bean

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component("myClass")//这里通过Component来定义了一个Bean,括号里面的是自己定义的Bean的名字相当于xml中的id
public class MyClass {@Autowired      //这里自动注入Students的Beanprivate Students students;private String classId;private String className;private String teacherName;public String getClassId() {return classId;}public void setClassId(String classId) {this.classId = classId;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public Students getStudents() {return students;}public void setStudents(Students students) {this.students = students;}}

还需要在xml文件配置

Spring在2.5之后提供了一个context命名空间,它提供了通过扫描应用类包以应用注解定义bean的方式。

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"  <!--声明context命名空间-->xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"><bean id="students" class="com.bean.Students">
<property name="name" value="litao"></property>
<property name="id" value="1110321124"></property>
<property name="major" value="software"></property>
</bean><context:component-scan base-package="com.bean"></context:component-scan><!--使用componet-scan的base-package属性定义要扫描的类包--></beans>

我们用一个Main函数来测试一下这个定义

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainForComponent {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext context  = new ClassPathXmlApplicationContext("beans.xml");MyClass myClass =(MyClass) context.getBean("myClass");myClass.setClassId("software engineering");System.out.println(myClass.getClassId()+"\n"+myClass.getClassName()+"\n"+myClass.getStudents().getId());}}

3.基于java类的配置

我们可以不再用xml配置文件来定义bean了

定义一个Class为Bean的配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.bean.MyClass;
import com.bean.Students;@Configuration              //将一个pojo标注为定义bean的配置类
public class AppConf {@Bean                       //将一个pojo定义为一个beanpublic Students myStudents(){return new Students();}@Beanpublic MyClass myClass(){MyClass myClass = new MyClass();myClass.setStudents(myStudents());//将Students注入MyClassreturn myClass();}}

同样的我们来写一个Main来测试一下这个bean的配置函数

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.bean.MyClass;public class Main {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext context = new AnnotationConfigApplicationContext(AppConf.class);MyClass myClass = context.getBean(MyClass.class);}}

转载于:https://www.cnblogs.com/ironmantony/p/3475227.html

Spring 三种bean装配的方式相关推荐

  1. 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程

    一.依赖注入DI 学习目标 1)搞清楚构造参数依赖注入的过程及类 2)搞清楚注解方式的属性依赖注入在哪里完成的. 学习思路 1)思考我们手写时是如何做的 2)读 spring 源码对比看它的实现 3) ...

  2. SpringBoot 三种拦截http请求方式Filter,interceptor和aop

    SpringBoot 三种拦截http请求方式Filter,interceptor和aop. 这三种拦截方式的拦截顺序是:filter->Interceptor-->ControllerA ...

  3. JDK/Dubbo/Spring 三种 SPI 机制,谁更好?

    点击关注公众号,Java干货及时送达 来源:juejin.cn/post/6950266942875779108 SPI 全称为 Service Provider Interface,是一种服务发现机 ...

  4. JDK/Dubbo/Spring 三种 SPI 机制,谁更好呢?

    JDK/Dubbo/Spring 三种 SPI 机制,谁更好? SPI 全称为 Service Provider Interface,是一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文 ...

  5. zynq学习03 zynq中三种实现GPIO的方式

    http://m.blog.csdn.net/article/details?id=52123465 http://blog.chinaaet.com/songhuangong/p/43084 本文介 ...

  6. SSO单点登录三种情况的实现方式详解

    SSO单点登录三种情况的实现方式详解 单点登录(SSO--Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子 ...

  7. Objective-C:三种文件导入的方式以及atomic和nonatomic的区别

    一.三种文件导入的方式比较:   类的前项声明@class.import.include: 1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

  8. 浅淡Webservice、WSDL三种服务访问的方式(附案例)

    Webservice Webservice是使应用程序以与平台和编程语言无关的方式进行相互通信技术. eg:站点提供访问的数据接口:新浪微博.淘宝. 官方解释:它是一种构建应用程序的普遍模型,可以在任 ...

  9. Python中的urllib,urllib三种不同的请求方式

    1.urllib获取服务器的资源 自定义爬虫的重要组件 获取百度首页的资源: #3.x的标准写法 import urllib.request import urllib.parse#百度的首页 fro ...

最新文章

  1. 本人CCNP、OCP MCSA 证书寻求挂靠
  2. 我的泰坦尼克数据分析
  3. sublime text3 注册码,亲测可用
  4. CF1110F Nearest Leaf
  5. winform窗体中嵌入显示Excel文件
  6. RTDS学习笔记——网口连接
  7. iOS视频播放器开发
  8. 十个程序员必备的网站推荐和较出名的国外程序员论坛
  9. docker修改服务器防火墙,docker宿主机iptables配置
  10. 怎么打开和修改dll文件的?如何调用和编辑?
  11. 1076: 三位数求解-python
  12. Android Studio第九课(学习打卡Day11)
  13. 极验:验证码在黑灰产对抗中的角色和实践
  14. BZOJ 1066 POJ 2711 [SCOI2007]蜥蜴
  15. socket编程之read()/write()
  16. python中1到100怎么表示_python如何计算1到100的和(用for循环)
  17. 清华大学赵明国:AI芯片 +机器人,突破算法瓶颈
  18. 德国战车7比1狂胜巴西
  19. 电商项目的并发量一般是多少_【高并发】高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!...
  20. 插在计算机主板的扩展插槽,终于有人在计算机主板上放置了四个通用插槽: PCI...

热门文章

  1. 1094 谷歌的招聘 (20分)
  2. 问题 I: A+B Problem : Input/Output Practice 山东科技大学OJ C语言
  3. ROS学习笔记4(编译一个ROS Package)
  4. Python中的字符串特性(索引、切片、重复、连接、成员操作符号)
  5. Python检验某个字符(串)是否属于另一个字符串
  6. php revel,试用revel-一个基于golang的web框架
  7. PIP scrapydo时报错ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log
  8. 灰度董事总经理:BTC突破2万美元并不令人惊讶
  9. SAP License:税-你知道多少?
  10. 赛锐信息:在云中交付SAP解决方案