- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是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.
因此,您将其标记为必须进行依赖注入(inject) - 而不是构造函数注入(inject)。
关于java - Spring @Required 和@Mandatory 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383245/
当另一个 [关联] 字段具有特定值时,如何使必填字段成为非必填字段? 假设我有以下模型: class foo(models.Model): bar = models.CharField(max
我有一个父表 Venue (venueId[PK],venueName) 和两个子表,Performance (performanceId[PK], performanceName) 和 Meal (
总结:我需要将 YAML 格式的数据解析为 golang 结构。是否有一种方法(库、属性)使某些字段成为必需字段,即如果某些字段不存在,则使 Unmarshal 函数返回错误? 例如想要什么:此代码中
我想知道 SSL 证书和以 https:// 开头的 URL 是否对于 Telegram 机器人的 webhook 是必需的?正如它在 https://core.telegram.org/bots/a
我需要强制使用 Django User 模型中的电子邮件字段。如何做到这一点对我来说并不明显。欢迎提出建议。我目前正在使用: from django.contrib.auth.forms import
我有以下 XSD 验证: SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XM
使用带有 的响应式图像集时如果没有断点(即没有 sizes 属性),您通常会提供不同分辨率的同一图像的多个版本,然后您在 srcset 中定义这些版本使用像素密度语法的属性,例如1x , 2x ,
不少网站不需要注册人验证他们的电子邮件,而且它们似乎运行良好,而无需您经历这种(轻微的)考验。 这种机制可以确保电子邮件的有效性并阻止不受欢迎的机器人似乎是合乎逻辑的,但是它是否适用于低用户功能的网站
我正在尝试在 wildfly 8.2.0-Final 上定义带有注释和部署描述符的消息驱动 bean。我想用注释声明配置的静态部分,用 xml 部署描述符声明变量部分。部署以下 mdb 时,出现异常。
我需要在没有沙盒的情况下分发我的应用程序,因为我需要可访问性 API。有谁知道如果我在没有启用沙盒的情况下提交应用程序是否会被拒绝(有充分的理由)? 在 Mac 中 App Store Guideli
我们有一个通过 Web Start 使用 JNLP 启动的应用程序,使用以下内容来确保应用程序仅使用最新代码运行: title.... vendor nam
我发现这令人费解且违反直觉: 为什么会这样: LoggingConfiguration config = LogManager.Configuration; config.LoggingRules.A
我在 Play 中发现了有趣的东西!框架表单验证。例如我有这样的形式: case class Foo(mystring: String, myint: Int, mybool: Boolean) {
我有一个 Activity 类,它被注释为一个调用 Action 类的组件: @Transactional(propagation = Propagation.REQUIRED, readOnly
我是 azure 管道的新手。我有使用 azure 管道的构建作业,源代码位于同一分支开发中并且运行良好,但是是否可以在单独的分支中拥有 azure 管道和源代码? 如果是这样,请帮助我 此外,我如何
我们正在尝试以编程方式在开发测试实验室中启用强制工件。我们发现:https://learn.microsoft.com/en-us/azure/templates/microsoft.devtestl
我们正在尝试以编程方式在开发测试实验室中启用强制工件。我们发现:https://learn.microsoft.com/en-us/azure/templates/microsoft.devtestl
我希望我可以设置一个 cmdlet,以便它可以使用环境变量作为参数值(如果存在),或者以其他方式提示。 function Test-Mandatory { [CmdletBinding()]
好的,首先是一些代码。这是我的 displayCollection.tag 的内容: ${irc.mgrid} 这是我从 (myq.jsp) 调用它的 JSP: 这是 irColle
这是BeanClass.java package com.practice.spring; import org.springframework.beans.factory.annotation.Re
我是一名优秀的程序员,十分优秀!