Spring的新注解
2024-09-02 15:31:37
1 @Configuration
1.1 作用
- 用于指定当前类是一个Spring的配置类,当创建容器的时候会从该类上加载注解。获取容器的时候需要使用AnnotationApplicationContext(有@Configuration注解的类的.class)。
1.2 属性
- value:用于指定配置类的字节码。
1.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.Configuration; /**
* @Configuration 注解标注的类就代表当前类是一个配置类
*/
@Configuration
public class SpringConfiguration {
}
2 @ComponentScan
2.1 作用
- 用于指定Spring在初始化容器时要扫描的包。作用和在Spring的xml配置文件中的<context-component-scan base-package="com.sunxiaping"/>是一样的。
2.2 属性
- basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。
2.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.sunxiaping.spring5")
public class SpringConfiguration {
}
3 @Bean
3.1 作用
- 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入到Spring容器中。默认情况下,方法名就是Bean的id。
3.2 属性
- name:给当前@Bean注解方法创建的对象指定一个名称(即Bean的id)。
3.3 应用示例
- 示例:
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;
import java.beans.PropertyVetoException; @Configuration
@ComponentScan("com.sunxiaping.spring5")
public class SpringConfiguration { @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setUser("root");
dataSource.setPassword("123456");
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
} }
4 @PropertySource
4.1 作用
- 用于加载.properties文件中的配置。例如我们配置数据源的时候,可以把连接信息写到properties文件中,就可以使用此注解指定properties配置文件的位置。
4.2 属性
- value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:
4.3 应用示例
- 示例:
- jdbc.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
jdbc.user=root
jdbc.password=123456
- JdbcConfig.java
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
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")
public class JdbcConfig { @Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password; @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setDriverClass(driverClass);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
}
5 @Import
5.1 作用
- 导入其他的配置类,在引起其他配置的时候,可以不用再写@Configuration注解。当然,写上也没什么关系。
5.2 属性
- value[]:用于指定其他配置类的字节码
5.3 应用示例
- 示例:
- JdbcConfig.java
package com.sunxiaping.spring5.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
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")
public class JdbcConfig { @Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password; @Bean
public DataSource dataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setDriverClass(driverClass);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
} @Bean
public QueryRunner queryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
}
- SpringConfig.java
package com.sunxiaping.spring5.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@ComponentScan("com.sunxiaping.spring5")
@Import(JdbcConfig.class)
public class SpringConfiguration { }
6 通过注解获取容器
- 示例:
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
最新文章
- JDK1.7 安装配置
- SQL脚本--有关压缩数据库日志
- python 各种控制语句
- python批量导出导入MySQL用户的方法
- 解决git Push时请求username和password,而不是ssh-key验证
- Difference Between XML and XAML.
- PHP中字符串类型与数值类型混合计算
- keil TEA
- VC操作Image的三种方法(收集)
- struts1.x中web.xml文件的配置
- 【转】Impala导出查询结果到文件
- centos7下kubernetes(13。kubernetes-探讨service IP)
- Linux中的pipe(管道)与named pipe(FIFO 命名管道)
- OpenGL的一些名词
- vue mock
- 01 C语言程序设计--01 C语言基础--第1章 C语言概述&;第2章 GCC和GDB
- Linux常用命令之文件和目录处理命令
- 替换iframe的内容
- 最新的Delphi版本号对照
- [UE4]Switch on String,根据字符串决定条件分支,类似于高级语言中的switch语句
热门文章
- Centos 7 安装tomcat并部署jar实录
- LoadRunner 技巧之 脚本设计
- Linux服务器重启后IP变掉的处理方式
- 物料批量盘点,调用其中两个BAPI BAPI_MATPHYSINV_COUNT BAPI_MATPHYSINV_CHANGECOUNT
- 【计算机视觉】ViBe - a powerful technique for background detection and subtraction in video sequences
- docker安装jenkins自动化部署
- PostgreSQL创建只读账户
- 小记---------FLUM的三种配置方式:spooldir、exec、hadoop sink
- Matcher和Pattern总结
- 从入门到自闭之Python 基础习题训练