这真的很奇怪.我开始将Spring Boot项目作为一个单独的maven项目开始,并且完美无缺.基本上它是一个带有安全性和邮件的Spring MVC应用程序.
然后,当我看到一些组件(如服务,存储库,模型)将被独立应用程序重用时,我决定将maven项目拆分为子模块.
突然没有自动装配开始工作.经过一番调查后,我发现我需要将这些软件包明确地放在我的独立应用程序的应用程序中:
@ComponentScan (basePackages={"service"})
@EnableJpaRepositories(basePackages={"repository"})
@EnableAutoConfiguration
@EntityScan(basePackages={"model"})
Run Code Online (Sandbox Code Playgroud)
好的,之后我的自定义类开始自动装配.但是出现了另一个问题
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.mail.javamail.JavaMailSender service.impl.UserServiceImpl.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at …
Run Code Online (Sandbox Code Playgroud) 如何让Spring启动加载Groovy的外部属性?需要类似于java机制的东西(资源中的application.properties和带有@Value注释的ConfigBean)?
当尝试使用与java相同的机制时,我不知道如何注释ConfigBean
@Component
public class ConfigBean {
@Value("${seleniumAddress}")
private String seleniumAddress; ...
Run Code Online (Sandbox Code Playgroud)
然后在 application.properties
seleniumAddress=http://localhost:4444/wd/hub
Run Code Online (Sandbox Code Playgroud)
但是对于groovy,我无法使用@Value("${seleniumAddress}"
它来注释该字段,它会引发错误抱怨"$ {}" - 这是groovy中的特殊序列.那么我应该在这里使用什么机制呢?
谢谢
如何从命令行dropins
中将插件放入自定义文件夹的方式开始eclipse ?我需要这样做,以避免在两个eclipse实例要在具有不同插件版本的同一台机器上运行时发生冲突。
什么是更好的?当 REST 端点查询实体时 - 一次返回所有实体和子实体,然后使用一些客户端代码(我们称之为“急切”模式)将它们显示在 UI 上,或者最好返回主要实体而不是它的子实体,返回这些子实体的 id,然后让 UI 照顾并为每个 id 发出正确的 REST 请求?(我们称之为lazy
模式)。
所以要返回这个 JSON(这实际上不是一个有效的 JSON,只是留下 region: 前缀让你了解它是什么实体):
country: {
name: 'C1',
regions: [
region: {
id: 'I1'
name: 'R1',
area: 'A1'
},
region: {
id: 'I2'
name: 'R2',
area: 'A2'
},
]
}
Run Code Online (Sandbox Code Playgroud)
或这个 :
country: {
name: 'C1',
regions: ['I1','I2']
}
Run Code Online (Sandbox Code Playgroud)
进而:
GET /rest/region/I1
GET /rest/region/I2
Run Code Online (Sandbox Code Playgroud)
哪一个更好?什么时候使用哪个?谢谢
我需要获取给定日期的一年中的第几周。使用 SimpleDateFormat 会产生一年中最后一周的错误结果。
例子:
这会正确生成第 52 周:
SimpleDateFormat w = new SimpleDateFormat("w");
Calendar c = Calendar.getInstance();
c.set(2021, 11, 25);
System.out.println("Week: ${w.format(c.getTime())}");
Run Code Online (Sandbox Code Playgroud)
产生:Week: 52
但第二天已经被视为明年的第一周了?
SimpleDateFormat w = new SimpleDateFormat("w");
Calendar c = Calendar.getInstance();
c.set(2021, 11, 26);
System.out.println("Week: ${w.format(c.getTime())}");
Run Code Online (Sandbox Code Playgroud)
产生:Week: 1
这只发生在 Java 7 中,而不发生在 Java 8 及更高版本中!