我正在学习春天3,我似乎没有把握背后的功能<context:annotation-config>
和<context:component-scan>
.
根据我的阅读,他们似乎处理不同的注释(@ Required,@ Autowired etc vs @Component,@ Repository,@ Service等),但也从我读过的内容中注册了相同的bean后处理器类.
为了让我更加困惑,有一个@Required
属性@Autowired
.
有人可以对这些标签有所了解吗?什么是相似的,什么是不同的,一个被另一个取代,它们相互完成,我需要其中一个,两者都有吗?
我知道@Component
在Spring 2.5中引入了注释,以便通过使用类路径扫描来消除xml bean的定义.
@Bean
是在3.0版本中引入的,可以用来@Configuration
完全摆脱xml文件并使用java配置.
是否可以重复使用@Component
注释而不是引入@Bean
注释?我的理解是,最终目标是在两种情况下都创建bean.
Spring注释是@Controller
一样的@Service
吗?
我知道@Controller
哪些可以用于URL
映射和调用业务逻辑.
而@Service
用来注释包含业务逻辑服务类.
我可以使用@Controller
而不是@Service
注释Service类吗?
所以我一直在学习一周中的Spring,一直在学习本教程
一切顺利,直到我试图将它整合到mongodb.所以我按照本教程.
但我的做法部分仍在使用第一个.所以我的项目目录结构是这样的.
src/
??? main/
? ??? java/
| ??? model/
| | ??? User.java
| ??? rest/
| | ??? Application.java
| | ??? IndexController.java
| | ??? UsersController.java
| ??? service/
| ??? UserService.java
??? resources/
??? application.properties
Run Code Online (Sandbox Code Playgroud)
这是我的model/User.java文件
package main.java.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="user")
public class User {
private int age;
private String country;
@Id
private String id;
private String name;
public User() {
super();
}
public String getId() { …
Run Code Online (Sandbox Code Playgroud) 在SpringSource博客条目中,以下句子引用了构造型.
因为
@Controller
是Spring的@Component
Stereotype注释的特化,所以Spring容器会自动检测该类作为容器组件扫描过程的一部分,创建bean定义并允许实例像任何其他Spring管理的组件一样被依赖注入.
这个单词刻板印象的用法是什么?这是一个技术性的春季术语吗?或者只是在一般意义上使用的刻板印象?
我正在使用spring-mvc开发一个Web应用程序.
现在可以使用@ Controller,@ Service和@Repository构造型.
我发现@Controller特别有用,特别是因为我正在使用
<context:component-scan base-package="my.cool.controller"/>
Run Code Online (Sandbox Code Playgroud)
现在,关于@Service和@Repository,到目前为止看起来像
那么,除了更好的例外,还有其他任何优势吗?注释类是否会影响性能?
我用Spring框架开发了几年的Web应用程序.最近,我的团队的新鲜问我一个问题,Spring有@Component
注释,是什么注释的真正目的@Repository
,@Service
,@Controller
?我试着给他答案,他们之间没有区别,只是为了识别java Bean的类型.如你所知,我的解释缺乏说服力,他没有买.
所以,我想问一个问题,什么是注释的真正目的@Repository
,@Service
,@Controller
?这些注释之间真正的区别是什么?
据我所知,@Service
它用于将类标记为业务逻辑类.为什么春天有用?我已经可以@Component
用来将类标记为bean.我明白@Service
比具体而言@Component
,但如何?
什么时候在春天使用服务或组件?
例如,负责将电子邮件或公共业务逻辑发送为"服务"或"组件"的模块是什么?有什么区别?
服务是否能够呼叫其他服务?有交易问题吗?或者服务应该只调用组件?
有人告诉我,服务永远不应该调用其他服务,而应该只调用组件,这意味着Controller-> Service-> Component-> DAO,但是我发现很多人分享了Controller-> Service-> DAO的概念.没有组件.
Spring中是否有关于此主题的系统设计标准?
spring ×9
java ×5
annotations ×3
autowired ×1
controller ×1
mongodb ×1
rest ×1
service ×1
spring-3 ×1
spring-mvc ×1
stereotype ×1