@NotNull @Min(18) private Integer age;
-
@NotNull
won't allow an empty value -
@Min(18)
won't allow if the age is less than 18 -
!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Handing Form Submission</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Form</h1> <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post"> <p>Id: <input type="text" th:field="*{id}" /></p> <p>Message: <input type="text" th:field="*{content}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html> The
th:action="@{/greeting}"
expression directs the form to POST to the/greeting
endpoint, while theth:object="${greeting}"
expression declares the model object to use for collecting the form data. The two form fields, expressed withth:field="*{id}"
andth:field="*{content}"
, correspond to the fields in theGreeting
object above@EnableSchedulingpublic class ScheduledTasks { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("The time is now " + dateFormat.format(new Date())); } }
-
The key components that make this code perform scheduled tasks are the
@EnableScheduling
and@Scheduled
annotations.@EnableScheduling
ensures that a background task executor is created. Without it, nothing gets scheduled.You use
@Scheduled
to configure when a particular method is run.Note: This example uses
fixedRate
, which specifies the interval between method invocations measured from the start time of each invocation. There are other options, likefixedDelay
, which specifies the interval between invocations measured from the completion of the task. You can also use@Scheduled(cron=". . .")
expressions for more sophisticated task scheduling.http://localhost:8080/greeting?name=World"
@RequestParam(value="name",required=false,defaultValue="World")@Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; private String firstName; private String lastName; private Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } }
Here you have a
Customer
class with three attributes, theid
, thefirstName
, and thelastName
. You also have two constructors. The default constructor only exists for the sake of JPA. You won't use it directly, so it is designated asprivate
.The
Customer
class is annotated with@Entity
, indicating that it is a JPA entity. For lack of a@Table
annotation, it is assumed that this entity will be mapped to a table namedCustomer
.The
Customer
'sid
property is annotated with@Id
so that JPA will recognize it as the object's ID. Theid
property is also annotated with@GeneratedValue
to indicate that the ID should be generated automatically@EnableJpaRepositories public class Application { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().setType(H2).build(); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("hello"); return lef; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(false); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.H2); return hibernateJpaVendorAdapter; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); }
In the configuration, you need to add the
@EnableJpaRepositories
annotation. This annotation tells Spring Data JPA that it should seek out any interface that extendsorg.springframework.data.repository.Repository
and automatically generate an implementation. By extendingJpaRepository
, yourCustomerRepository
interface transitively extendsRepository
. Therefore, Spring Data JPA will find it and create an implementation for you.Most of the content in
Application
sets up several beans to support Spring Data JPA and the sample:- The
dataSource()
method defines aDataSource
bean, as an embedded database to which the objects are persisted. - The
entityManagerFactory()
method defines aLocalContainerEntityManagerFactoryBean
that is ultimately used to createLocalContainerEntityManagerFactory
a bean that implements theEntityManagerFactory
interface. It is the bean through which JPA operations will be performed. Note that this factory bean'spackagesToScan
property is set to look for entities in the package named "hello". This makes it possible to work with JPA without defining a "persistence.xml" file. - The
jpaVendorAdapter()
method defines a Hibernate-based JPA vendor adapter bean for use by theEntityManagerFactory
bean. - The
transactionManager()
method defines aJpaTransactionManager
bean for transactional persistence.
@Service
public class FacebookLookupService { RestTemplate restTemplate = new RestTemplate(); @Async public Future<Page> findPage(String page) throws InterruptedException { System.out.println("Looking up " + page); Page results = restTemplate.getForObject("http://graph.facebook.com/" + page, Page.class); Thread.sleep(1000L); return new AsyncResult<Page>(results); } }
The class is marked with the
@Service
annotation, making it a candidate for Spring's component scanning to detect it and add it to the application context.The
findPage
method is flagged with Spring's@Async
annotation, indicating it will run on a separate thread. The method's return type isFuture<Page>
instead ofPage
, a requirement for any asynchronous service. This code uses the concrete implementation ofAsyncResult
to wrap the results of the Facebook query.@Configuration @EnableAsync @EnableAutoConfiguration @ComponentScan public class Application implements CommandLineRunner { @Autowired FacebookLookupService facebookLookupService; @Override public void run(String... args) throws Exception { // Start the clock long start = System.currentTimeMillis(); // Kick of multiple, asynchronous lookups Future<Page> page1 = facebookLookupService.findPage("GoPivotal"); Future<Page> page2 = facebookLookupService.findPage("CloudFoundry"); Future<Page> page3 = facebookLookupService.findPage("SpringFramework"); // Wait until they are all done while (!(page1.isDone() && page2.isDone() && page3.isDone())) { Thread.sleep(10); //millisecond pause between each check } // Print results, including elapsed time System.out.println("Elapsed time: " + (System.currentTimeMillis() - start)); System.out.println(page1.get()); System.out.println(page2.get()); System.out.println(page3.get()); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The
@EnableAsync
annotation switches on Spring's ability to run@Async
methods in a background thread pool.The
@EnableAutoConfiguration
annotation switches on reasonable default behaviors based on the content of your classpath. For example, it looks for any class that implements theCommandLineRunner
interface and invokes itsrun()
method. In this case, it runs the demo code for this guide.public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
@RestController
combines@Controller
and@ResponseBody
, two annotations that results in web requests returning data rather than a viewpublic class FacebookLookupService { RestTemplate restTemplate = new RestTemplate(); @Cacheable("hello") public Page findPage(String page) { return restTemplate.getForObject("http://graph.facebook.com/" + page, Page.class); } }
@Cacheable("hello")
. Spring's caching abstraction intercepts the call tofindPage
to check whether it's already been called. If so, Spring's caching abstraction returns the cached copy. Otherwise, it proceeds to invoke the method, store the response in the cache, and then return the results to the caller. - The
相关推荐
自学Spring所产生的代码,包含IOC、AOP的入门以及进阶,Spring与数据库(MySQL)的交互。(该资源不仅包含我写的代码,一切Spring、aspect、jdbc等所需jar包都俱全,项目导入就能跑起来)
Mybatis 最小依赖jar 不集成spring 联系版本,适合初学者自己写个小demo
在自学Spring Framework 开发参考手册2.5时做的学习笔记,其中在遇到手册上不能理解的地方的时候,在网上找了一些比较好的文档也放在了里面。
弹簧样品 Spring 自学示例项目
Spring5自学讲义
Spring入门实例,里面注释很清楚,用eclipse写的,直接可以导入
《JavaWeb整合开发完全自学手册》介绍如何整合Struts+Hibernate+Spring+Eclipse进行J2EE开发,所有实例都基于MyEclipseIDE开发,引领读者快速进入基于JavaWeb的J2EE应用领域。《JavaWeb整合开发完全自学手册》主要...
Spring_Security-3.0.1_中文自学教程
spring3.0mvc自学教程ppt+注解教程+注解实例+springmybatis教程+项目实例+中文api 初学(自学)着的不二法定,从入门到项目实例深入学习(浅-深)
spring揭秘自学用 王福强 高清版
spring in action 优秀的spring自学入门教材
很好自学spring和hibernate的资源
自学文档 适用于初学者 内附经典案例并有详细的操作步骤
前几日自几看spring3.0就这么简单这本书时候留下来的东西,可见本身会有些许问题,但是笔者本身是小白,所以只是更基础适合新手,大神自动略过。ppt
Spring_MVC_3.0快速自学实战指南
在自学的过程中,小生不才,整理了一点狂神的笔记同大家分享,这是狂神说Spring的全部笔记及代码,后续还会有springmvc、mybatis、mybatis-puls等部分的笔记以及代码,小生会尽快整理出来!最后一个小小的提醒:关注...
如果你打算自己琢磨学习spring,并要做些例子的话!应该能给您带来帮助
这是一份自学SpringBoot--入门文档,里面包含了从环境准备到最后基本熟练使用的各个技术要点,共31个文档,适合入门者学习。
Spring_3.0_MVC_简单自学教程(英文)