在Spring中的@Required注释

时间:2020-02-23 14:35:42  来源:igfitidea点击:

@RequiredSpring中的注释适用于Bean属性Setter方法:

package org.igi.javapostsforlearning;
 
public class Country {
 
    Capital capital; 
 
    @Required
    public void setCapital(Capital capital) {
        this.capital = capital;
    }
}

此注释简单地表示必须在配置时填充受影响的bean属性:通过bean定义中的显式属性值或者通过自动控制。
如果受影响的bean属性尚未填充,则容器将抛出异常;这允许渴望和显式失败,避免 NullPointerExceptionS等后来。

假设你有很大的应用程序,你得到了 NullPointerException由于未被注入所需的依赖性,那么很难找到出现问题。
所以该注释有助于我们调试代码。

例子:

在Eclipse IDE中配置Spring,请参阅Hello World示例

1.Country.java:

这是简单的Pojo类,其中有一些属性,所以国家有资本类的名称和对象。

在package org.igi.javapostssforlarearning中创建country.java .Copy内容到Country.java之后。

package org.igi.javapostsforlearning;
import org.springframework.beans.factory.annotation.Required;
public class Country {
 
	String countryName;
	Capital capital;
	public String getCountryName() {
		return countryName;
	}
	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}
	public Capital getCapital() {
		return capital;
	}
 
	@Required
	public void setCapital(Capital capital) {
		this.capital = capital;
	}
}

2.Capital.java.

这也是一个名为"bueinkname"的一个属性的简单pojo类。

在package org.igi.javapostsforlearning时创建Capital.java .java.above country类包含此类的对象。
内容进入capical.java之后

package org.igi.javapostsforlearning;
 
public class Capital {
 
	String capitalName;
 
	public String getCapitalName() {
		return capitalName;
	}
 
	public void setCapitalName(String capitalName) {
		this.capitalName = capitalName;
	}
}

3.ApplicationContext.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <context:annotation-config
 <bean id="CountryBean" class="org.igi.javapostsforlearning.Country">
  <property name="countryName" value="Netherlands" 
 </bean>
 <bean id="CaptialBean" class="org.igi.javapostsforlearning.Capital">
  <property name="capitalName" value="Amsterdam" 
 </bean>
</beans>

现在我们可以在此处注意到,我们没有将资本属性注入国家级,但我们使用它来获取CapitalName在CardandannotationInspringMain类中。

4.requiredannotationinspringmain.java.

此类包含Main Function.CreateAnnotationInspringMain.java在package org.igi.javapostsforlearning中的.copy之后的内容到CardationAnnotationInspringMain.java之后

package org.igi.javapostsforlearning;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class RequiredAnnotationInSpringMain{
 
	    public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
 
		Country countryObj = (Country) appContext.getBean("CountryBean");
		String countryName=countryObj.getCountryName();
		Capital capital=countryObj.getCapital();
		String capitalName=capital.getCapitalName();
		System.out.println(capitalName+" is capital of "+countryName);
		       
	}
}

5.运行:

当我们将运行上面的应用程序时,我们将按照输出作为输出。

Sep 01, 2012 1:43:16 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17a1767: startup date [Sat Sep 01 13:43:16 IST 2012]; root of context hierarchy
Sep 01, 2012 1:43:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ApplicationContext.xml]
Sep 01, 2012 1:43:17 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@54446d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,CountryBean,CaptialBean,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Sep 01, 2012 1:43:17 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@54446d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,CountryBean,CaptialBean,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CountryBean' defined in class path resource [ApplicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'capital' is required for bean 'CountryBean'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at org.igi.javapostsforlearning.SetterInjectionMain.main(SetterInjectionMain.java:11)
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'capital' is required for bean 'CountryBean'
    at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:149)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 11 more

当你运行它时,你会得到异常抱怨“属性'capital'是bean'CountryBean'所必需的”