我正在将我的应用从spring 3.x升级到4.3。我正在删除xml配置并为其使用注释。我有以下例外:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Properties 'authorities' and 'username' are required for bean 'getJobUsingQuartz'
以下xml代码的注释是什么:
<bean id="mcn"
class="quartzJobs.jobs.MyClassName">
<property name="username" value="CronUser"/>
<property name="authorities">
<list>
<value>ROLE_SYSTEM</value>
</list>
</property>
</bean>
我想使用注释向属性注入上述值。我正在尝试跟随:
MyClassName.java
@Configuration
@ComponentScan(basePackages = "quartzJobs.jobs")
public class MyClassName{
@Autowired
@Value("CronUser")
private String username;
@Autowired
@Value(value = "{ROLE_SYSTEM}")
private List<String> authorities;
@Required
public
void setUsername(final String aUsername)
{
username = aUsername;
}
@Required
public
void setAuthorities(final List<String> aAuthorities)
{
authorities = aAuthorities;
}
}
我做错了什么?请帮帮我。
答案 0 :(得分:1)
这可以通过Spring Expression Language(简称SpEL)来实现,这是一种强大的表达式语言,支持在运行时查询和操作对象图。语言语法类似于Unified EL,但提供了其他功能,最着名的是方法调用和基本字符串模板功能。
这适用于您的情况:
@Value("#{new String[]{\"ROLE_SYSTEM\"}}")
private List<String> authorities;
答案 1 :(得分:0)
将课程更改为
@Configuration
@ComponentScan(basePackages = "quartzJobs.jobs")
public class MyClassName{
private String username;
private List<String> authorities;
@Required
public void setUsername(final String aUsername)
{
username = aUsername;
}
@Required
public void setAuthorities(final List<String> aAuthorities)
{
authorities = aAuthorities;
}
}
无论您何时使用MyClassName的实例 你可以将该对象注入
@Autowired
private MyClassName myClassName;
自动装配将基于Type。 and please refer this Link