Spring新注解
之前我们在配置spring时,还是不能通过完全是注解的方式来取代xml配置文件,如applicationContext.xml中的一些标签.
- 非自定义的Bean的配置:< bean>
- 加载properties文件的配置:< context:property-placeholder>
- 组件扫描的配置:< context:component-scan>
- 引入其他文件:< import>
如下图所示
<!--使用注解开发时,需要配置组件扫描-->
<context:component-scan base-package="com.cn"></context:component-scan>
<!--加载配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
从而有spring的新注解来代替以上xml的配置,
- @ Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解 |
- @ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package=“com.itheima”/>一样
- @Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
- @PropertySource 用于加载.properties 文件中的配置
- **@Import 用于导入其他配置类