Spring Boot入门 (二) :无xml配置实现

无xml配置的实现

  • 自Spring3.X 以后 spring 提供了很多的注解来代替XML文件的配置,最核心的是下面两个注解。

    • ::@Configuration:: 标注该类是配置类,类似于我们定义的applicationContext.xml
    • ::@Bean:: 类似于我们在之前的spring配置文件中配置的<bean id=" " class="">
  • 有了上面两个注解我们可以用编码的方式来完成spring 的相关配置,接下来我们就来使用java编码的方式来实现spring配置

  • 1、新建一个工程,里面是使用原生Spring实现无配置文件。
    — 建立July-javaconfig 工程
    — 在pom.xml中加入一下内容:

    <properties>
        <java.version>1.8</java.version>
        <spring.version>5.0.8.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

这里使用spring 5.0.8.RELEASE 版本。
— 在工程中新建包 cn.org.july.spring.service
— 在该包下新建Class HelloService.class,该service 实现一个方法sayHello(),代码如下:

package cn.org.july.spring.service;

/***
 * @author***wanghongjie*
 * 2018-10-26*
 **/
public class HelloService {

    public String sayHello(){
        return "Hello JavaConfig.....";
    }
}

— 在该目录下创建Class ApplicationConfiguration,该类相当于定义了一个applicationContext.xml。
在ApplicationConfiguration.class 中定义一个方法,该方法获取HelloService 。代码如下:

package cn.org.july.spring.service;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

 /***
  * 使用 ApplicationConfiguration 代替 配置文件*
  *  @author   wanghongjie
  */
@Configuration  //相当于定义了一个applicationContext.xml
public class ApplicationConfiguration {

    @Bean  // 相当于在applicationContext.xml中定义一个bean标签
    public HelloService helloService(){
        return new HelloService();
    }
}

— 创建测试类:Application.class,这里引入一个新的ClassAnnotationConfigApplicationContext对象。通过该类来加载Spring上下文信息。
存在applicationContext.xml

public static void main(String[] args) {
        //所有配置文件
        args = new String[] {
                "classpath:spring/spring-servlet.xml",
                "classpath:spring/ApplicationContext.xml",
                "classpath:spring/mybatis-config.xml",
        };
        ApplicationContext actx = new ClassPathXmlApplicationContext(args);
        //得到类的实例
        HelloService helloService = (HelloService) actx.getBean("helloService");
        //调用类的方法
        helloService.sayHello();
        
    }

— 不使用applicationContext.xml文件获取Spring上下文信息。

package cn.org.july.spring.service;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {

    public static void main(String[] args) {
        // 获取spring 容器。
        AnnotationConfigApplicationContext configApplicationContext
                = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        //从spring容器中获取bean
        HelloService helloService = configApplicationContext.getBean(HelloService.class);
        //方法调用
        String value = helloService.sayHello();
        System.*out*.printf(value);
    }
}

项目结构如下:
项目结构

运行结果:
运行结果

以上是两种启动方式对比。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Julywhj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值