當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 基于注解(annotation)的配置之@Required注解
生活随笔
收集整理的這篇文章主要介紹了
Spring 基于注解(annotation)的配置之@Required注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從 Spring 2.5 開始就可以使用注解來配置依賴注入。注解連線在默認情況下在 Spring 容器中不打開。因此,在可以使用基于注解的連線之前,我們將需要在我們的 Spring 配置文件中啟用它:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><!-- bean definitions go here --></beans>一些重要的注解:
- @Required: 應用于 bean 屬性的 setter 方法。
- @Autowired: 應用到 bean 屬性的 setter 方法,非 setter 方法,構造函數和屬性。
- JSR-250 Annotations: Spring 支持 JSR-250 的基礎的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解。
Spring @Required 注釋
@Required 注釋應用于 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時必須放在 XML 配置文件中,否則容器就會拋出一個 BeanInitializationException 異常。
看個例子:
import org.springframework.beans.factory.annotation.Required; public class Student {private Integer age;private String name;@Requiredpublic void setAge(Integer age) {this.age = age;}public Integer getAge() {return age;}@Requiredpublic void setName(String name) {this.name = name;}public String getName() {return name;} }MainApp:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");Student student = (Student) context.getBean("student");System.out.println("Name : " + student.getName() );System.out.println("Age : " + student.getAge() );} }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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><!-- Definition for student bean --><bean id="student" class="com.sap.Student"><property name="name" value="Zara" /><!-- try without passing age and check the result --><!-- property name="age" value="11"--></bean></beans>按照上述的beans.xml執行應用,會遇到下列錯誤:Property ‘age’ is required for bean ‘student’
Jul 25, 2020 11:31:37 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c6: startup date [Sat Jul 25 11:31:37 CST 2020]; root of context hierarchy Jul 25, 2020 11:31:37 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml] Jul 25, 2020 11:31:38 AM org.springframework.context.support.ClassPathXmlApplicationContext refresh WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [Beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'age' is required for bean 'student' Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [Beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'age' is required for bean 'student'at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)at com.sap.MainApp.main(MainApp.java:8) Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'age' is required for bean 'student'at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)... 11 more在Beans.xml里重新啟用age property的注入,錯誤消失。
經過實際測試,Student bean里這個setter方法的@Required注解,加或不加,結果都一致。
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的Spring 基于注解(annotation)的配置之@Required注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spine批量导出Command lin
- 下一篇: flash如何制作扇子张开收合的动画效果