Spring新注解-配置注解

📅 2026/8/2 16:04:58
Spring新注解-配置注解
1.Spring新注解使用上面的注解还不能全部替代xml配置文件还需要使用注解替代的配置如下:非自定义的Bean的配置 bean加载 properties文件的配置 context:property-placeholder组件扫描的配置:context:componet-scan引入其他文件import新注解:Configuration :用于指定当前类是一个 Spring配置类当创建容器时会从该类上加载注解。ComponentScan :用于指定 Spring 在初始化容器时要扫描的包作用和 Spring的xml 配置文件中的context:component-scan base-packagecom.ycl/ 一样。Bean :使用在方法上标注该方法的返回值存储到Spring容器中PropertySource :用于加载 .properties文件中的配置。Import :用于导入其他配置类为了“完全替代XML配置文件”而设计出的核心注解组合属于同一个配置体系。具体细节如下Configuration、Bean、Import这三者是 Java 配置类的核心铁三角100% 确认为 Spring 3.0 引入用于将 XML 中的 beans 和 bean 标签转换为 Java 代码。ComponentScan注解本身是在 Spring 3.0 引入的用于替代 XML 中的 context:component-scan 标签。补充一个小冷知识虽然 context:component-scan 标签在 Spring 2.5 就有了但把它写成代码注解 ComponentScan 确实是 3.0 才有的。PropertySource也是 Spring 3.0 引入的用于替代 XML 中的 context:property-placeholder以便加载外部 .properties 配置文件。一个快速记忆点Spring 3.0 的官方宣传口号就是 JavaConfigJava配置你列出的这 5 个注解恰好就是 JavaConfig 时代的“标准全家桶”。只要你在项目里同时使用它们就代表项目完全告别了 XML。2.代码实现vim jdbc.properties jdbc.drivercom.mysql.jdbc.Driver jdbc.urljdbc:mysql://192.168.3.11:3306/test jdbc.usernametest jdbc.passwordtest #子配置类 package com.ycl.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; import java.beans.PropertyVetoException; //我们把它当作一个子配置类。 PropertySource(classpath:jdbc.properties)//等于上面的xml配置 public class DataSourceConfiguration { //下面的注入提到配置文件中的 bean iddataSource class中的内容。 Value(${jdbc.driver}) private String driver; Value(${jdbc.url}) private String url; Value(${jdbc.username}) private String username; Value(${jdbc.password}) private String password; Bean(dataSource)//spring会将当前方法的返回值以指定名称存储到Spring容器 public DataSource getDataSource() throws PropertyVetoException { ComboPooledDataSource dataSource new ComboPooledDataSource(); dataSource.setDriverClass(driver); dataSource.setJdbcUrl(url); dataSource.setUser(username); dataSource.setPassword(password); return dataSource; } } #总配置类 package com.ycl.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; //我们把它当作一个主配置类。 Configuration //标志该类是Spring的一个核心配置类。注解代替标签 //context:component-scan base-packagecom.ycl/context:component-scan ComponentScan //等同于上面这句xml配置 //context:property-placeholder locationclasspath:jdbc.properties/ Import({DataSourceConfiguration.class}) public class SpringConfiguration { } #使用配置类提交XML; package com.ycl.web; import com.ycl.config.SpringConfiguration; import com.ycl.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserController { public static void main(String[] args) { //ClassPathXmlApplicationContext app new ClassPathXmlApplicationContext(applicationContext.xml); //这样写使用的是我们自定义的spring总配置类和 xml配置文件无关了。 ApplicationContext app new AnnotationConfigApplicationContext(SpringConfiguration.class); UserService userService app.getBean(UserService.class); userService.save(); } } #如下的XML配置基本上被配置类提交了不需要再写XML配置文件了。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd !--加载外部配置文件properties-- context:property-placeholder locationclasspath:jdbc.properties/ bean iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSource property namedriverClass value${jdbc.driver}/property property namejdbcUrl value${jdbc.url}/property property nameuser value${jdbc.username}/property property namepassword value${jdbc.password}/property /bean !--配置组件扫描 扫面这个包及其子包 -- context:component-scan base-packagecom.ycl/context:component-scan /beans