使用java配置来构建spring项目

  java配置是Spring4.x推荐的配置方式,可以完全代替xml配置,java配置是通过@Configuration和@Bean来实现的。@Configuration声明当前类是一个配置类,相当于Spring配置的xml文件,@Bean注解在方法上,声明当前方法的返回值为一个Bean。

  下面是自己使用java配置搭建Spring项目的demo:

1、IDE:spring tool suite,构建工具:maven,新建maven工程,注意spring和jdk的版本兼容问题,pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>powerx.io</groupId>
  <artifactId>spring4javaconfig</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
      <java.version>1.8</java.version>
  </properties>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.0.7.RELEASE</version>
      </dependency>
  </dependencies>
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>2.3.2</version>
              <configuration>
                  <source>${java.version}</source>
                  <target>${java.version}</target>
              </configuration>
          </plugin>
      </plugins>
  </build>
</project>

2、功能类的bean,其中StudentService使用@Bean方式由spring容器生成单例对象并存储,UserService使用注解方式由spring容器生成单例对象并存储,两种方式是等价的,并且在UserService中使用@autowired注解有容器为其属性studentService赋值进行初始化,这也是spring的核心技术之一IOC(依赖注入)。

package powerx.io.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired//注入studentService
    private StudentService studentService;
    
    public void sysout() {
        System.out.println("spring");
    }
    
    public void say() {
        studentService.say();
    }
}
package powerx.io.service;

public class StudentService {

    public void say() {
        System.out.println("I am student");
    }
}

3、java配置文件

package powerx.io;

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

import powerx.io.service.StudentService;

@ComponentScan("powerx.io")//自动扫描报名下所有使用@Service、@Component、@Repository和@Controller注解的类,并注册为bean
@Configuration
public class JavaConfig {

    @Bean//使用@Bean在java配置中定义bean,所以StudentService类无需加@Service注解
    public StudentService studentService() {
        return new StudentService();
    }
}

4、主类,加载spring容器,调用bean完成功能

package powerx.io;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import powerx.io.service.UserService;

public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        UserService us = context.getBean(UserService.class);
        us.sysout();
        us.say();
        context.close();
    }
}

  使用java配置搭建Spring项目非常方便,在实际应用中(非web项目),我们可以通过改造此demo来完成特定的功能,更好的管理我们的bean,以此使我们的项目更加稳定。

原文地址:https://www.cnblogs.com/hhhshct/p/9449135.html