Tag: spring boot

最小化Spring Boot启动时间

在我看来,SpringBoot项目需要很长时间才能加载。 这可能是因为SpringBoot正在为您配置组件,其中一些甚至可能不需要。 最明显的做法是从类路径中删除不必要的依赖项。 但是,这还不够。 有没有办法找出SpringBoot正在配置哪些模块来挑选你不需要的东西并禁用它们? 还有什么办法可以加快SpringBoot应用程序的启动时间吗?

控制器中的Spring Boot @Async方法同步执行

我的[基本] Spring Boot应用程序接受来自浏览器的请求,通过jQuery.get()发送,并且应该立即收到响应 – 例如“ 您的请求已经排队 ”。 为此,我写了一个控制器: @Controller public class DoSomeWorkController { @Autowired private final DoWorkService workService; @RequestMapping(“/doSomeWork”) @ResponseBody public String doSomeWork() { workService.doWork(); // time consuming operation return “Your request has been queued.”; } } DoWorkServiceImpl类实现了DoWorkService接口,非常简单。 它有一种方法来执行耗时的任务。 我不需要从此服务调用返回任何内容,因为电子邮件将在工作结束时发送,包括失败或成功方案。 所以它实际上看起来像: @Service public class DoWorkServiceImpl implements DoWorkService { @Async(“workExecutor”) @Override public void doWork() { try […]

如何在web.xml中配置spring-boot servlet?

我在web.xml中有一个简单的servlet配置: appServlet org.atmosphere.cpr.MeteorServlet org.atmosphere.servlet org.springframework.web.servlet.DispatcherServlet contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext contextConfigLocation net.org.selector.animals.config.ComponentConfiguration 1 true appServlet / 如何为SpringBootServletInitializer重写它?

如何在MySQL数据库和JPA中使用Spring Boot?

我想用MySQL和JPA设置Spring Boot。 为此我创造: 人 package domain; import javax.persistence.*; @Entity @Table(name = “person”) public class Person { @Id @GeneratedValue private Long id; @Column(nullable = false) private String firstName; // setters and getters } PersonRepository package repository; import domain.Person; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.CrudRepository; public interface PersonRepository extends CrudRepository { Page findAll(Pageable pageable); } PersonController package […]

Spring Boot在端口80上运行app

我无法在端口80上启动应用程序。我在IDE和服务器上试过本地计算机,没有运气。 我检查了其他类似的post,并确保我在root服务器上运行jar。 这是错误: till here all ok … java.net.SocketException: Permission denied at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.Net.bind(Net.java:425) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:338) at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:760) at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:472) at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardService.addConnector(StandardService.java:237) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:186) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:149) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at […]

通过注释而不是XML配置Spring LdapTemplate的最佳实践?

对于Spring Boot应用程序,我使用注释成功配置了Spring LdapTemplate ,包括来自application.properties的@Value的LdapContextSource依赖@Value 。 (Woot!我找不到一个例子,所以也许这会帮助别人。) 片段(下面)设置上下文源,将其注入LdapTemplate ,然后将其自动装入我的DirectoryService。 有没有更好/更清晰的方法在Spring Boot应用程序中设置ContextSource ? application.properties(在类路径上): ldap.url=ldap://server.domain.com:389 ldap.base:OU=Employees,OU=Users,DC=domain,DC=com ldap.username:CN=myuserid,OU=employees,OU=Users,DC=domain,DC=com ldap.password:secretthingy MyLdapContextSource.java: @Component public class MyLdapContextSource extends LdapContextSource implements ContextSource { @Value(“${ldap.url}”) @Override public void setUrl(String url) { super.setUrl(url); } @Value(“${ldap.base}”) @Override public void setBase(String base) {super.setBase(base); } @Value(“${ldap.username}”) @Override public void setUserDn(String userDn) {super.setUserDn(userDn); } @Value(“${ldap.password}”) @Override public void […]

在Spring Boot中部署Quartz时出现NullPointerException

我试图使用Quartz 2.2.1和spring boot。 我试图声明一个应该将一些数据写入文件的计划任务。 我的工作定义如下: public class JobTask implements Job { @Autowired JobController controller; @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { try { controller.doPrintData(); } catch (Exception e) { e.printStackTrace(); } } } 然后 : public class StartJob { public static void main(final String[] args) { final SchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler; […]

Spring Boot Hibernate 5忽略@Table和@Column

这让我很生气。 我正在实现Spring Social,它要求您拥有一个名为UserConnection的数据库表(而不是使用使用下划线分隔这两个单词的标准命名约定)。 所以在我天真的世界观中,我认为通过指定@Table(name=”UserConnection”)可以很容易地解决这个问题……但不,这太容易了。 注释被忽略,表被创建为user_connection ,然后导致Spring Social有一个合适的东西。 请告诉我有一些简单的方法告诉我的Spring Boot应用程序只是命名一个表(及其相应的列)以使用camel-case命名约定而不是标准约定。

如何自定义Spring Data REST以使用存储库资源的多段路径?

我正在使用Spring Data JPA和Spring Data REST开发基于组件的CRUD应用程序。 我有几个组件。 例如, 系统组件具有User模型和UserRepository 。 组件由包名称区分。 例如com.example.app. 因此,为了使我的REST API看起来更干净,我需要实现API URL,如下所示。 host:8080// 例如 host:8080/system/users 我在我的存储库中执行了以下操作 @RepositoryRestResource(collectionResourceRel = “users”, path = “system/users”) public interface UserRepository extends PagingAndSortingRepository { … } 当我转到http://localhost:8080时,会生成以下内容 { “_links”: { “users”: { “href”: “http://localhost:8080/system/users{?page,size,sort}”, “templated”: true }, … 但是当我转到http://localhost:8080/system/users 它给出了一个错误 5月22日星期五17:56:37 IST 2015出现意外错误(type = Not Found,status = 404)。 没有消息可用 […]

Lazy One-To-One Spring JPA并构建“动态”JSON

我正在使用Spring Boot开发一个相对较大的项目,一般来说我对它很满意,但是我遇到了一些问题,在我看来应该不是问题。 首先,一对一的关系。 令人沮丧的是它不能正常工作(至少在我看来)。 我有两个实体,例如User和UserProfile 。 他们有一对一的关系,但大多数时候我只需要User数据,但它取而代之(无论我尝试什么,哦,男孩,我在5页Google的每篇post上尝试了世界建议)。 所以我的第一个问题是,有没有办法能够在JPA和Spring中懒惰地获取一对一的关系? (因为大多数post都超过2 – 3年)。 我遇到的另一个问题是以“动态”方式构建JSON响应。 我使用Rails做了一些事情,并且对JBuilder甚至to_json非常满意,这让我能够根据控制器和我目前的需求构建json响应。 在Spring中,我看到了以下解决方案: Jackson @JsonView (它不能完全解决我的问题,因为响应不是静态的,并且属性不能分配给多个视图(据我理解的概念)); 设置为null我不想要的响应属性(使用它,但我太丑了,看起来像一个错误的演练); 或者构建HashMap就像我在Rails上构建.json.jbuilder一样(但这会.json.jbuilder我的表现,因为有时它会有很多关系来构建json,而且这看起来像是一个丑陋的演练)。 我正在寻找某人的某些指示,有朝一日可能会遇到其中一个问题,因为它让我无法解决问题,而在我看来这不应该是这么难。 编辑1 已经尝试在@OneToOne注释上添加optional = false来解决@snovelli建议的OneToOne关系的Eager负载。 例: @OneToOne(optional=false, fetch = FetchType.LAZY) public UserProfile getUserProfile(){ … }