gpt4 book ai didi

java - Spring @Required 和@Mandatory 注解

转载 作者:行者123 更新时间:2023-11-30 08:18:38 25 4
gpt4 key购买 nike

这是BeanClass.java

package com.practice.spring;

import org.springframework.beans.factory.annotation.Required;

import com.apress.springrecipes.sequence.Mandatory;

public class BeanClass {

private int count;
private String prefix;

public BeanClass() {
System.out.println("Default Constructor");
}

public BeanClass(int count, String prefix) {
this.count = count;
this.prefix = prefix;
System.out.println(prefix+count);
}

@Required
public void setCount(int count) {
this.count = count;
}

@Mandatory
public void setPrefix(String prefix) {
this.prefix = prefix;
}

@Override
public String toString() {
return prefix+count;
}

}

这是beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config />

<bean id="beanClass1"
class="com.practice.spring.BeanClass">
<property name="count" value="1" />
<property name="prefix" value="Bean" />
</bean>
<bean id="beanClass2"
class="com.practice.spring.BeanClass">
<constructor-arg value="2" />
<constructor-arg value="Bean" />
</bean>
<bean id="beanClass3"
class="com.practice.spring.BeanClass">
<constructor-arg>
<value>3</value>
</constructor-arg>
<constructor-arg>
<value>Bean</value>
</constructor-arg>
</bean>
</beans>

这就是我实例化上下文的方式。

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

问题:

count 属性是必需的,前缀是必需的。如果您看到 beanClass2 我正在通过构造函数设置属性,在 beanClass3

中也是如此

但是当我执行代码时,它会抛出异常。令我困惑的重要事情是:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanClass2' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'count' is required for bean 'beanClass2'

它打印默认构造函数和 Bean2。我还将整个堆栈跟踪粘贴在这里:

Dec 09, 2014 8:47:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a71e93: startup date [Tue Dec 09 20:47:33 PKT 2014]; root of context hierarchy Dec 09, 2014 8:47:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [beans.xml] Dec 09, 2014 8:47:33 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4aed64: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanClass1,beanClass2,beanClass3]; root of factory hierarchy

Default Constructor

Bean2

Dec 09, 2014 8:47:33 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4aed64: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanClass1,beanClass2,beanClass3]; root of factory hierarchy Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanClass2' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'count' is required for bean 'beanClass2' 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$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.practice.spring.Main.main(Main.java:13) Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'count' is required for bean 'beanClass2' at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:149) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ... 11 more

最佳答案

@Required 暗示您正在使用 setter 注入(inject),而不是构造函数注入(inject)。这些被设计成两个选项——不要同时做这两个选项。

注意这个来自 Spring 的博客 -

@ Required allows you to instruct Spring to check required dependencies for you. In case you are not in the position to use constructor injection, or for whatever other reasons, you prefer setter injection, @ Required is the way to go.

Setter Injection vs Constructor Injection .

最近,

@ Required Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.

RequiredAnnotation

因此,您将其标记为必须进行依赖注入(inject) - 而不是构造函数注入(inject)。

关于java - Spring @Required 和@Mandatory 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com