Spring 中Bean的自动装配六种模式,你懂得几种?
public class AddressServiceImpl { /**住址*/private String address; public void setAddress(String address){this.address=address;}}public class HomeAddressServiceImpl extends AddressServiceImpl {private String address;public void setAddress(String address){this.address=address;}public HomeAddressServiceImpl() {super();}public HomeAddressServiceImpl(String address){this.address=address;}}public class EmpServiceImpl {/**公司地址*/private AddressServiceImpl companyAddress;public void setCompanyAddress(AddressServiceImpl companyAddress){this.companyAddress=companyAddress;}}
<?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-2.0.xsd"default-autowire="no"><bean id="homeAddressServiceImpl" autowire="default" /></beans>
public class App { @Testpublic void test(){ApplicationContext ac= new ClassPathXmlApplicationContext("classpath:default.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");}}
<?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-2.0.xsd"><bean id="homeAddressServiceImpl" autowire="byName" /></beans>
public class App { @Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");}}
<?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-2.0.xsd"><bean id="homeAddressServiceImpl" autowire="byType" /></beans>
<beanid="addressServiceImpl"scope="singleton"/>//homeAddressServiceImpl是继承addressServiceImpl,所以他们是同一类型!<!-- 当有多个相同类型的bean时,会出现bug如下: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empServiceImpl' defined in file [D:\Workspaces\MyEclipse 8.6\20110419_01\bin\applicationContext.xml]: Unsatisfied dependency expressed through bean property 'companyAddress': : No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cn.csdn.service.AddressServiceImpl] is defined: expected single matching bean but found 2: [homeAddressServiceImpl, addressServiceImpl]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)at java.security.AccessController.doPrivileged(Native Method)..........................-->
public class App { @Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");}}
<?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-2.0.xsd"> <!-- 配置bean 相同类型只能在 配置文件中出现一次<bean id="homeAddressServiceImpl" scope="prototype"> <property name="address"> <value>北京</value> </property></bean><!-- 自动装配 采用constructor 构造器中的参数是按照byType进行装配的<bean id="empServiceImpl" scope="singleton" autowire="constructor"/></beans>
public class App { @Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");}}
<?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-2.0.xsd"> <!-- 配置bean --><bean id="addressServiceImpl" scope="singleton"> <property name="address"> <value>北京</value> </property></bean><bean id="empServiceImpl" scope="singleton" autowire="autodetect"/></beans>
public class App { @Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");}}
是不是大部分人认为有5种?但你你们忽略的往往是最重要的