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,以此使我们的项目更加稳定。

最新文章

  1. Vector Calculus
  2. 中转Http请求
  3. Mbps、Kbps、bps、MB、KB
  4. HDU 3911 Black And White(线段树区间合并+lazy操作)
  5. paper 110:凸优化和非凸优化
  6. Jquery easyui-combobox 的一个BUG
  7. 左侧菜单 z
  8. 地图 ajax-数据库
  9. main函数是个什么东西
  10. hdu 4630 树状数组
  11. PHP - 验证用户名
  12. VMware虚拟机安装
  13. 设计模式总结篇系列:原型模式(Prototype)
  14. Error occurred during initialization of VM Could not reserve enough space for 2097152KB object heap
  15. Linux 命令整理-ps
  16. 排序函数 sort() 和 高阶函数sorted()
  17. js中函数对象创建的总结
  18. Harbor镜像清理
  19. nohup 和 &amp;的含义
  20. MAT eclipse内存分析工具

热门文章

  1. 编写高质量代码改善C#程序的157个建议——建议77: 正确停止线程
  2. 关于.NET C#调用Sqlite的总结二
  3. IIS 发布webservice 需要用户名和密码访问 解决
  4. [NetCore学习记录]第一章.使用netcore撸个简单的增删改查
  5. Autofac的简单使用
  6. 序列(DP)(组合数)
  7. 【程序】必看干货:Photon多人游戏开发教程
  8. 2个list取交集
  9. How to mount a remote directory in Linux using sshfs
  10. linux下的常用指令