Bean的装配方式

一、基于xml的装配

Student.java

package com.yh;

public class Student implements People {

    public void breath() {
        System.out.println("呼吸");
    }
}

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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.1.xsd">

     <bean id="student" class="com.yh.Student"></bean>

</beans>

测试

@Test
public void demo01() {
    String xmlPath = "applicationContext.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
    Student stu = (Student) context.getBean("student");
    stu.breath();
}

二、基于Annotation(注解)的装配

Student,java

package com.yh;

import org.springframework.stereotype.Component;
@Component
public class Student implements People {

    public void breath() {
        System.out.println("呼吸");
    }
}

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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.yh"></context:component-scan>

</beans>

测试

@Test
public void demo01() {
    String xmlPath = "applicationContext.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
    Student stu = (Student) context.getBean("student");
    stu.breath();
}

原文地址:https://www.cnblogs.com/YeHuan/p/11072651.html

时间: 2024-08-30 18:10:36

Bean的装配方式的相关文章

Spring Bean的装配方式

Spring容器负责创建应用程序中的Bean,并通过依赖注入协调这些对象之间的关系.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入(Dependentcy Injection)的本质.Bean的装配方式机Bean依赖注入. Spring容器支持多种形式的Bean装配方式,如基于XML的装配.基于注解(Annotation)的装配和自动装配. 基于XML的Bean装配 Spring提供了两种基于XML的装配方式:属性setter方法注入和构造方法注入. 在Spring实

Bean自动装配-XML最小化配置

上一个讲了怎样用xml配置所有的Bean以及进行依赖注入,但是这样太麻烦了,对于每一个bean都要写一个xml,可以有几种方式进行自动装配. 四种装配方式 byName(通过名字装配,这时属性的名字必须与bean名字一致) byType(通过类型,匹配与interface或class相同的类型),这种是找到对应的方法,然后进行setter方法进行注入的 constructor(也是通过类型匹配,但是是通过new的进行装配的) 最佳自动装配(先用constructor装配,然后再用byType)

Spring框架第一篇之Bean的装配

一.默认装配方式 代码通过getBean();方式从容器中获取指定的Bean实例,容器首先会调用Bean类的无参构造器,创建空值的实例对象. 举例: 首先我在applicationContext.xml配置文件中配置了一个bean: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" x

XML 配置里的 Bean 自动装配

在XML文件中,先看一下下面的代码: <bean id="student" class="com.jeremy.spring.beans.student"> <property name="s_name" value="jeremy"></property> <property name="age" value="20"></prop

Spring bean依赖注入、bean的装配及相关注解

依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { private Messaging messaging; /* * DI via Setter */ public void setMessaging(Messaging messaging){ this.messaging = messaging; } public void communicate(){

Spring中如何混用XML与Java装配方式

由David发表在天码营 多种装配方式的混用 除了自动配置,Spring使用显式的Java配置或者XML配置也可以完成任何装配.通常情况下可能在一个Spring项目中同时使用自动配置和显式配置. 对于自动配置,它自动从整个容器上下文中查找合适的bean,这个bean是通过@Component之类的标准,可能来自Java配置或者XML配置. 我们先来了解一下Java配置吧. Java配置 创建配置类 使用Java配置,通过创建一个专门用于Spring配置的类,然后通过@Configuration标

Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.kejian.spring.bean.autowire.Student" p:name="Tony" autowire="byName"></bean> 1.2 类型 byName 目标bean的id与属性名一置,若不匹配置为null byTy

Spring -- Bean自动装配&amp;Bean之间关系&amp;Bean的作用域

Bean的自动装配 Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式 有以下几种自动装配的类型: byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配. byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同. constructor(通过构造器自动装配):

spring使用之旅(一) ---- bean的装配

基础配置 启用组件扫描配置 Java类配置文件方式 package com.springapp.mvc.application; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * Created by xuc on 2018/1/7. * 配置类 */ @ComponentScan(baseP