GVKun编程网logo

在自定义@RepositoryRestController方法中填充实体链接(怎么加载自定义填充)

11

在本文中,我们将带你了解在自定义@RepositoryRestController方法中填充实体链接在这篇文章中,我们将为您详细介绍在自定义@RepositoryRestController方法中填充

在本文中,我们将带你了解在自定义@RepositoryRestController方法中填充实体链接在这篇文章中,我们将为您详细介绍在自定义@RepositoryRestController方法中填充实体链接的方方面面,并解答怎么加载自定义填充常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的@Component 注解、@Service 注解、@Repository 注解、@Controller 注解区别、@Component、@Controller、@Service、@Repository的区别与联系、@Controller,@Service,@Repository,@Component你搞懂了吗?、@Controller,@Service,@Repository,@Component是什么

本文目录一览:

在自定义@RepositoryRestController方法中填充实体链接(怎么加载自定义填充)

在自定义@RepositoryRestController方法中填充实体链接(怎么加载自定义填充)

我正在使用Spring-data-
rest在某些JPA实体上提供读取的API。对于写操作,我需要发出Command对象,而不是直接写到DB,因此我添加了一个使用@RepositoryRestController各种命令处理方法的自定义控制器:

@RequestMapping(method = RequestMethod.POST)public @ResponseBody MyEntity post(@RequestBody MyEntity entity) {  String createdId = commands.sendAndWait(new MyCreateCommand(entity));  return repo.findOne(createdId);}

我希望输出像spring-data-rest控制器的任何其他响应一样,得到充实,特别是我希望它为自身及其关系添加HATEOAS链接。

答案1

小编典典

最近,Oliver Gierke亲自回答了这个问题(请参见第3点)(尽管问题使用了完全不同的关键字,所以我不会将其标记为重复)。

单个实体的示例将变为:

@RequestMapping(method = RequestMethod.POST)public @ResponseBody PersistentEntityResource post(@RequestBody MyEntity entity,    PersistentEntityResourceAssembler resourceAssembler)) {  String createdId = commands.sendAndWait(new MyCreateCommand(entity));  return resourceAssembler.toResource(repo.findOne(createdId));}

非分页列表的示例:

@RequestMapping(method = RequestMethod.POST)public @ResponseBody Resources<PersistentEntityResource> post(    @RequestBody MyEntity entity,    PersistentEntityResourceAssembler resourceAssembler)) {  List<MyEntity> myEntities = ...  List<> resources = myEntities      .stream()      .map(resourceAssembler::toResource)      .collect(Collectors.toList());  return new Resources<PersistentEntityResource>(resources);}

最后,对于分页响应,应该使用注入的PagedResourcesAssembler,传入方法注入的ResourceAssembler和Page,而不是实例化Resources。有关如何使用的更多细节PersistentEntityResourceAssembler,并PagedResourcesAssembler可以发现这个答案。请注意,目前这需要使用原始类型和未检查的强制类型转换。

可能还有自动化的空间,欢迎更好的解决方案。

PS:我还创建了JIRA票证,将其添加到Spring
Data的文档中。

@Component 注解、@Service 注解、@Repository 注解、@Controller 注解区别

@Component 注解、@Service 注解、@Repository 注解、@Controller 注解区别

---------------------------------------------------------------------------------------------------

  Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。
在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。

@Service 用于标注业务层组件 服务 (注入 dao)

@Controller 用于标注控制层组件(如 struts 中的 action)控制器(注入服务)

@Repository 用于标注数据访问组件,即 DAO 组件

@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。(把普通 pojo 实例化到 spring 容器中,相当于配置文件中的 <bean id=""/>)

@Component: 定义Spring管理Bean

扩展:

@Component 扩展,被 @Service 注解的 POJO 类表示 Service 层实现,从而见到该注解就想到 Service 层实现,使用方式和 @Component 相同;

@Component 扩展,被 @Controller 注解的类表示 Web 层实现,从而见到该注解就想到 Web 层实现,使用方式和 @Component 相同;

@Component,@Service,@Controller,@Repository 注解的类,并把这些类纳入进 spring 容器中管理。 

下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.mmnc”>    

其中base-package为需要扫描的包(含所有子包) 
       1@Service用于标注业务层组件 
       2@Controller用于标注控制层组件(如struts中的action) 
       3@Repository用于标注数据访问组件,即DAO组件. 
       4@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
           @Service 
           public class UserServiceImpl implements UserService { } 
           @Repository 
           public class UserDaoImpl implements UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)       
                   这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
           @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { }

  

Spring2.5 为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进 spring 容器中管理。
它的作用和在 xml 文件中使用 bean 节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:

代码

<?xml version="1.0" encoding="UTF-8" ?> 
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
   
	<context:component-scan base-package=”com.eric.spring”>   
</beans>

  

getBean 的默认名称是类名(头字母小写),如果想自定义,可以 @Service (“aaaaa”) 这样来指定。
这种 bean 默认是 “singleton” 的,如果想改变,可以使用 @Scope (“prototype”) 来改变。

