刚开始使用Spring框架的时候,一般都是使用xml配置文件,然后使用注解的方式来实现。让我们学习一种使用Java类配置的方法,也称为JavaConfig来配置Spring框架。这种方式让我们告别了XML文件,完全通过Java系统中的技术点来实现。好处是开发过程中不需要在Java类和XML文件之间来回切换,只需要编写Java即可。在SpringBoot中,配置也是这样实现的。让我们从Spring框架的发展史开始,逐步深入!1.Spring的发展其实这些版本不用死记硬背,了解版本之间的大概变化即可1.1Spring1.X时代在Spring1.x时代,bean是通过xml文件配置的。配置分为不同的配置文件,需要经常在java类和xml配置文件之间切换。1.2Spring2.X时代随着JDK1.5带来的注解支持,Spring2.x可以使用注解来声明和注入bean,大大减少了xml配置文件,大大简化了项目开发。所以,问题是,我应该使用xml还是注释?最佳实践:应用的基础配置使用xml,如:数据源、资源文件等;使用注解进行业务开发,例如:将bean注入Service等;1.3弹簧3。配置方式,使用Java的配置方式可以更好的帮助你配置Bean,Spring4.x和Springboot都推荐使用Java的配置方式。目前我们公司使用的是Spring5版本。6也已经发布,但需要Java17及以上版本。后续公司可能会慢慢迭代!这样的新技术暂时不会在企业中使用。2、Spring的Java配置方式说明:Java配置方式也是Spring4.X推荐的。作用是可以完全替代XML配置。在频繁的切换开发中,我们完全可以针对Java语言进行开发。在实战之前,我们需要知道2.1中的@Configuration和@Bean这几个注解。看到这两个标签,相信有小伙伴已经知道这两个标签的作用了。说明:Spring的Java配置是通过@Configuration和@Bean注解实现的@Configuration:作用于类,表示该类相当于一个xml文件@Bean:作用于方法,返回对象,将对象存储在Spring容器在里面,相当于xml文件中的标签。下面我们通过一个案例使用Java配置来实现Spring框架的配置和使用。3.案例一先贴出我们最终的项目结构3.1使用Java配置案例说明要实现Springbean管理,只需使用Java类代替xml配置文件来实现用户查询。这里我们将数据封装到List集合中,遍历集合打印出来。3.2实现1)创建项目,创建普通项目,导入jar包或者创建maven项目,这里创建maven项目,贴出项目结构和pom.xml配置版本可以随意切换properties>4.125.1.2.RELEASEjunitjunit${junit.version}testorg.springframeworkspring-context<版本>${spring.version}org.springframeworkspring-beans${spring.version}版本>org.springframeworkspring-webmvc<版本>${spring.version}org.springframeworkspring-jdbc${spring.version}org.springframeworkspring-aspects${spring.version}2)编写用户类publicclassUser{privateStringuserName;私有字符串用户密码;私有整数用户名;公共用户(){超级();}publicUser(StringuserName,StringuserPass,IntegeruserId){super();this.userName=用户名;this.userPass=用户密码;this.userId=userId;}publicStringgetUserName(){返回用户名;}publicvoidsetUserName(StringuserName){this.userName=用户名;}publicStringgetUserPass(){返回用户密码;}publicvoidsetUserPass(StringuserPass){this.userPass=userPass;}publicIntegergetUserId(){返回userId;}publicvoidsetUserId(IntegeruserId){this.userId=userId;}@OverridepublicStringtoString(){返回“用户[userName=”+userName+“,userPass=”+userPass+“,userId=”+userId+“]”;}}3)编写Dao层,使用ListCollect模拟数据库实现数据交互publicclassUserDao{publicListqueryAll(){Listusers=newArrayList();//添加用户并模拟数据库for(inti=1;i<11;i++){Useruser=newUser("张三"+i,"123456",i);users.add(用户);}//返回用户返回用户;}}4)编写Service层实现用户业务逻辑publicclassUserService{//注入Dao层对象@ResourceprivateUserDaouserDao;publicListqueryAll(){returnuserDao.queryAll();}}Tips:我们的dao层和service层没有接口5)写一个Spring配置类来代替实例化Spring容器的xml文件//说明这个类是一个配置类,相当于一个xml文件@配置//扫描包@ComponentScan(basePackages="com.stt")publicclassSpringConfig{//创建dao层对象@BeanpublicUserDaouserDao(){returnnewUserDao();}//创建服务对象@BeanpublicUserServiceuserService(){returnnewUserService();}}Tips:这个类是我们的配置类。可以看出我们这里使用了三个注解。这些注解我们上面已经提到了,这里就不再赘述了。6)编写测试类并启动SpringContainer,完成测试publicclassSpringApplication{publicstaticvoidmain(String[]args){//获取SpringConfig类中的所有配置AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(SpringConfig.class);//获取Service对象UserServiceuserService=context.getBean(UserService.class);//调用方法Listlist=userService.queryAll();for(Useruser:list){System.out.println(user);}//销毁容器context.close();}}通过上面的例子,我们简单的完成了使用Java配置进行bean管理的实现。重点是配置类及其三个注解。4.案例24.1案例描述我们在开发项目中会连接数据库,数据配置写在db.properties文件中。我们需要读取文件并配置数据源。在这种情况下,我们将连接到数据库并使用dbcp数据库连接池。4.2问题1)如何使用Java配置读取db.properties文件2)读取后如何给dbcp连接池中的driver、url等属性赋值?然后我们带着疑问看下面的代码配置,在原有配置类的基础上修改配置类://说明这个类是一个配置类,相当于一个xml文件@Configuration//扫描包@ComponentScan(basePackages="com.stt")//读取外部配置文件@PropertySource(value="classpath:resource/db.properties")publicclassSpringConfig{//创建dao层对象@BeanpublicUserDaouserDao(){returnnewUserDao();}//创建服务对象@BeanpublicUserServiceuserService(){returnnewUserService();}//获取文件中的值,使用Value注解赋值给变量并保存//注意:我们使用${}表达式获取值,参数与配置中的key保持一致file@Value("${driver}")privateString驱动程序;@Value("${url}")私有字符串url;@Value("${user}")私有字符串用户;@Value("${pass}")私有字符串传递;//配置数据库连接池@BeanpublicDataSourcedataSource(){//创建DBCP连接池对象BasicDataSourcedataSource=newBasicDataSource();//设置属性dataSource.setDriverClassName(driver);数据源.setUrl(url);dataSource.setUsername(用户);数据源.setPassword(通过);返回数据源;}}测试类:公开classSpringApplication{publicstaticvoidmain(String[]args){//获取SpringConfig中的所有配置classAnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(SpringConfig.class);//获取服务对象UserServiceuserService=context.getBean(UserService.class);//获取数据源对象BasicDataSourcedataSource=(BasicDataSource)context.getBean("dataSource");System.out.println(数据源);//调用方法/*Listlist=userService.queryAll();for(Useruser:list){System.out.println(user);}*///销毁容器context.close();}}Tips:1)@PropertySource:我们可以使用这个注解来读取外部配置文件如何导入多个配置文件?@PropertySource(value={"classpath:db.properties","otherfilepath"})如果文件不存在怎么办?添加ignoreResourceNotFound属性为true,当文件不存在时不会报错@PropertySource(value={"classpath:db.properties","xxx"},ignoreResourceNotFound=true)2)@Value:我们使用这个注解注入变量固定值5.总结至此我们使用Java配置的方式完成了springbean的管理,使用起来非常简单。其中,我们使用了一个配置类和多个注解一起完成,希望对读者有所帮助