org.springframework.beans.factory.UnsatisfiedDependencyException Java Examples

The following examples show how to use org.springframework.beans.factory.UnsatisfiedDependencyException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #2
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #3
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredFieldResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class,
				ex.getInjectionPoint().getField().getDeclaringClass());
	}
}
 
Example #4
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #5
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #6
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #7
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #8
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class));

	try {
		bf.getBean("annotatedBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
	}
}
 
Example #9
Source File: AbstractAutowireCapableBeanFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * ִ��һ�������Լ�飬�������ع�������ѱ�����
 * <p>
 * Perform a dependency check that all properties exposed have been set, if
 * desired. Dependency checks can be objects (collaborating beans), simple
 * (primitives and String), or all (both).
 * 
 * @param beanName
 *            the name of the bean
 * @param mbd
 *            the merged bean definition the bean was created with
 * @param pds
 *            the relevant property descriptors for the target bean
 * @param pvs
 *            the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			// �����㣬���쳣
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example #10
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #11
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredMethodResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class,
				ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
	}
}
 
Example #12
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationOptionalFieldResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationOptionalFieldResourceInjectionBean.class,
				ex.getInjectionPoint().getField().getDeclaringClass());
	}
}
 
Example #13
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationOptionalMethodResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationOptionalMethodResourceInjectionBean.class,
				ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
	}
}
 
Example #14
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #15
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #16
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenNoDependencyFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredMethodResourceInjectionBean.class));

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class,
				ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
	}
}
 
Example #17
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #18
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #19
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #20
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #21
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #22
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #23
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredMethodResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class,
				ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
	}
}
 
Example #24
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenNoDependencyFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredMethodResourceInjectionBean.class));

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class,
				ex.getInjectionPoint().getMethodParameter().getDeclaringClass());
	}
}
 
Example #25
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the SPR-2098 bug whereby no more than 1 property element could be
 * passed to a scripted bean :(
 */
@Test
public void testCanPassInMoreThanOneProperty() {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-multiple-properties.xml", getClass());
	TestBean tb = (TestBean) ctx.getBean("testBean");

	ContextScriptBean bean = (ContextScriptBean) ctx.getBean("bean");
	assertEquals("The first property ain't bein' injected.", "Sophie Marceau", bean.getName());
	assertEquals("The second property ain't bein' injected.", 31, bean.getAge());
	assertEquals(tb, bean.getTestBean());
	assertEquals(ctx, bean.getApplicationContext());

	ContextScriptBean bean2 = (ContextScriptBean) ctx.getBean("bean2");
	assertEquals(tb, bean2.getTestBean());
	assertEquals(ctx, bean2.getApplicationContext());

	try {
		ctx.getBean("bean3");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.contains(UnsatisfiedDependencyException.class));
	}
}
 
Example #26
Source File: AbstractAutowireCapableBeanFactory.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example #27
Source File: AbstractAutowireCapableBeanFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, @Nullable PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && (pvs == null || !pvs.contains(pd.getName()))) {
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			boolean unsatisfied = (dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example #28
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredFieldResourceInjectionBean.class));
	TestBean tb1 = new TestBean();
	bf.registerSingleton("testBean1", tb1);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class,
				ex.getInjectionPoint().getField().getDeclaringClass());
	}
}
 
Example #29
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() {
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationRequiredFieldResourceInjectionBean.class));

	try {
		bf.getBean("customBean");
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class,
				ex.getInjectionPoint().getField().getDeclaringClass());
	}
}
 
Example #30
Source File: AbstractAutowireCapableBeanFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}