可以使用以下方式指定初始化方法和销毁方法:

@PostConstruct
public void init() { 
} 
@PreDestroy
public void destory() { 
}

  

注入方式:

把 DAO 实现类注入到 action 的 service 接口 (注意不要是 service 的实现类) 中,注入时不要 new 这个注入的类,因为 spring 会自动注入,如果手动再 new 的话会出现错误,
然后属性加上 @Autowired 后不需要 getter () 和 setter () 方法,Spring 也会自动注入。  

在接口前面标上 @Autowired 注释使得接口可以被容器注入,如:

@Autowired
@Qualifier("chinese")
private Man man;

  

@Component、@Controller、@Service、@Repository的区别与联系

@Component、@Controller、@Service、@Repository的区别与联系

联系:都是为了使得被其标注的类可以被spring自动扫描到并纳入到spring的容器中进行管理。

区别:

@Component:后3者父注解,泛指各种spring组件,当某个类不适用与一下几种归类的时候,一般用该注解标注类。

@Controller:用于标注控制层的类。

@Service:用于标注服务层的类。

@Repository: 用于标注数据访问层的类。

总的来说4个注解可以混用,不过为了更好的区分类的分类以及用途一般都分开使用。就好比篮球服、网球服等同样是衣服,你穿篮球服去打网球,穿网球服去打篮球那也是完全没毛病的,不过为了更好的适应对应场景一般还是会选择穿对应的衣服。

@Controller,@Service,@Repository,@Component你搞懂了吗?

@Controller,@Service,@Repository,@Component你搞懂了吗?

@Controller

用来表示一个web控制层bean,如SpringMvc中的控制器。

@Service

用来表示一个业务层bean。

@Repository

用来表示一个持久层bean,即数据访问层DAO组件。

@Component

用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。

需要注意的是@Controller,@Service,@Repository都有带@Component父注解,说明它们除了基本组件的属性外还有其他的的场景应用,即如果不用SpringMVC其实它们就是一个普通的组件,但普通组件建议最好还是用@Component修饰。

为了让Spring自动扫描注册这些组件,需要在配置文件中加上扫描的配置,如扫描com.test包下的注解。

<context:component-scan base-package="com.test" />

些扫描配置默认use-default-filters="true",默认扫描@Component注解及子注解,可以配置过滤只扫描哪些注解不扫描哪些注解。

要过滤扫描注解,需要相应的带上下面的子标签,可以有多个。

context:include-filter>

<context:exclude-filter

如只扫描com.test包下的@Controller和@Service注解的组件。

<context:component-scan base-package="com.test" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>

</context:component-scan>

关于type的定义

另外,<context:component-scan>配置可以有多个。

推荐去我的博客阅读更多:

1.Java JVM、集合、多线程、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、后端、架构、阿里巴巴等大厂最新面试题

觉得不错,别忘了点赞+转发哦!

@Controller,@Service,@Repository,@Component是什么

@Controller,@Service,@Repository,@Component是什么

本篇内容介绍了“@Controller,@Service,@Repository,@Component是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

@Controller

用来表示一个web控制层bean,如SpringMvc中的控制器。

@Service

用来表示一个业务层bean。

@Repository

用来表示一个持久层bean,即数据访问层DAO组件。

@Component

用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。

@Controller,@Service,@Repository,@Component是什么

需要注意的是@Controller,@Service,@Repository都有带@Component父注解,说明它们除了基本组件的属性外还有其他的的场景应用,即如果不用SpringMVC其实它们就是一个普通的组件,但普通组件建议最好还是用@Component修饰。

@Controller,@Service,@Repository,@Component是什么

为了让Spring自动扫描注册这些组件,需要在配置文件中加上扫描的配置,如扫描com.test包下的注解。

<context:component-scan base-package="com.test" />

些扫描配置默认use-default-filters="true",默认扫描@Component注解及子注解,可以配置过滤只扫描哪些注解不扫描哪些注解。

要过滤扫描注解,需要相应的带上下面的子标签,可以有多个。

<context:include-filter>

<context:exclude-filter>

如只扫描com.test包下的@Controller和@Service注解的组件。

<context:component-scan base-package="com.test" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 

</context:component-scan>

关于type的定义

Filter Type Examples Expression Description
annotation org.example.someAnnotation 符合SomeAnnoation的target class
assignable org.example.someClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ语法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter 自定义Type,实现接口org.springframework.core.type.TypeFilter

另外,<context:component-scan>配置可以有多个。

“@Controller,@Service,@Repository,@Component是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注小编网站,小编将为大家输出更多高质量的实用文章!

关于在自定义@RepositoryRestController方法中填充实体链接怎么加载自定义填充的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于@Component 注解、@Service 注解、@Repository 注解、@Controller 注解区别、@Component、@Controller、@Service、@Repository的区别与联系、@Controller,@Service,@Repository,@Component你搞懂了吗?、@Controller,@Service,@Repository,@Component是什么等相关内容,可以在本站寻找。

本文标签: