Spring自動装着(atowire)はお手数ですか?(二)

11110 ワード

Spring自動組立(atowire)は面倒ですか?(二)
    前の文章のテキストが長すぎて、Spring自動組立の三つの属性を紹介しました.ここで紹介します.
 
 
 
//spring  app.xml   
<!--     empServiceImpl javabean   -->
	<bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl">
	<property name="name">
	<value>Nan</value></property>
	</bean>
<!--     hourEmpServiceImpl javabean  ,  empServiceImpl      -->
<bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire="constructor"  >
<!—   autowire="constructor" HourEmpServiceImpl            ,  empServiceImpl= null。
          , xml           bug:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hourEmpServiceImpl' defined in class path resource [app.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.csdn.service.EmpServiceImpl]: : No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:591)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.csdn.junit.EmpTest.test(EmpTest.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.csdn.service.EmpServiceImpl] is defined: Unsatisfied dependency of type [class com.csdn.service.EmpServiceImpl]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:622)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:584)
	... 42 more

-->
</bean>
//empServiceImpl   
package com.csdn.service;
public class EmpServiceImpl {
/**        */
private String name;
/**  set      */
	public void setName(String name) {
	this.name = name;
	}
}
// hourEmpServiceImpl   
package com.csdn.service;
public class HourEmpServiceImpl {
/**    EmpServiceImpl     */
	private EmpServiceImpl empServiceImpl;
/**         */
	public HourEmpServiceImpl(EmpServiceImpl empServiceImpl) {
		this.empServiceImpl = empServiceImpl;
	}
}
//junit     
 @Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
HourEmpServiceImpl  hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); 
}



 
 
 
 
ケース:
//spring  app.xml   
<!--     empServiceImpl javabean   -->
	<bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl">
	<property name="name">
	<value>Nan</value></property>
	</bean>
<!--     hourEmpServiceImpl javabean  ,  empServiceImpl      -->
<bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire=" autodetect"  >
<!—autowire    autodetect     constructor     ,    byType  -->
</bean>
//empServiceImpl   
package com.csdn.service;
public class EmpServiceImpl {
/**        */
private String name;
/**  set      */
	public void setName(String name) {
	this.name = name;
	}
}
// hourEmpServiceImpl   
package com.csdn.service;
public class HourEmpServiceImpl {
/**    EmpServiceImpl     */
	private EmpServiceImpl empServiceImpl;
	/**     */
	public HourEmpServiceImpl(EmpServiceImpl empServiceImpl) {
		System.out.println("     ");
		this.empServiceImpl = empServiceImpl;
	}
/**     set      set     */
	public void setEmpServiceImpl(EmpServiceImpl empServiceImpl) {
		System.out.println("set    ");
		this.empServiceImpl = empServiceImpl;
	}
	}
//junit     
 @Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
HourEmpServiceImpl  hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); 
}
 
 
 
 
 
<?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.5.xsd"
	default-autowire="byName">
	
	<!--     empServiceImpl javabean   -->
	<bean id="empServiceImpl" class="com.csdn.service.EmpServiceImpl">
	<property name="name">
	<value>   </value></property>
	</bean>
	<!--     hourEmpServiceImpl javabean  ,  empServiceImpl      -->
	<bean id="hourEmpServiceImpl" class="com.csdn.service.HourEmpServiceImpl" autowire=" default ">
<!—  bean autowire             -->
	</bean>
</beans>
//empServiceImpl   
package com.csdn.service;
public class EmpServiceImpl {
/**        */
private String name;
/**  set      */
	public void setName(String name) {
	this.name = name;
	}
}
// hourEmpServiceImpl   
package com.csdn.service;
public class HourEmpServiceImpl {
/**    EmpServiceImpl     */
	private EmpServiceImpl empServiceImpl;
/**     set      set     */
	public void setEmpServiceImpl(EmpServiceImpl empServiceImpl) {
		this.empServiceImpl = empServiceImpl;
	}
}
//junit     
 @Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
HourEmpServiceImpl  hsi=(HourEmpServiceImpl) ac.getBean("hourEmpServiceImpl"); 
}

 
http://www.verydemo.com/demo_c 143_i 16403.