Java:
1.8
javaMaven:
3
springSpringFramework版本以及各组件成员:
5.1.1.RELEASE
this
- spring-context
- spring-core
- spring-beans
byName
来自: W3CSchool中的概述code
这种模式由属性名称指定自动装配。Spring 容器看做 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。而后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和链接。若是找到匹配项,它将注入这些 beans,不然,它将抛出异常。xml
byName
在设值注入的应用)其目的是省略XML中的一些配置, 以减小代码量.对象
Bean - Bean.java
get
❗这个Bean只是因为Spring配置所写顺口叫的, 并不符合严格意义上的JavaBean.it
package noioo; public class Bean { public void sayHelloWorld(){ System.out.println("Hello World"); } }
Bean的使用者, 它依赖这个Bean - BeanUser.java
io
package noioo; public class BeanUser { private Bean beanName; public Bean getBeanName() { return beanName; } public void setBeanName(Bean beanName) { this.beanName = beanName; } public void useSayHelloWorld(){ beanName.sayHelloWorld(); } }
Spring XML配置文件 - spring-beans.xml
class
<?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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <bean id="beanUser" class="noioo.BeanUser"> <!-- 待会咱们将省略下面这个property配置 --> <property name="beanName" ref="bean"/> </bean> <bean id="beanName" class="noioo.Bean"> </bean> </beans>
执行者 - Runner.java
package noioo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Runner { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml"); BeanUser beanUser = (BeanUser)context.getBean("beanUser"); beanUser.useSayHelloWorld(); } }
Hello World Process finished with exit code 0
byName
来省略其中的一些配置修改spring-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" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <!-- 去掉了property, 新增了属性autowire="byName" --> <bean id="beanUser" class="noioo.BeanUser" autowire="byName"> </bean> <!-- 这里的id值对应了BeanUser中的属性名bean --> <bean id="beanName" class="noioo.Bean"> </bean> </beans>
也就是说, 在
BeanUser
这个类中的属性private Bean beanName
将被取出, 用来匹配同名为"beanName"
的Spring Bean.
运行结果同样.
Hello World Process finished with exit code 0
虽然在这里省略的代码并非不少, 毕竟只有一个对象....
byType
这种模式由属性类型指定自动装配。Spring 容器看做 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。而后,若是它的 type 刚好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和链接它的属性。若是找到匹配项,它将注入这些 beans,不然,它将抛出异常。
来自: W3CSchool中的概述
上面的Java代码不用改, 仅仅修改配置文件便可.
Spring配置文件 - spring-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" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <bean id="beanUser" class="noioo.BeanUser" autowire="byType"> <!-- 它将会以定义的属性值的类型来匹配这里的Bean名, 属性值是Bean, 也就是将会匹配一个类型为"Bean"的Spring Bean --> </bean> <!-- 由于是根据类型匹配的, 咱们甚至连给这个Bean起id都不用 --> <bean class="noioo.Bean"> </bean> </beans>
❗注意:
是按照
UserBean
中属性的类型来匹配的.
Hello World Process finished with exit code 0