spring 与spring boot

使用mvn创建项目

例:

mvn archetype:create -DgroupId=com.apress.j2ee -DartifactId=simple-web-app
-DarchetypeArtifactId=maven-archetype-webapp

上面用mvn创建的项目,默认使用原始的servlet api开发


用mvn打包项目

mvn clean package


Groovy Beans in Spring Boot

With this Groovy beans DSL, you can create your Spring beans withoutthe XML clutter.

Listing5-13. app.groovy

@RestController

class SimpleWebApp {
    @Autowired
    String text
    @RequestMapping("/")
    String index() {
         "You can do: ${text}!"
    }

}

beans {
      text String, "Spring Boot with Groovy beans"

}

spring run app.groovy
Point your browser at http://localhost:8080. You will get "You can do: Spring Boot with Groovy

beans". So, you have ways to reuse Spring XML files or use the Groovy syntax to create some configurations.



Standalone Spring Apps vs. Spring Boot Apps

Not all applications are web apps; sometimes you need to run your Spring application in standalone modewithout any server. You simply run it as a regular service or as a job and finish. To run a Spring application,you normally use the following code in your main method:

public static void main(final String[] args) {
    final ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/
    app-ctx.xml");
    final Service service = context.getBean(ServiceFacade.class);
        //Some process to run here
        //Extra work here

}

This code is using the ApplicationContext interface and the ClassPathXmlApplicationContextclass to load the beans and initialize the container. After that you can use your beans by using the getBeanmethod. Then you can do some process or call some functions and finish. In Spring Boot, it’s a littledifferent. In order to execute some code after the Spring Boot is initialized and running, you have somechoices, as shown in Listing 5-14.

Listing5-14. SpringBootExample,ImplementingtheCommandLineRunnerInterface

package com.apress.spring;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public MyApplication implements
CommandLineRunner {

    public void run(String... args) {
        // This will run after the SpringApplication.run(..)
        // Do something...

}

    public static void main(String[] args) throws Exception {
             SpringApplication.run(MyApplication.class, args);

}}

Listing 5-14 shows you how you can run some processes or jobs after SpringApplication.run is called,by implementing the org.springframework.boot.CommandLineRunner interface and implementing therun(String... args) method. This is useful when you want to execute jobs or services, such as send anotification about the application or execute a SQL statement to update some rows before your applicationruns. This is not a web application; it is a standalone app.


Using Spring Technologies in Spring Boot

I showed you in the previous sections of this chapter that Spring Boot is Spring, and you can use any Springbeans defined in a XML or a Java Configuration class. But what about some of the Spring technologies, suchas Spring JMS, Spring AMQP, Spring Integration, Spring Caching, Spring Session, Spring Rest, etc.?

The following chapters show you how to use all these technologies in more detail, but I can tell younow that the auto-configuration is the base of this, which means all the new annotations that SpringFramework version 4 uses. The key here is to get used to some of the annotations that allow you to use thesetechnologies very easily.

The only thing you need to know now is that there is an annotation called @Enable<Technology> foreach of these technologies;







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值