Spring中使用byName实现Beans自动装配

本文内容纲要:Spring中使用byName实现Beans自动装配

以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html:

此模式通过属性名称来指定自动装配。Spring容器查看XML配置文件中auto-wire属性设置为byName的bean。然后,它尝试将其属性与配置文件中相同名称定义的bean进行匹配并连接。如果找到匹配,它将注入这些bean。否则,bean将不会被连线。

例如,如果一个bean定义在配置文件中设置为autowire="byName",并且它包含一个spellChecker属性(即它有一个setSpellChecker(...)方法),则Spring会查找一个名为spellChecker的bean定义,并使用它设置属性。当然,还可以使用标签连接剩余的属性。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.jsoft.testspring</groupId>

<artifactId>testautowiringbyname</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>testautowiringbyname</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<!-- Spring Core -->

<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.1.4.RELEASE</version>

</dependency>

<!-- Spring Context -->

<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.4.RELEASE</version>

</dependency>

</dependencies>

</project>

SpellChecker.java:

package com.jsoft.testspring.testautowiringbyname;

public class SpellChecker {

public SpellChecker(){

System.out.println("SpellChecker无参数构造函数初始化");

}

public void checkSpelling(){

System.out.println("SpellChecker检查方法");

}

}

TextEditor.java:

package com.jsoft.testspring.testautowiringbyname;

public class TextEditor {

private SpellChecker spellChecker;

public void setSpellChecker(SpellChecker spellChecker){

System.out.println("TextEditor通过setter初始化");

this.spellChecker = spellChecker;

}

public void spellCheck() {

this.spellChecker.checkSpelling();

}

}

beans.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="spellChecker" class="com.jsoft.testspring.testautowiringbyname.SpellChecker"></bean>

<bean id="textEditor" class="com.jsoft.testspring.testautowiringbyname.TextEditor" autowire="byName">

</bean>

</beans>

变化之前的beans.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="spellChecker" class="com.jsoft.testspring.testautowiringbyname.SpellChecker"></bean>

<bean id="textEditor" class="com.jsoft.testspring.testautowiringbyname.TextEditor">

<property name="SpellChecker" ref="spellChecker" />

</bean>

</beans>

使用了自动装配类型byName去实现,省略了属性。

App.java:

package com.jsoft.testspring.testautowiringbyname;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* Hello world!

*

*/

public class App

{

public static void main( String[] args )

{

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");

textEditor.spellCheck();

}

}

运行结果:

Image

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test11/testautowiringbyname

本文内容总结:Spring中使用byName实现Beans自动装配

原文链接:https://www.cnblogs.com/EasonJim/p/6887457.html

以上是 Spring中使用byName实现Beans自动装配 的全部内容, 来源链接: utcz.com/z/296086.html

回到顶部